MySQL旧版本ORDER BY 方法
A. sort_buffer_size 排序缓存。
B. read_rnd_buffer_size 第二次排序缓存。
C. max_length_for_sort_data 的最大排序约束。
我来简单说下MySQL的排序规则。
如果查询语句select * from tb1 where 1 order by a ; 字段a没有建立索引。以上三个參数都足够大。
MySQL内部有两种排序规则:
第一种,是普通的排序。
这样的排序的特点是节省内存。可是终于会对磁盘有一次随机扫描。
大概主要步骤例如以下:
1. 因为没有WHERE条件,所以直接对磁盘进行全表扫描,把字段a以及每行的物理ID(如果为TID)拿出来。然后把全部拿到的记录全部放到sort_buffer_size中进行排序。
2. 依据排好序的TID。从磁盘随机扫描所须要的全部记录,排好序后再次把全部必须的记录放到read_rnd_buffer_size中。
另外一种,是冗余排序。
这样的排序的特点是不须要二次对磁盘进行随机扫描。可是缺点非常明显,太浪费内存空间。
跟第一种不同的是,在第一步里拿到的不不过字段a以及TID,而是把全部请求的记录全部拿到后,放到sort_buffer_size中进行排序。
这样能够直接从缓存中返回记录给client,不用再次从磁盘上获取一次。
从MySQL 5.7 后。对另外一种排序进行了打包压缩处理。避免太浪费内存。
比方对于varchar(255)来说,实际存储为varchar(3)。
那么相比之前的方式节约了好多内存。避免缓存区域不够时,建立磁盘暂时表。
下面为简单的演示
mysql> use t_girl;
Database changed
三个參数的详细值:
mysql> select truncate(@@sort_buffer_size/1024/1024,2)||'MB' as 'sort_buffer_size',truncate(@@read_rnd_buffer_size/1024/1024,2)||'MB' as read_rnd_buffer_zie,@@max_length_for_sort_data as max_length_for_sort_data;
+------------------+---------------------+--------------------------+
| sort_buffer_size | read_rnd_buffer_zie | max_length_for_sort_data |
+------------------+---------------------+--------------------------+
| 2.00MB | 2.00MB | 1024 |
+------------------+---------------------+--------------------------+
1 row in set (0.00 sec)
演示表的相关数据:
mysql> select table_name,table_rows,concat(truncate(data_length/1024/1024,2),'MB') as 'table_size' from information_schema.tables where table_name = 't1' and table_schema = 't_girl';
+------------+------------+------------+
| table_name | table_rows | table_size |
+------------+------------+------------+
| t1 | 2092640 | 74.60MB |
+------------+------------+------------+
1 row in set (0.00 sec)
开启优化器跟踪:
mysql> SET OPTIMIZER_TRACE="enabled=on",END_MARKERS_IN_JSON=on;
Query OK, 0 rows affected (0.00 sec)
从数据字典里面拿到跟踪结果:
mysql> select * from information_schema.optimizer_trace\G
*************************** 1. row ***************************
QUERY: select * from t1 where id < 10 order by id
TRACE: {
"steps": [
{
"join_preparation": {
"select#": 1,
"steps": [
{
"expanded_query": "/* select#1 */ select `t1`.`id` AS `id`,`t1`.`log_time` AS `log_time` from `t1` where (`t1`.`id` < 10) order by `t1`.`id`"
}
] /* steps */
} /* join_preparation */
},
{
"join_optimization": {
"select#": 1,
"steps": [
{
"condition_processing": {
"condition": "WHERE",
"original_condition": "(`t1`.`id` < 10)",
"steps": [
{
"transformation": "equality_propagation",
"resulting_condition": "(`t1`.`id` < 10)"
},
{
"transformation": "constant_propagation",
"resulting_condition": "(`t1`.`id` < 10)"
},
{
"transformation": "trivial_condition_removal",
"resulting_condition": "(`t1`.`id` < 10)"
}
] /* steps */
} /* condition_processing */
},
{
"table_dependencies": [
{
"table": "`t1`",
"row_may_be_null": false,
"map_bit": 0,
"depends_on_map_bits": [
] /* depends_on_map_bits */
}
] /* table_dependencies */
},
{
"ref_optimizer_key_uses": [
] /* ref_optimizer_key_uses */
},
{
"rows_estimation": [
{
"table": "`t1`",
"table_scan": {
"rows": 2092640,
"cost": 4775
} /* table_scan */
}
] /* rows_estimation */
},
{
"considered_execution_plans": [
{
"plan_prefix": [
] /* plan_prefix */,
"table": "`t1`",
"best_access_path": {
"considered_access_paths": [
{
"access_type": "scan",
"rows": 2.09e6,
"cost": 423303,
"chosen": true,
"use_tmp_table": true
}
] /* considered_access_paths */
} /* best_access_path */,
"cost_for_plan": 423303,
"rows_for_plan": 2.09e6,
"sort_cost": 2.09e6,
"new_cost_for_plan": 2.52e6,
"chosen": true
}
] /* considered_execution_plans */
},
{
"attaching_conditions_to_tables": {
"original_condition": "(`t1`.`id` < 10)",
"attached_conditions_computation": [
] /* attached_conditions_computation */,
"attached_conditions_summary": [
{
"table": "`t1`",
"attached": "(`t1`.`id` < 10)"
}
] /* attached_conditions_summary */
} /* attaching_conditions_to_tables */
},
{
"clause_processing": {
"clause": "ORDER BY",
"original_clause": "`t1`.`id`",
"items": [
{
"item": "`t1`.`id`"
}
] /* items */,
"resulting_clause_is_simple": true,
"resulting_clause": "`t1`.`id`"
} /* clause_processing */
},
{
"refine_plan": [
{
"table": "`t1`",
"access_type": "table_scan"
}
] /* refine_plan */
}
] /* steps */
} /* join_optimization */
},
{
"join_execution": {
"select#": 1,
"steps": [
{
"filesort_information": [
{
"direction": "asc",
"table": "`t1`",
"field": "id"
}
] /* filesort_information */,
"filesort_priority_queue_optimization": {
"usable": false,
"cause": "not applicable (no LIMIT)"
} /* filesort_priority_queue_optimization */,
"filesort_execution": [
] /* filesort_execution */,
"filesort_summary": {
"rows": 62390,
"examined_rows": 2097152,
"number_of_tmp_files": 0,
"sort_buffer_size": 2097152,
"sort_mode": "<sort_key, additional_fields>"
} /* filesort_summary */
}
] /* steps */
} /* join_execution */
}
] /* steps */
}
MISSING_BYTES_BEYOND_MAX_MEM_SIZE: 0
INSUFFICIENT_PRIVILEGES: 0
1 row in set (0.00 sec) mysql>
当中以上红色部分<sort_key, additional_fields> 表示用了另外一种排序规则。
其它的两种<sort_key, rowid> 以及<sort_key, packed_additional_fields>分别代表第一种和兴许版本号MySQL的提上涨。 走自己的经验。
版权声明:本文博主原创文章,博客,未经同意不得转载。
MySQL旧版本ORDER BY 方法的更多相关文章
- Zepto源码分析之二(新旧版本zepto.Z方法的区别)
在上一节中讲到Z()方法,是在初始化函数init中直接调用zepto.Z() zepto.Z = function(dom, selector) { dom = dom || [] dom.selec ...
- 如何在苹果官网下载旧版本的Xcode 方法
1 在百度里输入“苹果开发者中心“,进入以下页面.点击页面中的“Member Center" 2 出现登录界面.这是需要苹果开发者帐号的,没有帐号的可以选择“Create Apple ...
- ORM框架greenDao 2 (用于了解旧版本的使用方法,目前最新版本为3.2.2,使用注释的方式来生成)
摘要: Android中对SQLite数据库使用,是一件非常频繁的事情.现今,也有非常多的SQLite处理的开源框架,其中最著名的greenDao,它以占用资源少,处理效率高等特点,成为优秀的ORM框 ...
- yum安装指定(特定)版本(旧版本)软件包的方法
在命令行里输入: yum list SDL 注意这里类库的名字是区别大小写的. 参考 http://www.dabu.info/yum-install-specific-version-old-pac ...
- 安装AD15有问题多数是因为旧版本AD软件没有卸载干净,清理方法详解
论坛中总会看到有些朋友安装AD14.x,AD15.x后,使用不正常,多数情况是因为旧版本的AD软件没有卸载干净,安装新版本AD软件后,就会有问题.卸载和清理AD旧版本软件的方法如下(此方法只能解决卸载 ...
- 查看mysql数据库版本方法总结
当你接手某个mysql数据库管理时,首先你需要查看维护的mysql数据库版本:当开发人员问你mysql数据库版本时,而恰好你又遗忘了,那么此时也需要去查看mysql数据库的版本............ ...
- 如何下载旧版本的MySQL
可能存在这样的场景,比如一些老系统需要使用MySQL 5.5版本才能运行,其余的不行. 1.登录下载站点 https://dev.mysql.com/downloads/mysql/ 此时的最新版本为 ...
- mysql 5.7以上版本安装配置方法图文教程(mysql 5.7.12\mysql 5.7.13\mysql 5.7.14)(转)
http://www.jb51.net/article/90302.htm ******************************* 这篇文章主要为大家分享了MySQL 5.7以上缩版本安装配置 ...
- 【转载】 旧版本Microsoft Office正在配置解决方法
原文:https://blog.csdn.net/sinat_37215184/article/details/81053931 在运行Microsoft Office 2010等旧版本的Office ...
随机推荐
- 如何把canvas元素作为网站背景总结详解
如何把canvas元素作为网站背景总结详解 一.总结 一句话总结:最简单的做法是绝对定位并且z-index属性设置为负数. 1.如何把canvas元素作为网站背景的两种方法? a.设置层级(本例代码就 ...
- 28、从零写UVC驱动之实现设置属性
1. 先看APP以确定需要实现哪些接口xawtv.c: grabber_scan ng_vid_open//根据链表的设置和读取可以在xawtv中找到是调用v4l2_driver.open v4l2_ ...
- 【u230】回文词
Time Limit: 1 second Memory Limit: 128 MB [问题描述] CR喜欢研究回文词,有天他发现一篇文章,里面有很多回文数,这使他来了兴趣.他决定找出所有长度在n个字节 ...
- [TypeStyle] Reusable styles using TypeStyle mixins
TypeStyle’s style function allows you to give multiple objects as an argument. This provides a simpl ...
- [JS Compose] 2. Enforce a null check with composable code branching using Either
We define the Either type and see how it works. Then try it out to enforce a null check and branch o ...
- SAP 中的popup dialog (弹出对话框) 常见实现方法
方法1: FM:POPUP_TO_CONFIRM(标准对话弹出消息) 有三个button:YES-NO-CANL,可进行对应的逻辑推断 可设定标题,描写叙述问题,不方便对文本进行换行等排版,不能改 ...
- 苹果浏览器Safari对html标签submit按钮的默认渲染
-webkit-appearance: none; 上面的设置就告诉Safari不要使用默认渲染,使用我们写好的 有这么一个webkit的私有属性: -webkit-appearance:none; ...
- 多校第六场 HDU 4927 JAVA大数类+模拟
HDU 4927 −ai,直到序列长度为1.输出最后的数. 思路:这题实在是太晕了,比赛的时候搞了四个小时,从T到WA,唉--对算组合还是不太了解啊.如今对组合算比較什么了-- import java ...
- php实现二叉树遍历
php实现二叉树遍历 一.总结 关注输入输出 二.php实现二叉树遍历 题目描述 编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储). 例如如下的先序遍历字符串 ...
- Hdu4771(杭州赛区)
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...