Home > Database > Mysql Tutorial > Why Are High LIMIT OFFSET Values in MySQL Queries So Slow?

Why Are High LIMIT OFFSET Values in MySQL Queries So Slow?

DDD
Release: 2024-12-15 11:14:11
Original
667 people have browsed it

Why Are High LIMIT OFFSET Values in MySQL Queries So Slow?

Impact of Higher LIMIT OFFSET on MySQL Query Performance

When executing a SELECT query with a LIMIT clause and a large table, setting a high offset value can drastically slow down the query's execution time. This behavior is particularly noticeable when using the ORDER BY clause with the primary key.

For example, consider a table called "large" with over 16 million records (2GB in size). A query like the following:

SELECT * FROM large ORDER BY `id` LIMIT 0, 30
Copy after login

can execute much faster compared to a query with a higher offset, such as:

SELECT * FROM large ORDER BY `id` LIMIT 10000, 30
Copy after login

Both queries retrieve the same number of records (30), indicating that the performance difference is not due to the overhead of the ORDER BY operation.

To optimize the query, it is recommended to minimize the use of high LIMIT offsets. Instead, consider using a WHERE clause to select records based on a known identifier, which will result in a zero offset.

For instance, if the goal is to retrieve the latest 30 rows, the following query can be used:

SELECT * FROM large WHERE id > last_known_id LIMIT 0, 30
Copy after login

By maintaining a record of the last known ID (last_known_id), this approach allows the query to always start from an offset of zero, significantly improving performance.

The above is the detailed content of Why Are High LIMIT OFFSET Values in MySQL Queries So Slow?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template