我已經盡了一切努力使這個查詢更快,我知道。每個表的引擎相同,所有用於連接的字段的索引,order by或where子句。緩慢的MySQL查詢
問題似乎是表aam
和ag
沒有使用索引,儘管有索引可用。
查詢:
SELECT DISTINCT `a`.`id`, `a`.`full_name`, `a`.`rating`, `a`.`licence`, `a`.`licence_issued`, `ag`.`name` as agency_name
FROM (`property_suburb_map` psm)
JOIN `campaign_property_map` cpm ON `psm`.`property_id` = `cpm`.`property_id`
JOIN `campaign` c ON `cpm`.`campaign_id` = `c`.`id`
JOIN `campaign_agent_map` cam ON `cpm`.`campaign_id` = `cam`.`campaign_id`
JOIN `agent` a ON `cam`.`agent_id` = `a`.`id`
JOIN `agency_agent_map` aam ON `aam`.`agent_id` = `a`.`id`
JOIN `agency` ag ON `aam`.`agency_id` = `ag`.`id`
WHERE `c`.`closing_date` >= '2009-10-12'
AND `psm`.`suburb_id` = '5911'
AND `a`.`status` = 'Active'
ORDER BY `a`.`rating` DESC, `a`.`full_name`
LIMIT 12
解釋(對不起,格式化弄亂):使用此圖像,而不是http://imgur.com/UzSpC.jpg
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a ref PRIMARY,status status 1 const 790 Using where; Using temporary; Using filesort
1 SIMPLE aam ref agency_id_2,
agent_id,
agency_id agent_id 4 hindsight.a.id 1
1 SIMPLE ag eq_ref PRIMARY PRIMARY 4 hindsight.aam.agency_id 1
1 SIMPLE cam ref agent_id,
campaign_id agent_id 4 hindsight.a.id 9 Distinct
1 SIMPLE c eq_ref PRIMARY,closing_date PRIMARY 4 hindsight.cam.campaign_id 1 Using where; Distinct
1 SIMPLE cpm ref campaign_id campaign_id 4 hindsight.c.id 1 Using where; Using index; Distinct
1 SIMPLE psm ref property_id,suburb_id property_id 4 hindsight.cpm.property_id 1 Using where; Distinct
這裏是數據庫的相關結構http://pastebin.com/Rbyrj6x3
編輯我在查詢中完成了配置文件:Copying to tmp table
確實很慢。
mysql> show profile for query 6;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000000 |
| Opening tables | 0.000000 |
| System lock | 0.000000 |
| Table lock | 0.000000 |
| init | 0.000000 |
| optimizing | 0.000000 |
| statistics | 0.000000 |
| preparing | 0.000000 |
| Creating tmp table | 0.000000 |
| executing | 0.000000 |
| Copying to tmp table | 0.112000 | <- WTF!
| Sorting result | 0.004000 |
| Sending data | 0.000000 |
| end | 0.000000 |
| removing tmp table | 0.000000 |
| end | 0.000000 |
| query end | 0.000000 |
| freeing items | 0.000000 |
| logging slow query | 0.000000 |
| cleaning up | 0.000000 |
+----------------------+----------+
20 rows in set (0.00 sec)
解釋顯示正在使用aam和ag的索引。我不明白你爲什麼認爲他們沒有被使用。 – Martin 2010-10-11 06:33:10
我不是讀書專家解釋,但會假設'使用索引'會出現在右邊(額外)。謝謝你糾正我。如果所有的指標都在使用,我不確定瓶頸在哪裏。去除where和order子句對性能沒有顯着影響,但它仍然需要1秒以上。 – Keyo 2010-10-11 06:43:28
解釋輸出中的'key'列顯示選擇了哪個鍵。如果它包含NULL,則不使用索引。 「使用索引」表示正在使用某種索引掃描(而不是索引查找)。 – Martin 2010-10-11 08:15:41