Do not use Order By and Limit together in MySQL

Remember! Do not use ORDER BY and LIMIT together in MySQL, there is a big pit...

Do not use Order By and Limit together in MySQL Do not use Order By and Limit together in MySQL

Phenomenon and problems

After ORDER BY sorting, use LIMIT to fetch the first few items, and found that the order of the returned result set is not the same as expected.

Here are the problems I encountered:

Do not use Order By and Limit together in MySQL Do not use Order By and Limit together in MySQL

Do not use Order By and Limit together in MySQL Do not use Order By and Limit together in MySQL

Do not use Order By and Limit together in MySQL Do not use Order By and Limit together in MySQL

Do not use Order By and Limit together in MySQL Do not use Order By and Limit together in MySQL

As you can see, the results with LIMIT and without LIMIT are different from what I expected, and "very incredible", which is really puzzling.

Later on Baidu, if the columns of order by have the same value, MySQL will randomly select these rows. In order to ensure that the return order is consistent every time, an additional sorting field (such as id) can be added, using two fields to do it. May reduce the probability of repetition.

So, change to order by status, id:

Do not use Order By and Limit together in MySQL Do not use Order By and Limit together in MySQL

Although the problem is solved, let's take a look at what the official documentation says!

LIMIT query optimization

Do not use Order By and Limit together in MySQL Do not use Order By and Limit together in MySQL

If you only need a specified number of rows in the result set, use the LIMIT clause in the query instead of grabbing the entire result set and discarding the remaining data that you don’t want.

MySQL sometimes optimizes a query that contains a LIMIT clause and does not have a HAVING clause:

① MySQL usually prefers to perform a full table scan, but if you use LIMIT to query only a few rows of records, MySQL may use indexes in some cases.

② If you combine the LIMIT row_count clause with the ORDER BY clause, MySQL will stop sorting immediately after finding the first row_count row of the sorted result, instead of sorting the entire result. If you use the index to complete the sorting, it will be very fast.

If you must perform file sorting, before finding the first row_count row, select all rows that match the query but do not include the LIMIT clause, and sort most or all of them.

Once the first row_count is found, MySQL will not sort any remaining parts of the result set.

One manifestation of this behavior is that an ORDER BY query with or without LIMIT may return rows in a different order.

③ If LIMIT row_count is used with DISTINCT, once the only row of row_count is found, MySQL will stop.

④LIMIT 0 can quickly return an empty result set, which is a very useful method to detect whether a query is valid.

⑤ If the server uses a temporary table to parse the query, it will use the LIMIT row_count clause to calculate how much space is needed.

⑥ If ORDER BY does not take the index, and also comes with LIMIT, then the optimizer may be able to avoid using a merged file, and use the in-memory filesort operation to sort the rows in the memory.

⑦ If there are multiple rows in the ORDER BY column with the same value, the server is free to return these rows in any order, and may return in different ways according to the overall execution plan. In other words, the sort order of these rows is uncertain for unsequenced.

One factor that affects the execution plan is LIMIT, so for an ORDER BY query, the order of rows returned with and without LIMIT may be different.

Look at the following example:

Do not use Order By and Limit together in MySQL Do not use Order By and Limit together in MySQL

Including LIMIT may affect the order of each category row. E.g:

Do not use Order By and Limit together in MySQL Do not use Order By and Limit together in MySQL

If you need to ensure that the return is in the same order with or without LIMIT, then you can include additional columns in the ORDER BY to make the order deterministic. E.g:

Do not use Order By and Limit together in MySQL Do not use Order By and Limit together in MySQL

summary

If you only need a few rows in the result set, then limit is recommended. In this way, you can avoid grabbing the entire result set, and then discard those rows that you don't want.

For an order by query, the order of rows returned with or without limit may be different.

If limit row_count and order by are used together, the sorting stops when the first row_count is found and returns directly.

If the order by column has the same value, then MySQL is free to return these rows in any order. In other words, as long as the value of the order by column is not repeated, the return order can be guaranteed.

You can include additional columns in the order by clause to make the order deterministic.

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/114576355