当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL ORDER BY 的实现分析

MySQL ORDER BY 的实现分析

2017年12月12日  | 移动技术网IT编程  | 我要评论

人生感悟的文章,杜拉拉升职记谢楠,豫东红脸王陈建设

下面将通过实例分析两种排序实现方式及实现图解:
假设有 table a 和 b 两个表结构分别如下:
sky@localhost : example 01:48:21> show create table ag
*************************** 1. row ***************************
table: a
create table: create table `a` (
`c1` int(11) not null default ‘0′,
`c2` char(2) default null,
`c3` varchar(16) default null,
`c4` datetime default null,
primary key  (`c1`)
) engine=myisam default charset=utf8

sky@localhost : example 01:48:32> show create table bg
*************************** 1. row ***************************
table: b
create table: create table `b` (
`c1` int(11) not null default ‘0′,
`c2` char(2) default null,
`c3` varchar(16) default null,
primary key  (`c1`),
key `b_c2_ind` (`c2`)
) engine=myisam default charset=utf8

1、利用有序索引进行排序,实际上就是当我们 query 的 order by 条件和 query 的执行计划中所利用的 index 的索引键(或前面几个索引键)完全一致,且索引访问方式为 rang、 ref 或者 index 的时候,mysql 可以利用索引顺序而直接取得已经排好序的数据。这种方式的 order by 基本上可以说是最优的排序方式了,因为 mysql 不需要进行实际的排序操作。

假设我们在table a 和 b 上执行如下sql:
sky@localhost : example 01:44:28> explain select a.* from a,b
-> where a.c1 > 2 and a.c2 < 5 and a.c2 = b.c2 order by a.c1g
*************************** 1. row ***************************
id: 1
select_type: simple
table: a
type: range
possible_keys: primary
key: primary
key_len: 4
ref: null
rows: 3
extra: using where
*************************** 2. row ***************************
id: 1
select_type: simple
table: b
type: ref
possible_keys: b_c2_ind
key: b_c2_ind
key_len: 7
ref: example.a.c2
rows: 2
extra: using where; using index

我们通过执行计划可以看出,mysql实际上并没有进行实际的排序操作,实际上其整个执行过程如下图所示:

2、通过相应的排序算法,将取得的数据在内存中进行排序方式,mysql 比需要将数据在内存中进行排序,所使用的内存区域也就是我们通过 sort_buffer_size 系统变量所设置的排序区。这个排序区是每个 thread 独享的,所以说可能在同一时刻在 mysql 中可能存在多个 sort buffer 内存区域。

第二种方式在 mysql query optimizer 所给出的执行计划(通过 explain 命令查看)中被称为 filesort。在这种方式中,主要是由于没有可以利用的有序索引取得有序的数据,mysql只能通过将取得的数据在内存中进行排序然后再将数据返回给客户端。在 mysql 中 filesort 的实现算法实际上是有两种的,一种是首先根据相应的条件取出相应的排序字段和可以直接定位行数据的行指针信息,然后在 sort buffer 中进行排序。另外一种是一次性取出满足条件行的所有字段,然后在 sort buffer 中进行排序。

在 mysql4.1 版本之前只有第一种排序算法,第二种算法是从 mysql4.1开始的改进算法,主要目的是为了减少第一次算法中需要两次访问表数据的 io 操作,将两次变成了一次,但相应也会耗用更多的 sort buffer 空间。当然,mysql4.1开始的以后所有版本同时也支持第一种算法,mysql 主要通过比较我们所设定的系统参数 max_length_for_sort_data 的大小和 query 语句所取出的字段类型大小总和来判定需要使用哪一种排序算法。如果 max_length_for_sort_data 更大,则使用第二种优化后的算法,反之使用第一种算法。所以如果希望 order by 操作的效率尽可能的高,一定要主义 max_length_for_sort_data 参数的设置。曾经就有同事的数据库出现大量的排序等待,造成系统负载很高,而且响应时间变得很长,最后查出正是因为 mysql 使用了传统的第一种排序算法而导致,在加大了 max_length_for_sort_data 参数值之后,系统负载马上得到了大的缓解,响应也快了很多。

我们再看看 mysql 需要使用 filesort 实现排序的实例。

假设我们改变一下我们的 query,换成通过a.c2来排序,再看看情况:
sky@localhost : example 01:54:23> explain select a.* from a,b
-> where a.c1 > 2 and a.c2 < 5 and a.c2 = b.c2 order by a.c2g
*************************** 1. row ***************************
id: 1
select_type: simple
table: a
type: range
possible_keys: primary
key: primary
key_len: 4
ref: null
rows: 3
extra: using where; using filesort
*************************** 2. row ***************************
id: 1
select_type: simple
table: b
type: ref
possible_keys: b_c2_ind
key: b_c2_ind
key_len: 7
ref: example.a.c2
rows: 2
extra: using where; using index

mysql 从 table a 中取出了符合条件的数据,由于取得的数据并不满足 order by 条件,所以 mysql 进行了 filesort 操作,其整个执行过程如下图所示:

在 mysql 中,filesort 操作还有一个比较奇怪的限制,那就是其数据源必须是来源于一个 table,所以,如果我们的排序数据如果是两个(或者更多个) table 通过 join所得出的,那么 mysql 必须通过先创建一个临时表(temporary table),然后再将此临时表的数据进行排序,如下例所示:

sky@localhost : example 02:46:15> explain select a.* from a,b
-> where a.c1 > 2 and a.c2 < 5 and a.c2 = b.c2 order by b.c3g
*************************** 1. row ***************************
id: 1
select_type: simple
table: a
type: range
possible_keys: primary
key: primary
key_len: 4
ref: null
rows: 3
extra: using where; using temporary; using filesort
*************************** 2. row ***************************
id: 1
select_type: simple
table: b
type: ref
possible_keys: b_c2_ind
key: b_c2_ind
key_len: 7
ref: example.a.c2
rows: 2
extra: using where

这个执行计划的输出还是有点奇怪的,不知道为什么,mysql query optimizer 将 “using temporary” 过程显示在第一行对 table a 的操作中,难道只是为让执行计划的输出少一行?

实际执行过程应该是如下图所示:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网