当前位置:网站首页>MySQL无order by的排序规则因素
MySQL无order by的排序规则因素
2022-07-02 06:24:00 【软件开发随心记】
在没历经生产上遇到的问题前,记忆中认为MySQL的SELECT在无 order by的情况下会按ID排序返回~
然而并不是这样的!!!
1.下面篇章转载自: https://segmentfault.com/a/1190000016251056
前两天在工作中遇到一个Mysql排序的问题,在没有加order by的时候,获取的数据顺序是随机的,而不是按照主键排序的。以往我都以往mysql的排序默认是按主键来排序的。这才发现其实不是这样的。
①. 创建一个测试数据库:
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 INTO test VALUES(NULL,'张三','5');
INSERT INTO test VALUES(NULL,'李四','15');
INSERT INTO test VALUES(NULL,'王五','5');
INSERT INTO test VALUES(NULL,'赵信','15');
INSERT INTO test VALUES(NULL,'德玛','20');
INSERT INTO test VALUES(NULL,'皇子','5');
INSERT INTO test VALUES(NULL,'木木','17');
INSERT INTO test VALUES(NULL,'好汉','22');
INSERT INTO test VALUES(NULL,'水浒','18');
INSERT INTO test VALUES(NULL,'小芳','17');
INSERT INTO test VALUES(NULL,'老王','5');
③. 查询5条记录:select * from test limit 5:
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | 张三 | 5 |
| 2 | 张三 | 5 |
| 3 | 李四 | 15 |
| 4 | 王五 | 5 |
| 5 | 赵信 | 15 |
+----+------+------+
5 rows in set (0.00 sec)
现在我们只查询两个字段 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)
两条查询语句的Explain执行计划:
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)
结论:
MySQL在不给定order by条件的时候,得到的数据结果的顺序是跟查询列有关的。因为在不同的查询列的时候,可能会使用到不同的索引条件。Mysql在使用不同索引的时候,得到的数据顺序是不一样的。这个可能就跟Mysql的索引建立机制,以及索引的使用有关了。为了避免这种情况,在以后的项目中,切记要加上 order by
2.寻求真理
参考了部分个人以及官方文章,大致了解到在无order by的情况下,影响MySQL排序规则的因素有:
- SELECT查询的列
- 索引方式
- 物理位置,即记录插入的顺序rowid(非ID的顺序)
- 存储引擎,InnoDB、MyISM
[email protected]的见解:
https://stackoverflow.com/questions/8746519/sql-what-is-the-default-order-by-of-queries

MySQL官网答案:
边栏推荐
- Underlying mechanism mvcc
- Code execution sequence with and without resolve in promise
- SQLI-LABS通关(less15-less17)
- ORACLE 11G利用 ORDS+pljson来实现json_table 效果
- php中删除指定文件夹下的内容
- php中判断版本号是否连续
- IDEA2020中PySpark的两表关联(字段名相同)
- JS judge whether the object is empty
- Atcoder beginer contest 253 F - operations on a matrix / / tree array
- Differences between ts and JS
猜你喜欢

SQL injection closure judgment

Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK

flex九宫格布局

sqli-labs通关汇总-page3

Network security -- intrusion detection of emergency response

The use of regular expressions in JS

2021-07-05c /cad secondary development create arc (4)

外币记账及重估总账余额表变化(下)

PHP Session原理简析

Principle analysis of spark
随机推荐
Pyspark build temporary report error
Underlying mechanism mvcc
PM2 simple use and daemon
数仓模型事实表模型设计
js判断对象是否为空
Oracle EBS数据库监控-Zabbix+zabbix-agent2+orabbix
Use of interrupt()
Solve the problem of bindchange event jitter of swiper component of wechat applet
php中获取汉字拼音大写首字母
Stress test modification solution
Sentry construction and use
工具种草福利帖
ORACLE APEX 21.2安裝及一鍵部署
CVE-2015-1635(MS15-034 )遠程代碼執行漏洞複現
Oracle 11.2.0.3 handles the problem of continuous growth of sysaux table space without downtime
Oracle段顾问、怎么处理行链接行迁移、降低高水位
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
[leetcode question brushing day 35] 1060 Missing element in ordered array, 1901 Find the peak element, 1380 Lucky number in matrix
图解Kubernetes中的etcd的访问
Sqli-labs customs clearance (less15-less17)