2
希望你能幫助我在正確的軌道上開始優化我的查詢。我以前從未考慮過太多關於優化的問題,但我有幾個類似於下面的問題的查詢,並希望開始專注於提高其效率。我非常需要優化查詢的一個例子是如下:MySQL查詢優化幫助
SELECT COUNT(*) AS `records_found`
FROM (`records_owners` AS `ro`, `records` AS `r`)
WHERE r.reg_no = ro.contact_no
AND `contacted_email` <> "0000-00-00"
AND `contacted_post` <> "0000-00-00"
AND `ro`.`import_date` BETWEEN "2010-01-01" AND "2010-07-11" AND `r`.`pa_date_of_birth` > "2010-01-01" AND EXISTS (SELECT `number` FROM `roles` WHERE `roles`.`number` = r.`reg_no`)
運行解釋其對上述產生以下:
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+--------+---------------+---------+---------+---------------------------------------+-------+-------------+
| 1 | PRIMARY | r | ALL | NULL | NULL | NULL | NULL | 21533 | Using where |
| 1 | PRIMARY | ro | eq_ref | PRIMARY | PRIMARY | 4 | r.reg_no | 1 | Using where |
| 2 | DEPENDENT SUBQUERY | roles | ALL | NULL | NULL | NULL | NULL | 189 | Using where |
感謝MCHL。用連接取代子查詢並顯着加快查詢速度。我是否也應該在上表中添加索引? – ted776 2010-09-02 17:31:46
是的,你應該。 – Mchl 2010-09-03 04:49:33