2013-02-20 44 views
1

我得到的查詢在相當小的表上運行(每個行最多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語句應用,這些連接真的很重要。

有什麼解釋,爲什麼這個查詢需要這麼久?

+0

在連接表中WHERE子句中有列的其他查詢中......你仍然使用'LEFT JOIN',或者這些查詢使用'INNER JOIN'嗎? – 2013-02-20 18:23:44

+0

其他查詢看起來一樣,只是有一些where語句添加。但是它並沒有改變這個事實,即這個查詢沒有任何位置,需要4秒才能執行。 – 2013-02-20 18:26:32

+0

其他查詢也有一個計數(*)嗎? – DiMono 2013-02-20 18:28:09

回答

2

嘗試將所有這些left joins改爲inner joins。當您使用left join時,您會強制優化器按照您提供的順序加入表格......當您使用inner join時,優化程序可以選擇更好的加入表格的起始位置。

至於對left join迫使特定讀取順序的參考 - 從MySQL Docs

表中讀取順序被迫LEFT JOINSTRAIGHT_JOIN幫助 聯接優化做的工作更加迅速,因爲有更少的 表排列來檢查。

...指示left join強制讀取命令與STRAIGHT_JOIN相同。

+0

嗨,我很好奇你是如何發現這一點的? – Bijan 2014-04-25 21:20:14

+0

@Bijan我在MySQL文檔中添加了一個引用,它提到'left join'強制讀取順序......可能有更直接的引用,但是我找不到它。 – 2014-04-26 18:14:43

+0

對此有疑問。隨着刪除左連接,wouldnt這意味着與ID爲null的記錄將不再返回? – 2016-11-29 22:23:00