MySql paging query slow solution

background

We use the pagination is inevitable in the process of development, normally our practice is to use limit plus an offset:
select * from table where column=xxx order by xxx limit 1,20.
When the amount of data is small (less than 1 million), no matter where you turn the page, the performance is very fast. If the query is slow, as long as
indexed and conditions on where and order by columns can be resolved. However, when the amount of data when (situation encountered in small series
is 500 million data), if the turn to the last few pages, even though the addition of an index, the query is very slow, which is what causes it? How do we solve it?

Paging principle limit

When we turn to the last few pages, sql query usually: select * from table where column=xxx order by xxx limit 1000000,20.
Query very slow. But we see the first few pages, when speed is not slow. This is because the limit of the offset caused by too much.
Principle when using a limit MySql (for example using the above example):

  1. MySql to check out 1,000,020 records.
  2. Then give it in front of 1,000,000 records.
  3. Return the remaining 20 records.

The above-described process is "high performance MySql" book confirmed.

solution

The solution is to try to use an index scan coverage that we select is detected behind the index column, but not
all of the columns, and this is the best index of the column id. Then do it again associated with the query returns all the columns.
Sql above embodiment can be written as follows, finally, not optimized sql, 2s multiple execution time, execution time optimized sql 0.3 seconds more

SELECT
    *
FROM
    table t
INNER JOIN (
    SELECT
        id
    FROM
        table
    WHERE
        xxx_id = 143381
    LIMIT 800000,20
) t1 ON t.id = t1.id

Guess you like

Origin www.cnblogs.com/zhangliwei/p/12056769.html