SQL large data volume paging performance optimization

Currently, the web api read-only interface is being modified. In the process of modification, it is found that the response time after the modification is not very different from the previous one. The test result shows that the reason is found in the paging function of sql, and it is optimized to optimize the solution. as follows.

Test content

The running time comparison uses the user with the most platform fund records user_id 36062

The number of tests has not been 5 times. In order to avoid changing the starting value of the limit before each test in the index cache, the number of queries is 10 unchanged.

Average of 5 results The average query time before optimization is 0.287s

         After optimization sql1 average query time 0.012s

         After optimization, the average query time of sql2 is 0.016s

The results show that the query efficiency of sql1 and sql2 after optimization is significantly higher than that before optimization.

By first finding all the indexes that meet the conditions, and then retrieving the required data through the indexes, the efficiency is greatly improved.

Attached: sql

Original sql

SELECT  * FROM  account_log WHERE user_id  =  36062ORDER BY id DESC LIMIT 3300, 10;

Optimized sql 1

SELECT * FROM account_log a JOIN ( SELECTid FROM rd_account_log WHERE user_id = 36062 ORDER BY id DESC LIMIT 3300, 10) bON b.id = a.id;

Optimization has sql 2

SELECT * FROM account_log WHERE user_id =36062 AND id <=(SELECT id FROM rd_account_log

WHERE user_id = 36062 ORDER BY id DESC LIMIT33416,1) LIMIT 10

to sum up

1. As the start record increases, the time also increases, which shows that the paging statement limit has a lot to do with the start page number;

1) The query time of the limit statement is proportional to the position of the starting record.
2) The limit statement of mysql is very convenient, but it is not suitable for direct use for tables with a lot of records.

2. Use the covering index of the table to speed up the paging query.
We all know that if only the index column (covering index) is included in the statement that uses the index query, the query will be very fast in this case.

Because there is an optimized algorithm for index search, and the data is on the query index, there is no need to find the relevant data address, which saves a lot of time. In addition, there are related index caches in Mysql, and it is better to use the cache when the concurrency is high.



Guess you like

Origin blog.csdn.net/A___B___C/article/details/80592685