简体   繁体   中英

Why is Mysql using the wrong index?

Mysql is using an index on (faver_profile_id,removed,notice_id) when it should be using the index on (faver_profile_id,removed,id). The weird thing is that for some values of faver_profile_id it does use the correct index. I can use FORCE INDEX which drastically speeds up the query, but I'd like to figure out why mysql is doing this.

This is a new table (35m rows) copied from another table using INSERT INTO.. SELECT FROM. I did not run OPTIMIZE TABLE or ANALYZE after. Could that help?

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" is an index on (faver_profile_id,removed,notice_id) “ idx_notice_id_profile_id”是(faver_profile_id,removed,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    

I don't know if its causing any confusion or not, but maybe by moving some of the AND qualifiers to the Item's join might help as its directly correlated to the ITEM and not the favorite. In addition, I've explicitly qualified table.field references where they were otherwise missing.

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 
      AND Item.Removed = 0
      AND Item.Nudity = 0
   WHERE Favorite.faver_profile_id = 11619
   AND Favorite.removed = 0
   ORDER BY Favorite.id DESC 
   LIMIT 26

So now, from the "Favorites" table, its criteria is explicitly down to faver_profile_id, removed, id (for order)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM