[MySQL] The default sorting is not necessarily the primary key sorting [Original]

Overview

There was a bug recently. Some people reported that the order of approval records of a certain user is disordered, and only this user has it, and other users do not.


reason

Looking at the code, it is found that it is obtained through ORM, which is equivalent to SQL:

select id,credit_audit_id, admin_name,rejection, create_time from credit_audit_record where credit_audit_id = '56556' and alias = 'C1000100' and save_status = 0;


SQL does not add order by to sort. In general, the primary key is used to sort in positive order. Take a look at the table structure:

> desc credit_audit_record;
+--------------------+---------------------+------+-----+---------------------+----------------+
|       FIELD        |        TYPE         | NULL | KEY |       DEFAULT       |     EXTRA      |
+--------------------+---------------------+------+-----+---------------------+----------------+
| id                 | bigint(20) unsigned | NO   | PRI | NULL                | auto_increment |
| credit_audit_id    | int(10)             | NO   | MUL | NULL                |                |
| admin_id           | varchar(20)         | NO   |     |                     |                |
| admin_name         | varchar(30)         | NO   |     |                     |                |

As you can see, id is an auto-incrementing primary key


Let's use sql directly to view the results:

> select id,credit_audit_id, admin_name,rejection,create_time from credit_audit_record where credit_audit_id = '56556' and alias = 'C1000100' and save_status = 0;
+--------+-----------------+------------+-----------+---------------------+
|   ID   | CREDIT AUDIT ID | ADMIN NAME | REJECTION |     CREATE TIME     |
+--------+-----------------+------------+-----------+---------------------+
| 109962 |           56556 |            |           | 2022-01-02 17:00:01 |
| 112294 |           56556 | aaa     |           | 2022-01-10 14:42:17 |
| 112653 |           56556 | bbb     |           | 2022-01-12 10:46:04 |
| 109963 |           56556 | ccc       |           | 2022-01-02 17:03:02 |
+--------+-----------------+------------+-----------+---------------------+

The result is unexpected, without adding order by, when selecting, it is not sorted in positive order according to id


I always thought that if order by is not added, the default is to sort by id in positive order, but the reality may not be.


Check out the related documentation:

https://segmentfault.com/a/1190000016251056

https://blog.csdn.net/qq_35383263/article/details/81433693

https://www.imooc.com/article/4925


reasons may be:

The default sorting of MySQL SELECT is displayed in the order of physical storage. (without additional sorting) That is, SELECT * FROM produces a "table scan". If the table is not added, deleted, or modified, the records will be displayed in the order of insertion. That's why the initial looks like an increment.

Then, when we add, delete, and change, it is not difficult to find that the order will be out of order, and this is the problem. Additions, deletions and modifications are idiomatic functions, so attention must be paid to this detail. When we do not perform ORDER BY to specify the ordering, MySQL will display the data in the fastest form (physical storage order), resulting in out of order.


The conclusion is:

If the order by sort condition is not specified in the sql statement, the sort order of the result set is related to the query column. Because different query columns may use different indexes, resulting in different orders, the specific reasons will not be discussed in depth.


Looking at the online great god's statement is: In the absence of conditions, the default sorting order of the database is not easy to determine, and it should not be determined by any factors. Different databases have different implementations. It can only be limited by order by.


Anyway, don't be superstitious that mysql will sort in the order you think by default. If you want to sort, first create an index for the field you want to sort (to improve efficiency), and then order by this field to sort.


In a word:

In the absence of conditions, the database default sort order is indeterminate


solve

The solution is very simple, if you need to sort, be sure to add an order by

Guess you like

Origin blog.csdn.net/jiandanokok/article/details/122504344