explain select id, nome from bea_clientes where id in (
select group_concat(distinct(bea_clientes_id)) as list
from bea_agenda
where bea_clientes_id>0
and bea_agente_id in(300006,300007,300008,300009,300010,300011,300012,300013,300014,300018,300019,300020,300021,300022)
)
當我嘗試做以上(沒有解釋)時,MySQL只是忙於使用DEPENDENT SUBQUERY,這使得它變得如此緩慢。這就是優化器爲什麼要爲客戶端中的每個ID計算子查詢的原因。我甚至把IN參數放在一個group_concat中,認爲這將是一個簡單的「字符串」,以避免掃描的結果。爲什麼這個MySQL查詢性能很差(DEPENDENT_SUBQUERY)
我認爲這不會是一個5.5 + MySQL服務器的問題? MariaDb中的測試也是如此。
這是一個已知的錯誤嗎?我知道我可以將它改寫成一個連接,但這仍然很糟糕。
Generated by: phpMyAdmin 4.4.14/MySQL 5.6.26
Comando SQL: explain select id, nome from bea_clientes where id in (select group_concat(distinct(bea_clientes_id)) as list from bea_agenda where bea_clientes_id>0 and bea_agente_id in(300006,300007,300008,300009,300010,300011,300012,300013,300014,300018,300019,300020,300021,300022));
Lines: 2
Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available.
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|----|--------------------|--------------|-------|-------------------------------|---------------|---------|------|-------|------------------------------------|
| 1 | PRIMARY | bea_clientes | ALL | NULL | NULL | NULL | NULL | 30432 | Using where |
| 2 | DEPENDENT SUBQUERY | bea_agenda | range | bea_clientes_id,bea_agente_id | bea_agente_id | 5 | NULL | 2352 | Using index condition; Using where |
我更新了問題的擴展結果 – Miguel
'id IN ... GROUP_CONCAT(...)' - 你確定你想要嗎? 123 IN('123,456,789')'不會成功。這樣做:'123 IN('123','456','789')',但這不是你有的,也不能得到它。重來。 –
因爲這只是數字我相信你可以選擇退出引號...... – Miguel