MySQL order by sorting the results are incorrect

Create a test table:

CREATE TABLE `tb1` (
 
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
 
  `a` decimal(19,2) NOT NULL,
 
  `acid` bigint(20) NOT NULL,
 
  `prid` bigint(20) NOT NULL,
 
  PRIMARY KEY (`id`),
 
  KEY `idx_prid` (`prid`),
 
  KEY `idx_acid` (`acid`)
 
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

A field not indexed, insert the test data:

INSERT INTO `tb1` (`id`, `a`, `acid`, `prid`) 
VALUES (1,2.00,3,2),(2,3.00,3,2),(3,4.00,2,3),(4,5.00,2,3),(5,6.00,2,3),(6,8.00,2,3),(7,10.00,2,3),(8,12.00,2,3),(9,16.00,2,3),(10,20.00,2,3),(11,6.00,2,4),(12,8.00,2,4),(13,10.00,2,4),(14,12.00,2,4),(15,5.00,2,2),(16,6.00,2,2);

View table data:

([yoon]> select * from tb1;
+----+-------+------+------+
| id | a     | acid | prid |
+----+-------+------+------+
|  1 |  2.00 |    3 |    2 |
|  2 |  3.00 |    3 |    2 |
|  3 |  4.00 |    2 |    3 |
|  4 |  5.00 |    2 |    3 |
|  5 |  6.00 |    2 |    3 |
|  6 |  8.00 |    2 |    3 |
|  7 | 10.00 |    2 |    3 |
|  8 | 12.00 |    2 |    3 |
|  9 | 16.00 |    2 |    3 |
| 10 | 20.00 |    2 |    3 |
| 11 |  6.00 |    2 |    4 |
| 12 |  8.00 |    2 |    4 |
| 13 | 10.00 |    2 |    4 |
| 14 | 12.00 |    2 |    4 |
| 15 |  5.00 |    2 |    2 |
| 16 |  6.00 |    2 |    2 |
+----+-------+------+------+

The non-indexed fields and field data is repeated for a sort order by:

([yoon]> select * from tb1 order by a desc ;
+----+-------+------+------+
| id | a     | acid | prid |
+----+-------+------+------+
| 10 | 20.00 |    2 |    3 |
|  9 | 16.00 |    2 |    3 |
| 14 | 12.00 |    2 |    4 |
|  8 | 12.00 |    2 |    3 |
| 13 | 10.00 |    2 |    4 |
|  7 | 10.00 |    2 |    3 |
| 12 |  8.00 |    2 |    4 |
|  6 |  8.00 |    2 |    3 |
| 11 |  6.00 |    2 |    4 |
| 16 |  6.00 |    2 |    2 |
|  5 |  6.00 |    2 |    3 |
|  4 |  5.00 |    2 |    3 |
| 15 |  5.00 |    2 |    2 |
|  3 |  4.00 |    2 |    3 |
|  2 |  3.00 |    3 |    2 |
|  1 |  2.00 |    3 |    2 |
+----+-------+------+------+

Used with the order by and limit:

([yoon]> select * from tb1 order by a desc limit 4;
+----+-------+------+------+
| id | a     | acid | prid |
+----+-------+------+------+
| 10 | 20.00 |    2 |    3 |
|  9 | 16.00 |    2 |    3 |
| 14 | 12.00 |    2 |    4 |
|  8 | 12.00 |    2 |    3 |
+----+-------+------+------+

Adding an index to a field:

([yoon]> alter table tb1 add index idx_a(a);
Query OK, 0 rows affected (0.05 sec)
([yoon]> select * from tb1 order by a desc limit 4;
+----+-------+------+------+
| id | a     | acid | prid |
+----+-------+------+------+
| 10 | 20.00 |    2 |    3 |
|  9 | 16.00 |    2 |    3 |
| 14 | 12.00 |    2 |    4 |
|  8 | 12.00 |    2 |    3 |
+----+-------+------+------+

When ordering add a field id:

([yoon]> select * from tb1 order by a desc,id desc limit 4;
+----+-------+------+------+
| id | a     | acid | prid |
+----+-------+------+------+
| 10 | 20.00 |    2 |    3 |
|  9 | 16.00 |    2 |    3 |
| 14 | 12.00 |    2 |    4 |
|  8 | 12.00 |    2 |    3 |
+----+-------+------+------+

For a non-unique field, regardless of whether it contains an index, the result set is uncertain. If the business logic or order by paging result sets have relatively high stringent requirements, please remember to use a unique key sort.

Guess you like

Origin www.cnblogs.com/hankyoon/p/12614581.html