当前位置:网站首页>MySQL has no collation factor of order by
MySQL has no collation factor of order by
2022-07-02 07:18:00 【Software development heart】
Before experiencing the problems encountered in production , In my memory, I think MySQL Of SELECT In none order by Will press ID Sort returns ~
But it's not !!!
1. The following chapter is reproduced from : https://segmentfault.com/a/1190000016251056
I met one at work two days ago Mysql The question of sequencing , Without adding order by When , The order of acquired data is random , Instead of sorting by primary key . I used to mysql The default sorting of is by primary key . This is not the case .
①. Create a test database :
CREATE TABLE `test` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` char(100) DEFAULT NULL,
`age` char(5) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `age` (`age`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
②. Insert some data :
INSERT INTO test VALUES(NULL,' Zhang San ','5');
INSERT INTO test VALUES(NULL,' Li Si ','15');
INSERT INTO test VALUES(NULL,' Wang Wu ','5');
INSERT INTO test VALUES(NULL,' Zhao Xin ','15');
INSERT INTO test VALUES(NULL,' Dema ','20');
INSERT INTO test VALUES(NULL,' Prince ','5');
INSERT INTO test VALUES(NULL,' mywood ','17');
INSERT INTO test VALUES(NULL,' Hero ','22');
INSERT INTO test VALUES(NULL,' Water Margin ','18');
INSERT INTO test VALUES(NULL,' Xiaofang ','17');
INSERT INTO test VALUES(NULL,' Lao Wang ','5');
③. Inquire about 5 Bar record :select * from test limit 5
:
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | Zhang San | 5 |
| 2 | Zhang San | 5 |
| 3 | Li Si | 15 |
| 4 | Wang Wu | 5 |
| 5 | Zhao Xin | 15 |
+----+------+------+
5 rows in set (0.00 sec)
Now we only need to query two fields select id,age from test limit 5
:
+----+------+
| id | age |
+----+------+
| 3 | 15 |
| 5 | 15 |
| 8 | 17 |
| 11 | 17 |
| 10 | 18 |
+----+------+
5 rows in set (0.00 sec)
Of two query statements Explain Implementation plan :
mysql> explain select * from test limit 5 \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 12
Extra:
1 row in set (0.00 sec)
mysql> explain select id,age from test limit 5 \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test
type: index
possible_keys: NULL
key: age
key_len: 16
ref: NULL
rows: 12
Extra: Using index
1 row in set (0.00 sec)
Conclusion :
MySQL Not given order by conditional , The order of the data results obtained is related to the query column . Because in different query columns , Different index conditions may be used .Mysql When using different indexes , The order of data obtained is different . This may be the same as Mysql Indexing mechanism , And the use of indexes . To avoid that , In future projects , Remember to add order by
2. seek truth
Refer to some personal and official articles , Roughly understand that in none order by Under the circumstances , influence MySQL The factors of sorting rules are :
- SELECT Columns to query
- Index method
- physical position , That is, record the sequence of insertion rowid( Not ID The order of )
- Storage engine ,InnoDB、MyISM
[email protected] Of opinion :
https://stackoverflow.com/questions/8746519/sql-what-is-the-default-order-by-of-queries
MySQL Official website answer :
边栏推荐
- Sqli-labs customs clearance (less15-less17)
- Take you to master the formatter of visual studio code
- mapreduce概念和案例(尚硅谷学习笔记)
- Two table Association of pyspark in idea2020 (field names are the same)
- Proteus -- RS-232 dual computer communication
- Alpha Beta Pruning in Adversarial Search
- oracle-外币记账时总账余额表gl_balance变化(上)
- RMAN incremental recovery example (1) - without unbacked archive logs
- parser.parse_args 布尔值类型将False解析为True
- CSRF攻击
猜你喜欢
在php的开发环境中如何调取WebService?
使用MAME32K进行联机游戏
SQLI-LABS通关(less2-less5)
UEditor . Net version arbitrary file upload vulnerability recurrence
SQL injection closure judgment
ssm超市订单管理系统
Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK
Check log4j problems using stain analysis
Yaml file of ingress controller 0.47.0
Sqli labs customs clearance summary-page4
随机推荐
图解Kubernetes中的etcd的访问
MapReduce concepts and cases (Shang Silicon Valley Learning Notes)
【信息检索导论】第六章 词项权重及向量空间模型
Sqli labs customs clearance summary-page2
2021-07-19c CAD secondary development creates multiple line segments
Oracle apex 21.2 installation and one click deployment
oracle-外币记账时总账余额表gl_balance变化(上)
oracle EBS标准表的后缀解释说明
如何高效开发一款微信小程序
2021-07-19C#CAD二次开发创建多线段
TCP攻击
CAD secondary development object
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory'
中年人的认知科普
Oracle 11.2.0.3 handles the problem of continuous growth of sysaux table space without downtime
使用Matlab实现:弦截法、二分法、CG法,求零点、解方程
Agile development of software development pattern (scrum)
【信息检索导论】第三章 容错式检索
搭建frp进行内网穿透
Principle analysis of spark