一、什么是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';