Is InnoDB sorting really THAT slow?

前端 未结 6 999
挽巷
挽巷 2020-12-30 17:10

I had all my tables in myISAM but the table level locking was starting to kill me when I had long running update jobs. I converted my primary tables over to InnoDB and now m

6条回答
  •  萌比男神i
    2020-12-30 18:13

    That is a large result set (66,424 rows) that MySQL must manually sort. Try adding an index to metaward_achiever.modified.

    There is a limitation with MySQL 4.x that only allows MySQL to use one index per table. Since it is using the index on metaward_achiever.award_id column for the WHERE selection, it cannot also use the index on metaward_achiever.modified for the sort. I hope you're using MySQL 5.x, which may have improved this.

    You can see this by doing explain on this simplified query:

    SELECT * FROM `metaward_achiever` 
     WHERE `metaward_achiever`.`award_id` = 1507  
     ORDER BY `metaward_achiever`.`modified` DESC 
     LIMIT 100
    

    If you can get this using the indexes for both the WHERE selection and sorting, then you're set.

    You could also create a compound index with both metaward_achiever.award_id and metaward_achiever. If MySQL doesn't use it, then you can hint at it or remove the one on just award_id.

    Alternatively, if you can get rid of metaward_achiever.id and make metaward_achiever.award_id your primary key and add a key on metaward_achiever.modified, or better yet make metaward_achiever.award_id combined with metaward.modified your primary key, then you'll be really good.

    You can try to optimize the file sorting by modifying settings. Unfortunately, I'm not experienced with this, as our DBA handles the configuration, but you might want to check out this great blog: http://www.mysqlperformanceblog.com/

    Here's an article about filesort in particular: http://s.petrunia.net/blog/?p=24

提交回复
热议问题