order by 和 group by 排序字段优化

桃扇骨 2022-09-12 04:50 314阅读 0赞

一 点睛

order by 有两种方式排序,filesort 和 index,index 效率高,它指 mysql 扫描索引本身完成排序。filesort 方式效率较低。

order by 子句,尽量使用 index 方式排序,避免使用 filesort 方式排序。

order by 满足两种情况,会使用 index 方式排序:

1 order by 语句使用索引最左前列。

2 使用 where 子句与 order 子句条件组合满足索引最左前列。

尽快能在索引列上完成排序操作,遵照索引建的最佳左前缀。

如果不在索引列上,filesort 有两种算法:mysql就要启动双路排序和单路排序。

二 双路排序算法和单路排序算法

1 双路排序

MySQL 4.1 之前是使用双路排序,字面意思就是两次扫描磁盘,最终得到数据,读取行指针和 orderby 列,对他 们进行排序,然后扫描已经排序好的列表,按照列表中的值重新从列表中读取对应的数据输出。 从磁盘取排序字段,在 buffer 进行排序,再从磁盘取其他字段。 简单来说,取一批数据,要对磁盘进行了两次扫描,众所周知,I\O 是很耗时的,所以在 mysql4.1 之后,出现了第二种改进的算法,就是单路排序。

2 单路排序

从磁盘读取查询需要的所有列,按照 order by 列在 buffer 对它们进行排序,然后扫描排序后的列表进行输出, 它的效率更快一些,避免了第二次读取数据。并且把随机 IO 变成了顺序 IO,但是它会使用更多的空间, 因为它把每一行都保存在内存中了。

3 单路排序的问题

由于单路是后出的,总体而言好过双路。

但是单路存在以下问题: 在 sort_buffer 中,方法 2 比方法 1 要多占用很多空间,因为方法 2 是把所有字段都取出, 所以有可能取出的数据的总大小超出了 sort_buffer 的容量,导致每次只能取 sort_buffer 容量大小的数据,进行排序(创建 tmp 文件,多 路合并),排完再取 sort_buffer 容量大小,再排……从而多次 I/O。

结论:

本来想省一次 I/O 操作,反而导致了大量的 I/O 操作,反而得不偿失。

三 优化方法

1 增大 sort_butter_size 参数的设置

不管用哪种算法,提高这个参数都会提高效率,当然,要根据系统的能力去提高,因为这个参数是针对每个进程的,从 1M-8M 之间调整。

2 增大 max_length_for_sort_data 参数的设置

mysql 使用单路排序的前提是排序的字段大小要小于 max_length_for_sort_data。 提高这个参数,会增加用改进算法的概率。但是如果设的太高,数据总容量超出 sort_buffer_size 的概率就增大, 明显症状是高的磁盘 I/O 活动和低的处理器使用率。(1024-8192 之间调整)。

3 减少 select 后面的查询的字段。

当 Query 的字段大小总和小于 max_length_for_sort_data 而且排序字段不是 TEXT|BLOB 类型时,会用改进后的算法——单路排序, 否则用老算法——多路排序。 两种算法的数据都有可能超出 sort_buffer 的容量,超出之后,会创建 tmp 文件进行合并排序,导致多次 I/O, 但是用单路排序算法的风险会更大一些,所以要提高 sort_buffer_size。

四 脚本

  1. create table tblA(
  2. age int,
  3. birth timestamp not null
  4. );
  5. insert into tblA(age, birth) values(22, now());
  6. insert into tblA(age, birth) values(23, now());
  7. insert into tblA(age, birth) values(24, now());
  8. create index idx_A_ageBirth on tblA(age, birth);
  9. mysql> select * from tblA;
  10. +------+---------------------+
  11. | age | birth |
  12. +------+---------------------+
  13. | 22 | 2021-09-21 10:06:16 |
  14. | 23 | 2021-09-21 10:06:17 |
  15. | 24 | 2021-09-21 10:06:17 |
  16. +------+---------------------+
  17. 3 rows in set (0.00 sec)

五 实战

  1. # age 索引用于排序,另外因为使用到覆盖索引,数据从索引中取得
  2. mysql> explain select * from tblA where age>20 order by age;
  3. +----+-------------+-------+------------+-------+----------------+----------------+---------+------+------+----------+--------------------------+
  4. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  5. +----+-------------+-------+------------+-------+----------------+----------------+---------+------+------+----------+--------------------------+
  6. | 1 | SIMPLE | tblA | NULL | index | idx_A_ageBirth | idx_A_ageBirth | 9 | NULL | 3 | 100.00 | Using where; Using index |
  7. +----+-------------+-------+------------+-------+----------------+----------------+---------+------+------+----------+--------------------------+
  8. 1 row in set, 1 warning (0.00 sec)
  9. # age,birth 索引用于排序,另外因为使用到覆盖索引,数据从索引中取得
  10. mysql> explain select * from tblA where age>20 order by age,birth;
  11. +----+-------------+-------+------------+-------+----------------+----------------+---------+------+------+----------+--------------------------+
  12. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  13. +----+-------------+-------+------------+-------+----------------+----------------+---------+------+------+----------+--------------------------+
  14. | 1 | SIMPLE | tblA | NULL | index | idx_A_ageBirth | idx_A_ageBirth | 9 | NULL | 3 | 100.00 | Using where; Using index |
  15. +----+-------------+-------+------------+-------+----------------+----------------+---------+------+------+----------+--------------------------+
  16. 1 row in set, 1 warning (0.00 sec)
  17. # order by 中带头大哥不在,所以 Using filesort
  18. mysql> explain select * from tblA where age>20 order by birth;
  19. +----+-------------+-------+------------+-------+----------------+----------------+---------+------+------+----------+------------------------------------------+
  20. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  21. +----+-------------+-------+------------+-------+----------------+----------------+---------+------+------+----------+------------------------------------------+
  22. | 1 | SIMPLE | tblA | NULL | index | idx_A_ageBirth | idx_A_ageBirth | 9 | NULL | 3 | 100.00 | Using where; Using index; Using filesort |
  23. +----+-------------+-------+------------+-------+----------------+----------------+---------+------+------+----------+------------------------------------------+
  24. 1 row in set, 1 warning (0.00 sec)
  25. # order by 中顺序反了,所以 Using filesort
  26. mysql> explain select * from tblA where age>20 order by birth,age;
  27. +----+-------------+-------+------------+-------+----------------+----------------+---------+------+------+----------+------------------------------------------+
  28. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  29. +----+-------------+-------+------------+-------+----------------+----------------+---------+------+------+----------+------------------------------------------+
  30. | 1 | SIMPLE | tblA | NULL | index | idx_A_ageBirth | idx_A_ageBirth | 9 | NULL | 3 | 100.00 | Using where; Using index; Using filesort |
  31. +----+-------------+-------+------------+-------+----------------+----------------+---------+------+------+----------+------------------------------------------+
  32. 1 row in set, 1 warning (0.00 sec)
  33. # order by 中带头大哥不在,所以 Using filesort
  34. mysql> explain select * from tblA order by birth;
  35. +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+-----------------------------+
  36. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  37. +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+-----------------------------+
  38. | 1 | SIMPLE | tblA | NULL | index | NULL | idx_A_ageBirth | 9 | NULL | 3 | 100.00 | Using index; Using filesort |
  39. +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+-----------------------------+
  40. 1 row in set, 1 warning (0.00 sec)
  41. # order by 中带头大哥不在,所以 Using filesort
  42. mysql> explain select * from tblA where birth > '2021-09-21 00:00:00' order by birth;
  43. +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+------------------------------------------+
  44. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  45. +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+------------------------------------------+
  46. | 1 | SIMPLE | tblA | NULL | index | NULL | idx_A_ageBirth | 9 | NULL | 3 | 33.33 | Using where; Using index; Using filesort |
  47. +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+------------------------------------------+
  48. 1 row in set, 1 warning (0.00 sec)
  49. # age 用于排序
  50. mysql> explain select * from tblA where birth > '2021-09-21 00:00:00' order by age;
  51. +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+--------------------------+
  52. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  53. +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+--------------------------+
  54. | 1 | SIMPLE | tblA | NULL | index | NULL | idx_A_ageBirth | 9 | NULL | 3 | 33.33 | Using where; Using index |
  55. +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+--------------------------+
  56. 1 row in set, 1 warning (0.00 sec)
  57. # order 默认是升序,如果有的字段是升序,有的是降序,就会产生 filesort
  58. mysql> explain select * from tblA order by age asc,birth desc;
  59. +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+-----------------------------+
  60. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  61. +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+-----------------------------+
  62. | 1 | SIMPLE | tblA | NULL | index | NULL | idx_A_ageBirth | 9 | NULL | 3 | 100.00 | Using index; Using filesort |
  63. +----+-------------+-------+------------+-------+---------------+----------------+---------+------+------+----------+-----------------------------+
  64. 1 row in set, 1 warning (0.00 sec)

六 小结

1 sort by 小结

watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBAY2hlbmdxaXVtaW5n_size_20_color_FFFFFF_t_70_g_se_x_16

2 group by 小结

watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBAY2hlbmdxaXVtaW5n_size_20_color_FFFFFF_t_70_g_se_x_16 1

发表评论

表情:
评论列表 (有 0 条评论,314人围观)

还没有评论,来说两句吧...

相关阅读