MySQL order by limit paging data duplication or loss problem description

Let’s talk about the solution first:

Use unique values ​​(such as primary key id) in sorting to ensure that each piece of data is not repeated

question

MySQL official description: IfMultiple rows with the same value in a column ORDER BY, the server is free to return the rows in any order, and may be returned differently depending on the overall execution plan. In other words, the sort order of these rows relative to the unsorted columns is undefined.
One factor that affects the execution plan is LIMIT, so queries with ORDER BY and without LIMIT may return rows in a different order.

That is to say, if the field values ​​​​of our order by are the same, then the possible order of data returned in each query is different. If used together with limit, the problem of duplication of paging data may occur.

solution

official

Description: Use values ​​with a deterministic order, such as primary key id

Insert image description here
Read the official documents to find the answer
Official documents: MySQL 5.7 Reference Manual LIMIT Query Optimization
I checked online and found that version 5.6 also has the same problem.

Guess you like

Origin blog.csdn.net/csl12919/article/details/132895852