Experience on SQL optimization with excessive offset during mysql paging, mysqloffset

Source: Internet
Author: User

Experience on SQL optimization with excessive offset during mysql paging, mysqloffset

Problems Found

When we display the content in a list, we will inevitably encounter paging problems, because the number of content in the list may be large, but the page size that the user can see at a time is limited, it is impossible for a single interface to display all the content. Retrieving too much data from the backend at a time also puts extra pressure on the backend.

This statement is usually used for paging query:

SELECT*FROM tablewhere condition1 = 0and condition2 = 0and condition3 = -1and condition4 = -1order by id ascLIMIT 2000 OFFSET 50000

When the offset is very large, the execution efficiency of this statement will be significantly reduced, and the efficiency will decrease with the increase of offset.

The reason is:

MySQL does not skip the offset row, but takes the offset + N rows, and then returns the first offset row, and returns N rows. When the offset is particularly large and then the size of a single row is large, the more data you need to obtain each query, the slower it will naturally be.

Optimization solution:

SELECT*FROM tableJOIN(select id from tablewhere condition1 = 0and condition2 = 0and condition3 = -1and condition4 = -1order by id ascLIMIT 2000 OFFSET 50000)as tmp using(id)

Or

SELECT a.* FROM table a, (select id from tablewhere condition1 = 0and condition2 = 0and condition3 = -1and condition4 = -1order by id ascLIMIT 2000 OFFSET 50000) b where a.id = b.id

First, the primary key list is obtained, and then the target data is queried through the primary key. Even if the offset is large, many primary keys are obtained, instead of all field data, which improves the efficiency.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Related Article

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.