A table with about 70K records is displayed on a site, showing 50 records per page.
Pagination is done with limit offset,50 on the query, and the records can be ordered on different columns.
Browsing the latest pages (so the offset is around 60,000) makes the queries much slower than when browsing the first pages (about 10x)
Is this an issue of using the limit command?
Are there other ways to get the same results?
With large offsets,
MySQLneeds to browse more records.Even if the plan uses
filesort(which means that all records should be browsed),MySQLoptimizes it so that only$offset + $limittop records are sorted, which makes it much more efficient for lower values of$offset.The typical solution is to index the columns you are ordering on, record the last value of the columns and reuse it in the subsequent queries, like this:
which outputs:
To get to the next page, you would use:
, which uses the index on
(value, id).Of course this won’t help with arbitrary access pages, but helps with sequential browsing.
Also,
MySQLhas certain issues with late row lookup. If the columns are indexed, it may be worth trying to rewrite your query like this:See this article for more detailed explanations: