MySQL排序内部原理探秘
<p style="color:rgb(51,51,51);font-family:'microsoft yahei',arial;font-size:16px;text-align:justify;"> <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 一、我们要解决什么问题? 二、排序,排序,排序? 三、索引优化排序? 四、排序模式? 五、外部排序? 六、trace结果解释? 七、MySQL其他相关排序参数? 八、MySQL排序优化总结? 九、参考文献 <h2 style="font-family:'microsoft yahei',arial;font-weight:500;line-height:1.1;color:rgb(51,51);font-size:30px;text-align:justify;"> 一、我们要解决什么问题 <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> MySQL排序其实是一个老生常谈的问题了,但是我们这次想由浅入深详细说说MySQL排序模式,怎么影响MySQL选择不同的排序模式和怎么优化排序。 <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 同时也希望通过这篇文章解决大家的以下疑问: <ol style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 有啥关系,在哪些情况下增加 到底是什么鬼,该状态值过大说明了什么问题,可以通过什么方法解决;
)的索引就能够利用B tree的特性来避免额外排序。
* t1
key_part1,key_part2,... ;
<p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 从以上例子里面我们也可以看到,如果要让MySQL使用索引优化排序应该怎么建组合索引。 <h2 style="font-family:'microsoft yahei',51);font-size:30px;text-align:justify;"> 四、排序模式 <h3 style="font-family:'microsoft yahei',51);font-size:24px;text-align:justify;"> 4.1 实际trace结果 <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 但是还是有非常多的SQL没法使用索引进行排序,例如 <p style="color:rgb(51,63);">select * from film where Producer like '东京热%' and prod_time>'2015-12-01' order by actor_age; <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 我们想查询“东京热”出品的,从去年12月1号以来,并且按照演员的年龄排序的电影信息。? (好吧,假设我这里有一个每一位男DBA都想维护的数据库:) <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 这种情况下,使用索引已经无法避免排序了,那MySQL排序到底会怎么做列。 <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 笼统的来说,它会按照: <ol style="color:rgb(51,arial;font-size:16px;text-align:justify;"> ’2015-12-01’ 过滤数据,查找需要的数据;进行排序,并 按照
依据 '2015-12-01'))","attached_conditions_computation": [ ],"attached_conditions_summary": [ { "table": "`film`","attached": "((`film`.`Producer` like '东京热%') and (`film`.`prod_time` > '2015-12-01'))" } ] } 对查找到的数据按照 #stepsfilesort_informationdirectionfieldactor_agefilesort_priority_queue_optimizationusablecause applicable ( LIMIT)filesort_executionfilesort_summaryexamined_rowsnumber_of_tmp_filessort_buffer_sizesort_mode
字段的asc排序操作。 : [
{
: ,: ,0);">"field":
}
"
)
(,num_rows)
,param_rows)
,num_chunks)
,table_sort_buffer_size())
_alnum(,68);">.using_packed_addons() ?
" :
param_addon_fields() ?
: )
”和“< sort_key,additional_fields >看过其他介绍介绍MySQL排序文章的同学应该比较清楚,
对应的是MySQL 4.1以后引入的“修改后排序模式”
的参数。当“排序的键值对大小” >?
根据索引或者全表扫描,按照过滤条件获得需要查询的数据;
的描述只有一句话 passes that algorithm has had . If this is large,you should consider increasing sort_buffer_size .
到底是什么,该值比较大时说明了什么,通过什么方式可以缓解这个问题。
对应的就是MySQL做归并排序的次数,也就是说,如果
函数来实现,第5步单次归并使用
<p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 截取部分<code style="font-family:'Source Code Pro',63);">merge_buffers()的代码如下, <pre class="prettyprint" style="overflow:auto;font-family:'Source Code Pro',monospace;color:inherit;display:block;">int merge_buffers(Sort_param param,IO_CACHE from_file,IO_CACHE to_file,Merge_chunk last_chunk,int flag) { <span class="hljs-keyword" style="color:rgb(0,136);">... current_thd->inc_status_sort_merge_passes(); <span class="hljs-keyword" style="color:rgb(0,arial;font-size:16px;text-align:justify;"> 可以看到:每个<code style="font-family:'Source Code Pro',63);">merge_buffers()都会增加<code style="font-family:'Source Code Pro',63);">sort_merge_passes,也就是说每一次对MERGEBUFF (7)个block归并排序都会让<code style="font-family:'Source Code Pro',63);">sort_merge_passes加一,<code style="font-family:'Source Code Pro',63);">sort_merge_passes越多表示排序的数据太多,需要多次merge pass。解决的方案无非就是缩减要排序数据的大小或者增加<code style="font-family:'Source Code Pro',63);">sort_buffer_size。 <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 打个小广告,在我们的qmonitor中就有<code style="font-family:'Source Code Pro',63);">sort_merge_pass的性能指标和参数值过大的报警设置。 <h2 style="font-family:'microsoft yahei',51);font-size:30px;text-align:justify;"> 六、trace结果解释 <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 说明白了三种排序模式和外部排序的方法,我们回过头来看一下trace的结果。 <h3 style="font-family:'microsoft yahei',51);font-size:24px;text-align:justify;"> 6.1 是否存在磁盘外部排序 <p style="color:rgb(51,63);">"number_of_tmp_files": 0, <p style="color:rgb(51,63);">number_of_tmp_files表示有多少个分片,如果<code style="font-family:'Source Code Pro',63);">number_of_tmp_files不等于0,表示一个<code style="font-family:'Source Code Pro',63);">sort_buffer_size大小的内存无法保存所有的键值对,也就是说,MySQL在排序中使用到了磁盘来排序。 <h3 style="font-family:'microsoft yahei',51);font-size:24px;text-align:justify;"> 6.2 是否存在优先队列优化排序 <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 由于我们的这个SQL里面没有对数据进行分页限制,所以<code style="font-family:'Source Code Pro',63);">filesort_priority_queue_optimization并没有启用 <pre class="prettyprint" style="overflow:auto;font-family:'Source Code Pro',0);">"filesort_priority_queue_optimization": { <span class="hljs-string" style="color:rgb(0,0);">"usable": <span class="hljs-literal" style="color:rgb(0,102);">false,0);">"cause": <span class="hljs-string" style="color:rgb(0,0);">"not applicable (no LIMIT)" }, <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 而正常情况下,使用了Limit会启用优先队列的优化。优先队列类似于FIFO先进先出队列。 <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 算法稍微有点改变,以回表排序模式为例。 <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> <span style="font-weight:700;"><code style="font-family:'Source Code Pro',63);">sort_buffer_size足够大 <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 如果Limit限制返回N条数据,并且N条数据比<code style="font-family:'Source Code Pro',63);">sort_buffer_size小,那么MySQL会把sort buffer作为priority queue,在第二步插入priority queue时会按序插入队列;在第三步,队列满了以后,并不会写入外部磁盘文件,而是直接淘汰最尾端的一条数据,直到所有的数据都正常读取完成。 <p style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 算法如下: <ul style="color:rgb(51,arial;font-size:16px;text-align:justify;"> 不够大
,导致sort buffer空间不足; 大小,避免磁盘排序; ;
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- MySQL半同步复制
- MYSQL教程MySQL几点重要的性能指标计算和优化方法总结
- PHP-Mysql存储过程where子句
- MYSQL教程Windows(x86,64bit)升级MySQL 5.7.17免安装版的详
- Mysql学习mysql统计多表交叉组合总数实例
- Mysql应用关于mysql init_connect的几个要点总结
- Mysql应用MySQL-group-replication 配置步骤(推荐)
- Mysql入门MySQL数据库下用户及用户权限配置
- MYSQL数据库修改mysql默认字符集的两种方法详细解析
- Mysql实例mysql出现“Incorrect key file for table”处理方