当前位置:网站首页>How does MySQL sort by default when using the select statement without order by?
How does MySQL sort by default when using the select statement without order by?
2022-04-22 13:12:00 【Fei Yun a】
Hello everyone , I'm Fei Yun
Fear of the infinite truth , There is further joy in the near future
Record a MySQL Query sorting problem , One SQL Statement not added order by, Then, according to what rules are the query results sorted ? Inquired some information on the Internet , Share below :
•MyISAM surface
MySQL Select The default sort is displayed in physical storage order ( No additional sorting ). in other words SELECT * FROM tbl – Will produce “ Table scan ”. If the table is not deleted 、 Replace 、 update operation , The records are displayed in the order of insertion .
•InnoDB surface
Same thing , Will be arranged in the order of the primary key , It needs to be emphasized again , It's just a hidden rule , In fact, it may not be completely reliable .
Reference resources :MySQL There are also hidden rules – Select Sentence without order by How to sort ?[1]
If there is no definition order by:
The returned data is not necessarily sorted by primary key , The results can be returned in any order - It may also change over time .
There is no... In the relational database “ Natural order ” Or something like that ( At least as far as I know ). The only way to get an explicit sort is to specify a reliable sort order by Clause , source when-no-order-by-is-specified-what-order-does-a-query-choose-for-your-record[2].
For the same batch of data , At some point the order is the same , Over time , The data will change , So when making a query ,MySQL Will try to do it as fast as possible (MySQL The practical method is not necessarily fast ) Return the data .
Due to access to primary key 、 Indexing is faster in most cases ( stay Cache in ) Therefore, the returned data may be based on the primary key 、 The sequential output of the index , It's not really sorted here , This is mainly due to the primary key 、 The index itself is sorted into memory , So continuous output may be some kind of sequence . In some cases, the data that consumes the shortest seek time of the hard disk will be returned first . If you only query a single table , It is regular under special circumstances .
The following content analysis source :what-is-the-default-order-of-records-for-a-select-statement-in-mysql[3]
MySQL in SELECT What is the default recording order of statements ?

There is an answer with high recognition and praise :

Read the answer roughly , Republish a previously answered SQL Server Type of question .
stay SQL In the world , Order is not an inherent property of a set of data . therefore , Unless you use order by Clause to query your data , Otherwise you can't get from RDBMS Ensure that your data will be returned in a specific order - Even return... In a consistent order .
Then answer your question :
•MySQL Sort records as needed , But there is no guarantee of consistency • If you intend to rely on this order for any operation , Must be used order by Specify the order you want . Otherwise, doing anything else is preparing for an unpopular accident .
This is all. SQL Properties of , Not just MySQL.SQL-92 The relevant text in the specification is :
http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
If not specified <order by Clause >, be Q The order of rows depends on the underlying implementation .
The content of this article is the above contents , In summary : If not specified in use order by, So it basically depends on the underlying implementation , The specific sorting rules are uncertain , So the order of sorting is not fixed , May change over time .
In practice , If there is a query list to show the functions and requirements of data , Before development, we must first determine the rules of data sorting , This can avoid the problem of different sorting results of subsequent data queries .
If the MySQL SELECT The default recording order of statements is for those who know or have research partners , Welcome to leave a message to share your understanding .
If the article helps you , Please support me , Thank you for your generosity ~
References
[1] MySQL There are also hidden rules – Select Sentence without order by How to sort ?: https://www.cnblogs.com/lushilin/p/6094373.html[2] when-no-order-by-is-specified-what-order-does-a-query-choose-for-your-record: https://stackoverflow.com/questions/20050341/when-no-order-by-is-specified-what-order-does-a-query-choose-for-your-record[3] what-is-the-default-order-of-records-for-a-select-statement-in-mysql: https://dba.stackexchange.com/questions/6051/what-is-the-default-order-of-records-for-a-select-statement-in-mysql
Recommended reading
MySQL Database design specification , Worth reading
MySQL Database of the Internet commonly used sub database sub table scheme
【 A person seems to be thinking logically , But in fact, most of the time is just memory retrieval .】
版权声明
本文为[Fei Yun a]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221135539722.html
边栏推荐
- mysql数据库已经成功启动,可是show不是内部或外部命令,该如何解决呢?
- Corners of enterprise mailbox
- redis 更新升级版本
- Chrome多设备书签同步方案
- mysql FUNCTION xxx.charindex does not exist
- Graph search of obstacles in far planner
- From construction to governance, the industry's first white paper on microservice governance technology was officially released (including a free download link)
- RedisConfig配置类
- Leetcode 118, Yanghui triangle
- Node 调试指南 —— Inspector 协议
猜你喜欢

Wong Kwong Yu Kwai tiktok is unable to sleep soundly.

销量大腰斩,岚图无蓝图

Teach you what to beep

R语言绘制小提琴图geom_violin,如何添加额外的点geom_point?geom_violin + geom_boxplot + geom_point组合使用

Rust实现斐波那契数

Ros2 - what is an interface

VMware虚拟机克隆后NAT模式下网络的配置

XML外部实体攻击原理以及实战(XXE)(1)

Redisconfig configuration class

难得盈利或是昙花一现,达芙妮学不会南极人
随机推荐
ROS2——什么是接口
HDU 2680 最短路 Dijkstra + 链式向前星 + 优先队列(模板)
The decision curve analysis (DCA) function is written in R language, and multiple decision curves are analyzed. DCA curves are visualized in the same image
With the help of digital business cloud real estate industry procurement platform solution, realize simple, intelligent and compliant procurement
柔性印刷电路板(PCB)的设计方法、类型等详细解析
Compressed backup of raspberry pie
Redisconfig configuration class
VMware虚拟机克隆后NAT模式下网络的配置
Redis local connection to view data
API of H5 mobile terminal
Day code 300 lines learning notes day 47
R language uses dhyper function to generate hypergeometric distribution density data and plot function to visualize hypergeometric distribution density data
R language multiple decision curve analysis DCA (decision curve analysis) curve visualization in the same image, using PNG function to save the DCA visualization results of decision curve analysis in
Rsync remote synchronization
Walking in the clouds - but there are books
XML外部实体攻击原理以及实战(XXE)(1)
封装统一响应结果枚举类(工具模块)
手把手教你配置哔哔点啥
Station B cuts to the live broadcast, sooner or later
ROS2——手把手教你编写一个服务