When querying a non-partitioned table, the query optimizer can leverage indexes for sorting and limit the data read based on the LIMIT clause. For example, in a non-partitioned table my_table with a single-column index idx_a on column a, the following
SELECT *
FROM my_table
ORDER BY a DESC
LIMIT 100;
This query can scan the idx_a index from the end and stop after reading 100 rows, regardless of the total number of rows in my_table.
Now, consider a partitioned table my_partition_table partitioned by the primary key. Suppose the same query is run:
SELECT *
FROM my_partition_table
ORDER BY a DESC
LIMIT 100;
In this case, the query does not use filesort, as confirmed by the EXPLAIN plan. However, since the table is partitioned by the primary key, column a spans across all partitions.
How does MySQL handle sorting in this situation? Specifically, how does it retrieve and merge the data from all partitions to produce the sorted result for column a efficiently?
Npartitions and each of them has its own index onathen a merge join algorithm would be able to produce a globally sorted result. What does theEXPLAINsay?