Hongmu Notes
Home Summary of pitfalls MySQL Cursor Query: Say goodbye to slow pagination with `WHERE id> last_id`
Summary of pitfalls

MySQL Cursor Query: Say goodbye to slow pagination with `WHERE id> last_id`

MySQL Cursor Query: Say goodbye to slow pagination with `WHERE id> last_id`

When developing applications that handle large volumes of data, we often need to iterate over the entire table to perform data processing tasks—such as generating sitemaps, exporting data, or performing batch updates. The most common approach is to use  LIMIT offset, size Paginated query. However, when... offset When the query scope grows significantly, it will become increasingly slow, potentially leading to timeouts or a surge in server load. This article introduces a simple yet highly efficient approach-Primary key-based cursor queryWHERE id > last_idIt ensures a constant data traversal speed, making it effortlessly capable of handling millions of data records.

The pain points of traditional pagination

Suppose we have a song list; music_songsThe primary key is ; song_idWe need to export the URLs of all songs into a sitemap. Traditional pagination approach:

SELECT song_id FROM music_songs ORDER BY song_id LIMIT 1000000, 1000;

When this SQL statement is executed, the database must first scan and skip the first 1 million rows, then return the next 1,000 rows. Even if  song_id When a primary key index is used, MySQL first reads 1 million index entries before fetching the corresponding data from the underlying table. When the offset is very large, this portion of the cost can be substantial; furthermore, as the page number increases, performance decreases linearly.

Worse still, using ; OFFSET Can lead torescanEach query starts counting from the first row; the process becomes slower as the query progresses. When generating a sitemap containing 6 million data entries, the initial few hundred thousand entries can be returned relatively quickly; however, for subsequent batches, each batch may take dozens of seconds or even several minutes to process, and the entire export process can take several hours.

The core concept of the CUBIC query

The CROSOLAR query leverages the auto-incrementing primary key feature, querying only a single record each time; id > Previous maximum; id This record prevents the "skipping" of already read data. Its basic form is:

SELECT * FROM table WHERE id > last_id ORDER BY id LIMIT batch_size;

After each query, record the maximum value for this batch. id As the next phase; last_idRepeat this process until no data is returned.

The query performance for this approach isconstantThis is because each time the primary key index is used to locate the record directly; last_id Next position; then scan; batch_size  One record. Regardless of the total data volume, the time taken for each batch of queries is nearly identical.

Why is the CQL query so fast?

  1. Direct Indexingid > last_id It allows for rapid定位 to the starting position in the index, eliminating the need to scan previous records.
  2. Sequential readingSince the data is stored in sorted order within the index, subsequent operations... ORDER BY id It performs sequential reading, which is highly efficient.
  3. Fixed scan amountScan only one batch at a time; batch_size A fixed number of data entries does not increase as the total data volume grows.

Crosstab query use cases

  • Export all table data: Such as generating sitemap, exporting CSV, backing up data, etc.
  • Batch processing tasks: Perform certain operations on each piece of data (such as AI generating articles, updating fields) without skipping access.
  • Data migration:Copy data from one table to another.
  • Real-time data stream processing: Continuously read new data from the database (similar to message queue).

Crosstab query restrictions

  • Must be based on a monotonically increasing primary key(or other sorting fields) to ensure order. If the primary key is not monotonically increasing, but there are timestamp fields in the business, you can also use a similar method, but you need to create a corresponding index.
  • No random page jumps: If you need to implement a user interface that "jumps to page N", cursor query is not suitable because  last_id  Must be determined by the previous page. You should still use  LIMIT offset, size, but you can consider optimizing it in your business (such as reducing the number of pages, using cache).
  • Pay attention to the stability of the sort field: If there are duplicate values ​​in the sorting field, you must ensure that the sorting is unique, otherwise data may be lost. Usually the primary key is unique and increasing, which is the safest.

Performance Comparison Test

When I was processing sitemap generation for 6 million song data, using traditional  OFFSET  Paging, it takes several hours to generate more than 100 XML files, and it becomes slower as time goes by. After switching to cursor query, the entire process was completed within a few seconds, and each batch of queries was stable at the millisecond level. The performance difference is amazing!

sum up

cursor query (WHERE id > last_id) is a simple but powerful optimization technique, especially suitable for data processing scenarios that require traversing the entire table. It ditches the bulky  OFFSET, utilizing the orderliness of the primary key index to achieve constant-time paging. In actual development, when you need to "process all data one by one", you might as well try this method, which may bring you unexpected performance improvements.

Tip: If other filter conditions are required in the query (such as  status = 1), must be in  (status, id) Create a composite index to ensure that queries can still be efficiently located. For example:

ALTER TABLE music_songs ADD INDEX idx_status_id (status, id);

Then, the query is modified to:

SELECT * FROM music_songs WHERE status = 1 AND id > last_id ORDER BY id LIMIT batch_size;

This approach not only enables filtering but also leverages index ordering, delivering outstanding performance.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

Browser caching increases the speed of secondary visits to your website

Browser caching increases the speed of secondary visits to your website Summary of pitfalls

Use browser caching of official words: If users will visit your website multiple times, browser caching of static resources can save users time. Cache headers should be applied to all cacheable static resources, not just to a small subset of static resources (for example, images). Cacheable resources include JS and CSS files, image files, and other binary object files (media files, PDF files, etc.). Normally, HTML doesn’t…
👁 202

Recommended reading

Responsive premium ceramic tableware website template – 0436

Responsive premium ceramic tableware website template – 0436 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the premium ceramic tableware industry. Its elegant and sophisticated design style effectively showcases ceramic tableware products, design concepts, brand stories, and spatial arrangements. It helps ceramic brands showcase their products online and attract both household customers and gift-giving clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 35
(Adaptive Mobile Version) Responsive Marketing-Driven Constant Temperature and Humidity Controller – Environmental Equipment Website PBootCMS Template; Blue Marketing-Driven Air Conditioning Equipment Website Source Code Download – 0468

(Adaptive Mobile Version) Responsive Marketing-Driven Constant Temperature and Humidity Controller – Environmental Equipment Website PBootCMS Template; Blue Marketing-Driven Air Conditioning Equipment Website Source Code Download – 0468 Practical Collection pbootcms Template

An premium, sophisticated, and technologically advanced PbootCMS website template designed for mobile devices. Featuring a cutting-edge design and powerful functionality—including three-level navigation, download and recruitment features—this template is ideal for large technology companies and conglomerates to showcase their brand image, product and service offerings, as well as their talent recruitment initiatives. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: Pb...
👁 40
Responsive digital photography equipment website template 1034

Responsive digital photography equipment website template 1034 Practical Collection Yiyou template

An eyouCMS responsive website template designed for the digital photography equipment industry. Its modern, tech-driven design is ideal for showcasing photography equipment products, digital devices, brand imagery, and technical specifications. This template helps photography equipment brands effectively present their products online and attract photography enthusiasts. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YouyouCMS...
👁 66
(Adaptive mobile version) Bilingual (Chinese/English) foreign trade website PBootCMS template; Automotive parts website source code download – 1031

(Adaptive mobile version) Bilingual (Chinese/English) foreign trade website PBootCMS template; Automotive parts website source code download – 1031 Practical Collection pbootcms Template

A bilingual (Chinese/English) PbootCMS website template designed for foreign trade and automotive parts businesses, compatible with both PC and WAP devices. Its professional, international design is ideal for automotive parts companies to showcase their products and brand identity. The multilingual support facilitates business expansion across global markets. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: Pb...
👁 56
Responsive hotel and vacation rental website template 0437

Responsive hotel and vacation rental website template 0437 Practical Collection Yiyou template

An EyouCMS responsive website template designed specifically for the hotel and vacation rental industry. Its warm and inviting design effectively showcases hotel environments, room amenities, vacation rental services, and online booking functionality. This template helps hotel and vacation rental businesses attract visitors online and enhance their brand awareness. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 56
(Adaptive Mobile Version) Responsive Bookkeeping and Financial Advisory Service PBootCMS Website Template – Download HTML5 Financial Accounting Website Source Code: 0469

(Adaptive Mobile Version) Responsive Bookkeeping and Financial Advisory Service PBootCMS Website Template – Download HTML5 Financial Accounting Website Source Code: 0469 Practical Collection pbootcms Template

This adaptive, responsive PbootCMS website template for construction machinery equipment, designed for mobile devices, utilizes HTML5 technology. Its eye-catching and professional design makes it ideal for showcasing products such as excavators and engineering vehicles. It enables construction machinery companies to showcase their products online and attract clients in the construction and mining industries. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s...
👁 42