使用filesort来满足ORDER BY (Use of filesort to Satisfy ORDER BY )

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: filesort ORDER BY (Use of filesort to Satisfy ORDER BY ) MySQL

介绍

如果一个查询的ORDER BY从句无法使用索引,那么MySQL会执行filesort操作来读取表的行数据并对这些行进行排序。在查询执行中filsort会使用额外的排序阶段。

为了让filesort操作得到内存,在MySQL8.0.12版本及以后,优化器会为filesort增量分配内存缓冲,知道系统变量设置的sort_buffer_size阈值,而不是在MySQL 8.0.12版本以前会直接根据sort_buffer_size的配置直接分配固定大小的内存缓冲。通过按需分配内存缓冲可以让用户将sort_buffer_size设置为一个很大的值来加速大的filesort排序,而不用担心小的filesort会浪费内存空间。(WIndows上依然会根据sort_buffer_size的配置分配固定大小的内存缓冲,因为Windows有一个较弱的多线程malloc函数。)

如果结果集太大超过了内存缓冲区大小,那么filesort操作会使用临时文件来存储数据。一些类型的查询可以完成通过in-memory的filesort操作来完成。例如,优化器可以在内存中高效的完成filesort而不用使用临时文件,下列是查询或子查询的ORDER BY形式:

SELECT ... FROM single_table ... ORDER BY non_index_column [DESC] LIMIT [M,]N;

下列查询通常是web应用程序,这些通常是从一个大的结果集中展示几行:

SELECT col1, ... FROM t1 ... ORDER BY name LIMIT 10;
SELECT col1, ... FROM t1 ... ORDER BY RAND() LIMIT 15;

原文链接

  1. https://dev.mysql.com/doc/refman/8.0/en/order-by-optimization.html#order-by-filesort
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
4月前
|
SQL
[已解决]使用order by 排序后的是10 6 7 8 9 而不是 6 7 8 9 10?
[已解决]使用order by 排序后的是10 6 7 8 9 而不是 6 7 8 9 10?
53 0
|
5月前
|
SQL 关系型数据库 MySQL
mysql使用ORDER BY和GROUP BY
mysql使用ORDER BY和GROUP BY
39 0
|
10月前
ORDER BY子句
ORDER BY子句
35 0
1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause
1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause
172 0
1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause
Distinct
SQL 去重
112 0
|
SQL 关系型数据库 MySQL
报错:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregat
报错:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregat
280 0
报错:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregat
|
SQL 关系型数据库 MySQL
MySQL - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column
MySQL - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column
159 0
|
SQL 关系型数据库 MySQL
order by使用
order by使用
134 0
order by使用
|
SQL 关系型数据库 MySQL
select、distinct、limit使用
select、distinct、limit使用
226 0
select、distinct、limit使用