简体   繁体   中英

Why does this MySQL query take so long for sorting even though it has an index?

I don't understand why the following query takes so long to sort. In the profiling it shows that the sorting takes very long even though there is an index for the sorting row.

The MySQL server is a dedicated machine, basically running nothing but MySQL in the current Debian stable version with 32 GB physical RAM and a decent MySQL configuration. Table player (INNODB) currently has ~32,000,000 entries. Indexes for the table player :

gameplayer over game, id, server (unique)

serverplayer over id, server (index)

date over date (index) <-- this is what we will sort by

The following query is used to get data about a certain player's stats for a game, thus getting information about the player, the stats and the actual game:

set profiling = 1;

SELECT *
FROM `player` `gs`
INNER JOIN `playerstats` `gss` ON (`gss`.`player` = `gs`.`id`)
INNER JOIN `game` `g` ON (`g`.`id` = `gs`.`game`)
WHERE `gs`.`id` = :id
AND `gs`.`server` = :server
ORDER BY `gs`.`date` DESC
LIMIT 10;

show profile for query 1;

set profiling = 0;

Here is the result for show profile:

starting    0.000124
Waiting for query cache lock    0.000070
checking query cache for query  0.000090
checking permissions    0.000066
checking permissions    0.000058
checking permissions    0.000056
Opening tables  0.000074
System lock 0.000058
Waiting for query cache lock    0.000093
init    0.000071
optimizing  0.000054
statistics  0.000136
preparing   0.000073
executing   0.000044
Sorting result  0.201156 // <========================
Sending data    0.042253
end 0.000046
query end   0.000038
closing tables  0.000043
freeing items   0.000042
Waiting for query cache lock    0.000037
freeing items   0.000039
Waiting for query cache lock    0.000035
freeing items   0.000032
storing result in query cache   0.000031
logging slow query  0.000030
cleaning up 0.000031

If I understand MySQL right, then it should use the serverplayer index for the WHERE conditions and the date index for the sorting, right? Then why would it take so long, when I have proper indexes available?

Or do I have to add the column date to the serverplayer index?

Read this link for details: http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html

In your specific case this condition is not meet and MySql doesn't use the index to order rows:

in some cases, MySQL cannot use indexes to resolve the ORDER BY, although it still uses indexes to find the rows that match the WHERE clause. These cases include the following:
....
The key used to fetch the rows is not the same as the one used in the ORDER BY :
SELECT * FROM t1 WHERE key2=constant ORDER BY key1;
......


However when an index on columns id + server + date is created, where id and date are columns used in WHERE clause to select rows, and date is the last column in the index, the above condition does not apply and MySql can use this index to sort rows and avoid the filesort.

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