当前位置:网站首页>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官网答案:
边栏推荐
- JS divides an array into groups of three
- Redis -- cache breakdown, penetration, avalanche
- Explanation and application of annotation and reflection
- CVE-2015-1635(MS15-034 )远程代码执行漏洞复现
- Sentry construction and use
- SQLI-LABS通关(less15-less17)
- oracle-外币记账时总账余额表gl_balance变化(上)
- Cve - 2015 - 1635 (ms15 - 034) réplication de la vulnérabilité d'exécution de code à distance
- Flex Jiugongge layout
- oracle apex ajax process + dy 校验
猜你喜欢
CAD二次开发 对象
sqli-labs通關匯總-page2
CVE-2015-1635(MS15-034 )遠程代碼執行漏洞複現
ORACLE EBS 和 APEX 集成登录及原理分析
Flex Jiugongge layout
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
In depth study of JVM bottom layer (IV): class file structure
Wechat applet Foundation
CSRF攻击
Sqli labs customs clearance summary-page4
随机推荐
ORACLE APEX 21.2安装及一键部署
CRP实施方法论
工具种草福利帖
Sqli-labs customs clearance (less15-less17)
php中删除指定文件夹下的内容
ORACLE EBS接口开发-json格式数据快捷生成
ORACLE EBS DATAGUARD 搭建
pySpark构建临时表报错
User login function: simple but difficult
php中树形结构转数组(拉平树结构,保留上下级排序)
Ingress Controller 0.47.0的Yaml文件
Ceaspectuss shipping company shipping artificial intelligence products, anytime, anywhere container inspection and reporting to achieve cloud yard, shipping company intelligent digital container contr
Overload global and member new/delete
PXC high availability cluster summary
2021-07-19c CAD secondary development creates multiple line segments
JS delete the last bit of the string
2021-07-17C#/CAD二次开发创建圆(5)
TCP攻击
Oracle rman半自动恢复脚本-restore阶段
SQLI-LABS通关(less1)