我得到的查詢在相當小的表上運行(每個行最多4000行)。查詢持續約4秒幷包含多個連接,但是,所有連接都使用索引。有很多聯接的Mysql緩慢查詢
下面是該查詢:
SELECT count(DISTINCT p0_.id) AS sclr0
FROM promoter p0_
LEFT JOIN user u1_ ON p0_.user_id = u1_.id
LEFT JOIN profile p2_ ON p0_.id = p2_.promoter_id
LEFT JOIN promoter_city p4_ ON p0_.id = p4_.user_id
LEFT JOIN city c3_ ON c3_.id = p4_.city_id
LEFT JOIN promoter_language p6_ ON p0_.id = p6_.user_id
LEFT JOIN language l5_ ON l5_.id = p6_.language_id
LEFT JOIN promoter_worker_type p8_ ON p0_.id = p8_.promoter_id
LEFT JOIN worker_type w7_ ON w7_.id = p8_.workertype_id
LEFT JOIN practise p9_ ON p0_.id = p9_.promoter_id
LEFT JOIN contract c11_ ON p0_.id = c11_.promoter_id
這裏是輸出的解釋:
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
| 1 | SIMPLE | p0_ | index | |UNIQ_BCB929A3A76ED| 5 | | 4161 | Using index |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
| 1 | SIMPLE | u1_ | eq_ref | PRIMARY | PRIMARY | 4 |promoteri.p0_.user| 1 | Using index |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
| 1 | SIMPLE | p2_ | ref |type,IDX_8157AA0F4| type | 5 | promoteri.p0_.id | 1 | Using index |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
| 1 | SIMPLE | p4_ | ref |PRIMARY,IDX_183C53| PRIMARY | 4 | promoteri.p0_.id | 1 | Using index |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
| 1 | SIMPLE | c3_ | eq_ref | PRIMARY | PRIMARY | 4 |promoteri.p4_.city| 1 | Using index |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
| 1 | SIMPLE | p6_ | ref |PRIMARY,IDX_19EE2A| PRIMARY | 4 | promoteri.p0_.id | 1 | Using index |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
| 1 | SIMPLE | l5_ | eq_ref | PRIMARY | PRIMARY | 4 |promoteri.p6_.lang| 1 | Using index |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
| 1 | SIMPLE | p8_ | ref |PRIMARY,IDX_37AC17| PRIMARY | 4 | promoteri.p0_.id | 1 | Using index |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
| 1 | SIMPLE | w7_ | eq_ref | PRIMARY | PRIMARY | 4 |promoteri.p8_.work| 1 | Using index |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
| 1 | SIMPLE | p9_ | ref |IDX_352E261F4B84B2|IDX_352E261F4B84B2| 5 | promoteri.p0_.id | 1 | Using index |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
| 1 | SIMPLE | c11_ | ref |IDX_E98F28594B84B2|IDX_E98F28594B84B2| 5 | promoteri.p0_.id | 6 | Using index |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
我知道,該聯接是不必要在此查詢,但是我用很多類似的查詢有一些where語句應用,這些連接真的很重要。
有什麼解釋,爲什麼這個查詢需要這麼久?
在連接表中WHERE子句中有列的其他查詢中......你仍然使用'LEFT JOIN',或者這些查詢使用'INNER JOIN'嗎? – 2013-02-20 18:23:44
其他查詢看起來一樣,只是有一些where語句添加。但是它並沒有改變這個事實,即這個查詢沒有任何位置,需要4秒才能執行。 – 2013-02-20 18:26:32
其他查詢也有一個計數(*)嗎? – DiMono 2013-02-20 18:28:09