当前位置:网站首页>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官网答案:
边栏推荐
- Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
- 解决微信小程序swiper组件bindchange事件抖动问题
- IDEA2020中测试PySpark的运行出错
- Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
- JSP智能小区物业管理系统
- Sqli-labs customs clearance (less1)
- SQLI-LABS通關(less6-less14)
- ORACLE 11G SYSAUX表空间满处理及move和shrink区别
- RMAN增量恢复示例(1)-不带未备份的归档日志
- Eslint configuration code auto format
猜你喜欢
随机推荐
ORACLE 11.2.0.3 不停机处理SYSAUX表空间一直增长问题
In depth study of JVM bottom layer (IV): class file structure
Laravel8中的find_in_set、upsert的使用方法
Ingress Controller 0.47.0的Yaml文件
php中在二维数组中根据值返回对应的键值
JS to determine whether there is a value in the object in the array
Oracle RMAN semi automatic recovery script restore phase
图解Kubernetes中的etcd的访问
php中获取汉字拼音大写首字母
MySQL中的正则表达式
ORACLE APEX 21.2安装及一键部署
ORACLE 11G SYSAUX表空间满处理及move和shrink区别
php中计算树状结构数据中的合计
JS countdown case
工具种草福利帖
Oracle EBS数据库监控-Zabbix+zabbix-agent2+orabbix
Cve-2015-1635 (ms15-034) Remote Code Execution Vulnerability recurrence
Overload global and member new/delete
SQLI-LABS通关(less18-less20)
js删除字符串的最后一位









