Optimize MySQL limit offset

Source: Internet
Author: User

Limit and offset are usually used together with order. Indexes are helpful for sorting and comparison,

If no index is available, a large number of files are sorted. Therefore, you must add an index to the order by column.

If:

Select * from mytable order by sp_id limit 100000,10

When similar SQL statements appear, the operation costs a lot and will scan 100010 rows of data.

This can be changed

Select * from mytable

Inner join (

Select pid from mytable order by sp_id limit 100000,10

) As lim using (pid );

Note: sp_id creates a BTREE index.

The server can check as little data as possible on the index. Once the required rows are obtained, they are connected to the complete table and the remaining columns are obtained.

The SQL statement execution time before modification is 2.10 seconds, and the modified SQL statement execution time is 1.75 seconds. Saves 0.35 seconds.

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.