Mysql EXPLAIN 相关疑问: Using temporary ; Using filesort
一、什么是Using temporary ; Using filesort
1. using filesort
filesort主要用于查询数据结果集的排序操作,首先MySQL会使用sort_buffer_size大小的内存进行排序,如果结果集超过了sort_buffer_size大小,会把这一个排序后的chunk转移到file上,最后使用多路归并排序完成所有数据的排序操作。
MySQL filesort有两种使用模式:
模式1: sort的item保存了所需要的所有字段,排序完成后,没有必要再回表扫描。
模式2: sort的item仅包括,待排序完成后,根据rowid查询所需要的columns。
很明显,模式1能够极大的减少回表的随机IO。
2. using temporary
MySQL使用临时表保存临时的结构,以用于后续的处理,MySQL首先创建heap引擎的临时表,如果临时的数据过多,超过max_heap_table_size的大小,会自动把临时表转换成MyISAM引擎的表来使用。
从上面的解释上来看,filesort和temporary的使用场景的区别并不是很明显,不过,有以下的原则:
filesort只能应用在单个表上,如果有多个表的数据需要排序,那么MySQL会先使用using temporary保存临时数据,然后再在临时表上使用filesort进行排序,最后输出结果。
参考:数据库内核月报
1.What does Using filesort mean in MySQL?
The truth is, filesort is badly named. Anytime a sort can’t be performed from an index, it’s a filesort. It has nothing to do with files.
Filesort should be called “sort.” It is quicksort at heart.
2. Well, the doc gives the exact reasons when "Using temporary" will appear:
Temporary tables can be created under conditions such as these:
If there is an ORDER BY clause and a different GROUP BY clause, or if the ORDER BY or GROUP BY contains columns from tables other than the first table in the join queue, a temporary table is created.
DISTINCT combined with ORDER BY may require a temporary table.
If you use the SQL_SMALL_RESULT option, MySQL uses an in-memory temporary table, unless the query also contains elements (described later) that require on-disk storage.
如果有ORDER BY子句和一个不同的GROUP BY子句,或者如果ORDER BY或GROUP BY中的字段都来自其他的表而非连接顺序中的第一个表的话,就会创建一个临时表了
3. 可以向select 语句添加很多关键字来修改行为:
- DISTINCT关键字删除包含结果信中具有重复值的记录
- SQL_CALC_FOUND_ROWS关键字告诉MySQL计算符合查询(不需要考虑可能设置的任何LIMIT)的总行数。通过调用FOUND_ROWS()函数可以得到总行数
- SQL_CACHE 和SQL_NO_CACHE关键字告诉MySQL查询结果是否需要高速缓存
- SQL_BUFFER_RESULT关键字强制MySQL把查询结果存储到一个临时表。这使缓冲器消除了对查询所使用的表的锁定,而且结果被传送给客户,因而可以暂被其他进程使用
- SQL_BIG_RESULT 和SQL_SMALL_RESULT关键字可以指定结果集的期望大小,因此可帮助找到最佳的方法对返回的记录进行排序和存储(基于磁盘或者内存中的临时表)
- SQL_HIGH_PRIORITY关键字提升了与UPDATE,INSERT和DELETE语句相竞争的查询的优先级,因而可以在繁忙的数据库服务器上快速的执行查询
二、Using index VS Using index condition
Using index condition : where condition contains indexed and non-indexed column and the optimizer will first resolve the indexed column and will look for the rows in the table for the other condition (index push down)
Using where; Using index : 'Using index' meaning not doing the scan of entire table. 'Using where' may still do the table scan on non-indexed column but it will use if there is any indexed column in the where condition first more like using index condition
Which is better? 'Using where; Using index' would be better then 'Using index condition' if query has index all covering.
三、rows是估计值
估算值来自
select * from information_schema.tables where table_name='sm_bill' and table_schema='super_manager_v2';
Mysql EXPLAIN 相关疑问: Using temporary ; Using filesort的更多相关文章
- EXPLAIN sql优化方法(2) Using temporary ; Using filesort
优化GROUP BY语句 默认情况下,MySQL对所有GROUP BY col1,col2...的字段进行排序.这与在查询中指定ORDER BY col1,col2...类似.因此,如果显式包括一 ...
- 一次mysql 优化 (Using temporary ; Using filesort)
遇到一个SQL执行很慢 SQL 如下: SELECT ... FROM tableA WHERE time >= 1492044535 and time <= 1492046335 GRO ...
- mysql explain using filesort
创建表,字段tid上无索引(mysql 5.7) CREATE TABLE `test` ( `tid` int(11) DEFAULT NULL, `tname` varchar(12) DEFAU ...
- 【转载】 mysql explain用法
转载链接: mysql explain用法 官网说明: http://dev.mysql.com/doc/refman/5.7/en/explain-output.html 参数: htt ...
- Mysql Explain 详解(转)
原文:http://www.cnitblog.com/aliyiyi08/archive/2008/09/09/48878.html 一.语法 explain < table_name > ...
- mysql explain详解
对于经常使用mysql的兄弟们,对explain一定不会陌生.当你在一条SELECT语句前放上关键词EXPLAIN,MySQL解释它将如何处理SELECT,提供有关表如何联合和以什么次序的信息.借助于 ...
- [转]MySQL Explain详解
在日常工作中,我们会有时会开慢查询去记录一些执行时间比较久的SQL语句,找出这些SQL语句并不意味着完事了,些时我们常常用到explain这个命令来查看一个这些SQL语句的执行计划,查看该SQL语句有 ...
- mysql explain 命令讲解
explian命令可以显示select语句的执行计划 explain的结果中每行对应select语句中的一个表,输出结果中的顺序是按照语句处理表的顺序. mysql使用嵌套循环来处理所有的join连接 ...
- [mysql] mysql explain 使用
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 先解析一条sql语句,看出现什么内容 EXPLAINSELECTs.uid, ...
随机推荐
- linux cp 复制文件夹
复制文件夹需要添加 -r 或 -R 参数(recursive: 递归的:循环的) 如 cp -r DIR_A DIR_B; 同理,rm 也一样. 如 rm -r DIR_B
- VCL 中的 Windows API 函数(6): BeginDeferWindowPos
BeginDeferWindowPos 和 DeferWindowPos.EndDeferWindowPos 是一组一起使用的函数, 可对一组窗口的位置.大小.Z 序等进行调整, 在 ExtCtrls ...
- 重载 CreateParams 方法[1]: 从一个例子开始(取消窗口最大化、最小化按钮的三种方法)
方法1: 使用 TForm 的 BorderIcons 属性 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, C ...
- Ocx控件注册不成功?可能是tlb文件导致~
Ocx文件是最常用的文件,实际操作中常常需要注册之~ 但是问题来了,经常会出现注册不成功的问题: 解决方法: 1.以“管理员身份”注册 2.Dependency Walker查看依赖是否缺失 3.查看 ...
- 【转自IT虾米网:www.itxm.net】外部应用和drools-wb6.1集成解决方案
一.手把手教你集成外部应用和drools workbench6.1 1. 首先按照官方文档安装workbench ,我用的是最完整版的jbpm6-console的平台系统,里面既包含j ...
- POJ 3093 Margaritas(Kind of wine) on the River Walk (背包方案统计)
题目 Description One of the more popular activities in San Antonio is to enjoy margaritas in the park ...
- swift--动画效果
一.for循环创建4*4个view,然后对立面的所有view进行动画,这里列集中动画的效果: 1,旋转动画 for tile in backgrounds{ //现将数字块大小职位原始尺寸的1/10 ...
- 关于MultiByteToWideChar与WideCharToMultiByte代码测试(宽字符与多字节字符的转换)以及字符串的转换代码测试
#pragma once #include <stdio.h> //getchar() #include <tchar.h> #include <stdlib.h> ...
- WPS之替换样式
以前写文档需要颜色设置什么的时候,都是遇到的时候,就进行设置,挺烦的,要一直切换. 今天突然想到,既然有替换应该可能也有样式替换,就查了一下,试了试果然可以,以后就这么干了
- django restframework 教程之Serialization(序列化)
介绍 本教程将会通过创建一个简单的粘贴代码项目,突出显示WebAPI,过程中,会介绍组成REST框架的各种组件,让你全面了解如何配合使用. 环境设置 建议使用virtualenv建立新的虚拟环境,确保 ...