Mysql-Which of the following is more efficient for querying pages using the array_slice array of php and using limit?

Source: Internet
Author: User
Previously, we used limit for paging. Now we found that array_slice paging is quite useful, that is, we don't know if the efficiency will be too low. Previously, we used limit for paging. Now we found that array_slice paging is quite useful, I don't know if the efficiency will be too low.

Reply content:

Previously, I used limit for paging. Now I found that array_slice paging is quite useful, that is, I don't know if the efficiency will be too low.

SQL: select * from Table limit m offset n.

This is a normal Paging Business Requirement.

Execute the SQL statement on a table with a count:
Select * from Table limit

Execution time: 25 s

Problem:
In MySQL, the processing method of limit offset is to retrieve all data of OFFSET + LIMIT, and then remove OFFSET, and return the LIMIT at the bottom.
In this way, when the offset is very high, such as: limit 100020, 20, the system will query 100000, and then throw the first, which is a very costly operation, slow query.

How to optimize:

  1. Replacing with id> m limit n is much faster than using limit m and n because only n records are queried with the primary key index. this method is suitable for data loading, but not necessarily for front-end paging scenarios,
    Because the ID may not be consecutive, it is not applicable in the flip page of the elevator mode, but the flip page of the escalator mode is used.
    Select * from table where id> 1000000 limit 5
    Run: 0.013 s

Another simple optimization method is to use covering index to query and then perform the join operation with the entire row. In this way, you can directly use index to obtain data, instead of querying the table. After finding the required data, join the entire table to obtain other columns.
For example:
Select * from Table inner join (select id from Table limit 00,5) as lim on table. id = lim. id

Execution time: 0.211 s

Array_slice paging? Are you sure you want to retrieve all the data on the server page instead of querying the data on a certain page in the database?

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.