Programmer, you know the Limit MySQL has performance problems?

MySQL is usually achieved by paging query limit.

limit basic MySQL usage is very simple. receiving one or two limit integer argument, if two parameters, the first offset is to specify a first return rows, the second is the maximum number of rows returned. Offset 0 is an initial record line.

For compatibility with PostgreSQL, limit also supports the limit # offset #.

problem:

For small offset to directly limit the query is no problem, but with the increasing amount of data, the next page, the offset limit statements will be greater, the speed will be significantly slower.

Optimization idea:

Scanning avoid excessive amount of data recorded

solve:

Pagination subquery or JOIN way paging.

JOIN paging and paging efficiency sub-query on a basic level, the consumption of time are basically the same.

Here's an example. MySQL is generally self-energizing primary key of numeric type, this case can be optimized in the following manner.

The following table with a real production environment of 800,000 data, for example, compare the before and after consuming query optimization:

Programmer, you know the Limit MySQL has performance problems?

We can see optimized performance increased nearly 20-fold.

Optimization principle:

Sub-query is done on the index, while a general inquiry done on the data file, generally speaking, the index file is much smaller than the data file, so the operation will be more efficient. To remove all the fields as content, large amounts of data need to cross the first block and removed, while the second substantially directly positioned according to the index field, only fetches the corresponding content through, greatly enhance the efficiency of natural.

Therefore, to optimize the limit, the limit is not used directly, but first get to id offset, and then used directly limit size to obtain the data.

In actual use project, you can use a similar strategy to deal with the way paging mode, for example, page 100 data to determine if it is less than 100 pages, on the use of the most basic way paging, greater than 100, use pagination sub-queries.

 

Guess you like

Origin www.cnblogs.com/CQqf2019/p/11075527.html