當它應該使用(faver_profile_id,已移除,id)上的索引時,Mysql正在使用(faver_profile_id,已移除,notice_id)上的索引。奇怪的是,對於faver_profile_id的某些值,它確實使用了正確的索引。我可以使用FORCE INDEX,這極大地加快了查詢速度,但我想知道爲什麼mysql正在這樣做。爲什麼Mysql使用錯誤的索引?
這是一個使用INSERT INTO .. SELECT FROM從另一個表複製的新表(35m行)。 之後我沒有運行OPTIMIZE TABLE或ANALYZE。可以幫助嗎?
SELECT `Item`.`id` , `Item`.`cached_image` , `Item`.`submitter_id` , `Item`.`source_title` , `Item`.`source_url` , `Item`.`source_image` , `Item`.`nudity` , `Item`.`tags` , `Item`.`width` , `Item`.`height` , `Item`.`tumblr_id` , `Item`.`tumblr_reblog_key` , `Item`.`fave_count` , `Item`.`file_size` , `Item`.`animated` , `Favorite`.`id` , `Favorite`.`created`
FROM `favorites` AS `Favorite`
LEFT JOIN `items` AS `Item` ON ( `Favorite`.`notice_id` = `Item`.`id`)
WHERE `faver_profile_id` =11619
AND `Favorite`.`removed` =0
AND `Item`.`removed` =0
AND `nudity` =0
ORDER BY `Favorite`.`id` DESC
LIMIT 26
查詢執行計劃: 「idx_notice_id_profile_id」 是(faver_profile_id,刪除,notice_id)
1 | SIMPLE | Favorite | ref | idx_faver_idx_id,idx_notice_id_profile_id,notice_id_idx | idx_notice_id_profile_id | 4 | const,const | 15742 | Using where; Using filesort |
1 | SIMPLE | Item | eq_ref | PRIMARY | PRIMARY | 4 | gragland_imgfave.Favorite.notice_id | 1 | Using where
應該高度@ajreal上面還好添加的執行計劃 – ajreal 2012-02-02 18:05:24
。 – makeee 2012-02-02 18:13:58