mysql paging query optimization

 1. Slow query

Suppose we have an order table, a paging query statement is like this

SELECT * FROM table ORDER BY id LIMIT 1000, 10; 

But when the order table data reaches a million level, then writing like this will slow death

SELECT * FROM table ORDER BY id LIMIT 1000000, 10; 

The above statement may take tens of seconds.

We see that as the number of records increases, the query time of the limit statement increases proportionally to the position of the starting record.

Because LIMIT 100000, 10 means to scan 100010 lines that meet the conditions, throw away the first 100000 lines, and return to the last 10 lines, the problem is here. This paging query method starts scanning from the first record in the database every time, so the further back, the slower the query speed.

And our solution, we can start from the largest record that has been found last time.

2. Use subquery optimization

First locate the id of the offset position, and then query it later. This method is suitable for the case where the id is incremented:

select * from order  limit 100000,1;

select id from order limit 100000,1;

select * from order where id>=(select id from orders limit 100000,1) limit 100

The query time for the 3 statements is as follows:

  • 1st statement: 3674ms
  • 2nd statement: 1315ms
  • 3rd statement: 1327ms

Note for the above query:

  • Comparing statement 1 and 2: using select id instead of select * is 3x faster
  • Comparing the 2nd statement and the 3rd statement: the speed differs by tens of milliseconds

Look at the following query

select * from order where id between 1000000 and 1000100 limit 100

Query time: 12ms

This query method can greatly optimize the query speed and can basically be completed within tens of milliseconds.

There is another way of writing

select * from orders_history where id >= 1000001 limit 100;

Of course, you can also use the in method to query, this method is often used to query when multiple tables are associated, and use the id collection of other table queries to query:

select * from order where id in
(select order_id from goods where goodName = 'pen')
limit 100;

 

Original link: https://www.cnblogs.com/youyoui/p/7851007.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324619294&siteId=291194637