SELECT DISTINCT *
FROM institutions
LEFT JOIN units ON Unit_Institution = Institution_Key
LEFT JOIN sites ON Site_Institution = Institution_Key OR Site_Key = Unit_Site
由于OR. 洗掉它的一側(無論哪一側)都會將查詢成本除以數千。我可以提供什么 MySQL 引擎來解決這個性能問題?
非常感謝。
uj5u.com熱心網友回復:
SELECT DISTINCT <ALIAS>.<FIELD_NAME>,*
FROM INSTITUTIONS INS
LEFT JOIN UNITS UNI ON UNI.UNIT_INSTITUTION = INS.INSTITUTION_KEY
LEFT JOIN SITES SIT1 ON SIT1.SITE_INSTITUTION = INS.INSTITUTION_KEY
LEFT JOIN SITES SIT2 ON SIT2.SITE_KEY = (?).UNIT_SITE
嘗試加入同一個表兩個馴服,引擎不應該每次都檢查“或”,但他會用索引優化關節我自己嘗試過一個例子,結果發現查詢帶有“或”作業了 52 秒(對于 10k 條記錄),相反,使用兩個“連接”進行查詢只用了 6 秒并且記錄數相同
uj5u.com熱心網友回復:
這join對 a 是不可優化的HASH JOIN,這就是執行時間過長的原因。
我的建議是將連接條件拆分為多個查詢,然后使用union方法連接結果。
IE:
select distinct *
from institutions
left join units ON Unit_Institution = Institution_Key
LEFT JOIN sites ON Site_Institution = Institution_Key
union
SELECT distinct *
from institutions
left join units ON Unit_Institution = Institution_Key
LEFT JOIN sites Site_Key = Unit_Site
uj5u.com熱心網友回復:
你可以試試這個:
SELECT DISTINCT coalesce(sites_si.field, sites_sk.field) AS field -- ...
FROM institutions
LEFT JOIN units ON Unit_Institution = Institution_Key
LEFT JOIN sites_si ON Site_Institution = Institution_Key
LEFT JOIN sites_sk ON Site_Key = Unit_Site
您進行兩個連接并使用合并將它們放在一起。根據您的資料,它可能會更好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/522270.html
標籤:mysqlsql
上一篇:使用游標從視圖中輸出多列
