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:
-
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?