[Tips] Batch fetch database data

Source: Internet
Author: User

In some business scenarios you need to take all the data in a database table for processing, the easiest way is to use the paging query (lazy MySQL for example):

SELECT * FROM DataTable limit Offset,amount

There is a problem here, as the value of the offset is getting bigger and larger, the table data to be scanned by this SQL will be more and more, because to locate the offset line, you need to scan all rows smaller than offset. Obviously in a large data table, to do so, performance will be problematic. To avoid such a situation, we naturally think of using an index to solve it, such as using an automatic sequence ID for batch fetching:

SELECT * from DataTable where ID >= start and Id<start+batchcount

This allows us to index the ID, and then batch to fetch, obviously the effect will be much higher, but if the self-added sequence because the deletion and other operations become not continuous, there will be empty execution and multiple execution occurs. To resolve this approach, we need to work with the advantages of indexing and paging:

SELECT * from DataTable where ID >= start limit batchcount

Then we calculate the starting ID value each time we retrieve it, and then we take a batch of data. This can avoid the first not to go to the index, query performance problems, and solve the second ID discontinuity, resulting in the return of data volume instability caused by the problem of waste.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.