从MySQL临时表谈到filesort
内部临时表的类型和产生时机相关,翻译自:http://dev.mysql.com/doc/refman/5.6/en/internal-temporary-tables.html
In some cases, the server creates internal temporary tables while processing queries. Such a table can be held in memory and processed by the MEMORY storage engine, or stored on disk and processed by the MyISAM storage engine. The server may create a temporary table initially as an in-memory table, then convert it to an on-disk table if it becomes too large. Users have no direct control over when the server creates an internal temporary table or which storage engine the server uses to manage it.
有时候数据库服务器在执行某些查询的时候会生成内部临时表,这些临时表有可能是生成在内存里的由MEMORY引擎处理的,也有可能是生成在磁盘上由MyISAM引擎处理的。如果说在内存中的临时表大小超过限制,服务器则会将临时表保存成磁盘临时表。用户无法直接控制这些内部临时表和管理这些临时表的数据库引擎。
Temporary tables can be created under conditions such as these:
内部临时表产生的时机有以下几种:
- If there is an
ORDER BYclause and a differentGROUP BYclause, or if theORDER BYorGROUP BYcontains columns from tables other than the first table in the join queue, a temporary table is created.
- 使用 ORDER BY 子句和一个不一样的 GROUP BY 子句(经过笔者实验,应该是GROUP BY一个无索引列,就会产生临时表),或者 ORDER BY 或 GROUP BY 的列不是来自JOIN语句序列的第一个表,就会产生临时表(经笔者实验,应该是使用JOIN时, GROUP BY 任何列都会产生临时表)
DISTINCTcombined withORDER BYmay require a temporary table.
- DISTINCT 和 ORDER BY 一起使用时可能需要临时表(笔者实验是只要用了DISTINCT(非索引列),都会产生临时表)
- If you use the
SQL_SMALL_RESULToption, MySQL uses an in-memory temporary table, unless the query also contains elements (described later) that require on-disk storage.
用了 SQL_SMALL_RESULT, mysql就会用内存临时表。定义:SQL_BIG_RESULTorSQL_SMALL_RESULTcan be used withGROUP BYorDISTINCTto tell the optimizer that the result set has many rows or is small, respectively. ForSQL_BIG_RESULT, MySQL directly uses disk-based temporary tables if needed, and prefers sorting to using a temporary table with a key on theGROUP BYelements. ForSQL_SMALL_RESULT, MySQL uses fast temporary tables to store the resulting table instead of using sorting. This should not normally be needed.
To determine whether a query requires a temporary table, use EXPLAIN and check the Extra column to see whether it says Using temporary. See Section 8.8.1, “Optimizing Queries with EXPLAIN”.
可以用EXPLAIN来查看Extra字段判断是否使用了临时表
Some conditions prevent the use of an in-memory temporary table, in which case the server uses an on-disk table instead:
有些情况服务器会直接使用磁盘临时表
- 表里存在BLOB或者TEXT的时候(这是因为MEMORY引擎不支持这两种数据类型,这里笔者补充一下,并非只要查询里含有BLOB和TEXT类型的列就会产生磁盘临时表,按照高性能MYSQL里的话,应该这么说:“Because the Memory storage engine doesn't support the BLOB and TEXT types, queries that use BLOB or TEXT columns and need an implicit temporary table will have to use on-disk MyISAM temporry tables, even for only a few rows.”也就是说如果我们的查询中包含了BLOB和TEXT的列,而且又需要临时表,这时候临时表就被强制转成使用磁盘临时表,所以此书一直在提醒我们,如果要对BLOB和TEXT排序,应该使用SUBSTRING(column, length)将这些列截断变成字符串,这样就可以使用in-memory临时表了)
Presence of any column in a
GROUP BYorDISTINCTclause larger than 512 bytes- GROUP BY 或者 DISTINCT 子句大小超过 512 Bytes
Presence of any column larger than 512 bytes in the
SELECTlist, ifUNIONorUNION ALLis used- 使用了UNION 或 UNION ALL 并且 SELECT 的列里有超过512 Bytes的列
If an internal temporary table is created initially as an in-memory table but becomes too large, MySQL automatically converts it to an on-disk table. The maximum size for in-memory temporary tables is the minimum of the tmp_table_size and max_heap_table_size values. This differs from MEMORY tables explicitly created with CREATE TABLE: For such tables, the max_heap_table_size system variable determines how large the table is permitted to grow and there is no conversion to on-disk format.
如果内置内存临时表创建后变得太大,MySQL会自动将它转换成磁盘临时表。内存临时表的大小取决与 tmp_table_size参数和max_heap_table_size参数的值。用 CREATE TABLE 产生的内存临时表的大小取决与 max_heap_table_size来决定是否要将其转换成磁盘临时表
When the server creates an internal temporary table (either in memory or on disk), it increments theCreated_tmp_tables status variable. If the server creates the table on disk (either initially or by converting an in-memory table) it increments the Created_tmp_disk_tables status variable.
当服务器生成一个内存临时表,Created_tmp_tables状态变量值会增加,当服务器创建了一个磁盘临时表时,Created_tmp_disk_tables状态变量值会增加。(这几个变量可以通过 show status命令查看得到)
Tips: internal temporaray table 的大小受限制的是tmp_table_size和max_heap_table_size的最小值;而 user-created temporary table的大小只受限与max_heap_table_size,而与tmp_table_size无关。以下是文档原文,注意粗体部分
| Command-Line Format | --tmp_table_size=# |
||
| Option-File Format | tmp_table_size |
||
| Option Sets Variable | Yes, tmp_table_size |
||
| Variable Name | tmp_table_size |
||
| Variable Scope | Global, Session | ||
| Dynamic Variable | Yes | ||
| Permitted Values | |||
| Type | numeric |
||
| Default | system dependent |
||
| Range | 1024 .. 4294967295 |
||
The maximum size of internal in-memory temporary tables. (The actual limit is determined as the minimum oftmp_table_size and max_heap_table_size.) If an in-memory temporary table exceeds the limit, MySQL automatically converts it to an on-disk MyISAM table. Increase the value of tmp_table_size (andmax_heap_table_size if necessary) if you do many advanced GROUP BY queries and you have lots of memory. This variable does not apply to user-created MEMORY tables.
You can compare the number of internal on-disk temporary tables created to the total number of internal temporary tables created by comparing the values of the Created_tmp_disk_tables andCreated_tmp_tables variables.
See also Section 8.4.3.3, “How MySQL Uses Internal Temporary Tables”.
| Command-Line Format | --max_heap_table_size=# |
||
| Option-File Format | max_heap_table_size |
||
| Option Sets Variable | Yes, max_heap_table_size |
||
| Variable Name | max_heap_table_size |
||
| Variable Scope | Global, Session | ||
| Dynamic Variable | Yes | ||
| Permitted Values | |||
| Platform Bit Size | 32 |
||
| Type | numeric |
||
| Default | 16777216 |
||
| Range | 16384 .. 4294967295 |
||
| Permitted Values | |||
| Platform Bit Size | 64 |
||
| Type | numeric |
||
| Default | 16777216 |
||
| Range | 16384 .. 1844674407370954752 |
||
This variable sets the maximum size to which user-created MEMORY tables are permitted to grow. The value of the variable is used to calculate MEMORY table MAX_ROWS values. Setting this variable has no effect on any existingMEMORY table, unless the table is re-created with a statement such as CREATE TABLE or altered with ALTER TABLE or TRUNCATE TABLE. A server restart also sets the maximum size of existing MEMORY tables to the globalmax_heap_table_size value.
This variable is also used in conjunction with tmp_table_size to limit the size of internal in-memory tables. SeeSection 8.4.3.3, “How MySQL Uses Internal Temporary Tables”.
max_heap_table_size is not replicated. See Section 16.4.1.21, “Replication and MEMORY Tables”, andSection 16.4.1.34, “Replication and Variables”, for more information.
————————————————————————————————————————————————————————————————————
下面来说说filesort。什么是filesort?翻译一篇来自 Baron Schwartz的blog,他是 High Performance MySQL的第一作者
If you were interviewing to work at Percona, and I asked you “what does Using filesort mean in EXPLAIN,” what would you say?
I have asked this question in a bunch of interviews so far, with smart people, and not one person has gotten it right. So I consider it to be a bad interview question, and I’m going to put the answer here. If anyone gets it wrong from now on, I know they don’t read this blog!
这段在吹牛比。
The usual answer is something like “rows are being placed into a temporary table which is too big to fit in memory, so it gets sorted on disk.” Unfortunately, this is not the same thing. First of all, this is Using temporary. Secondly, temporary tables may go to disk if they are too big, but EXPLAIN doesn’t show that. (If I interview you, I might ask you what “too big” means, or I might ask you the other reason temporary tables go to disk!)
一般人的回答是: “当行数据太大,导致内存无法容下这些数据产生的临时表时,他们就会被放入磁盘中排序。” 很不幸,这个答案是错的。首先,这个叫做 Using temporary (参见Expain 或者 DESC里的Extra字段);第二,临时表在太大的时候确实会到磁盘离去,但是EXPLAIN不会显示这些。 (Bala bala bala...)
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.
那么事实是, filesort 这个名字取得太搓逼了。 filesort的意思是只要一个排序无法使用索引来排序,就叫filesort。他和file没半毛钱关系。filesort应该叫做sort。(笔者补充一下:意思是说如果无法用已有index来排序,那么就需要数据库服务器额外的进行数据排序,这样其实是会增加性能开销的。)
If the sort is bigger than the sort buffer, it is performed a bit at a time, and then the chunks are merge-sorted to produce the final sorted output. There is a lot more to it than this. I refer you to Sergey Petrunia’s article on How MySQL executes ORDER BY.You can also read about it in our book, but if you read Sergey’s article you won’t need to.
如果说sort数据比sort buffer还大,那么排序会分解成多部分,每次排序一小部分,最后将各部分合并后输出(你就是想说合并排序吧)。这里面还有很多东西可说哦。推荐你去看xxxxx,或者是看我们的书(High Performance MySQL,不得不说确实是好书)。bala bala。。。
OK。现在了解神码是filesort了,实际上确实这玩意和临时表和文件没半毛钱关系。大家可以试试用 order by 一个无索引的列,Extra里就会出现 Using filesort了
。。。额。。。我是为毛把临时表和filesort放在一起来了?
从MySQL临时表谈到filesort的更多相关文章
- MySQL 临时表
MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间. 临时表在MySQL 3.23版本中添加,如果你的MySQL版本 ...
- mysql临时表的产生
sql执行会生成一个巨大的临时表,当内存放不下时,要全部copy 到磁盘,导致IO飙升,时间开销增大. 额外收获知识收藏如下: 临时表存储 MySQL临时表分为"内存临时表"和&q ...
- Mysql临时表的用法 - 51CTO.COM
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- MySQL临时表与派生表(简略版)
MySQL临时表与派生表 当主查询中包含派生表,或者当select 语句中包含union字句,或者当select语句中包含一个字段的order by 子句(对另一个字段的group by 子句)时,M ...
- Python 基于python+mysql浅谈redis缓存设计与数据库关联数据处理
基于python+mysql浅谈redis缓存设计与数据库关联数据处理 by:授客 QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3 ...
- mysql临时表产生的执行效率问题改进(转)
问题: 近日,线上MySQL查出一个慢sql,每次都要查询1000ms以上,严重影响用户体验 今得空去诊断一番,记录如下: sql原句: SELECT r.object_id AS cardId, c ...
- 二十六、MySQL 临时表
MySQL 临时表 MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间. 临时表在MySQL 3.23版本中添加,如 ...
- DB-MySQL:MySQL 临时表
ylbtech-DB-MySQL:MySQL 临时表 1.返回顶部 1. MySQL 临时表 MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysq ...
- MySQL 临时表和复制表
MySQL 临时表在我们需要保存一些临时数据时是非常有用的.临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间. 临时表在MySQL 3.23版本中添加,如果你的MySQL版本 ...
随机推荐
- 设计模式C++实现——组合模式
模式定义: 组合模式同意你将对象组合成树形结构来表现"总体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 这个模式可以创建一个树形结构,在同一个结构中处理嵌套 ...
- KVO---视图间数据的传递:标签显示输入的内容【多个视图中】
RootViewController.m #import "ModalViewController.h" @interface RootViewController () @end ...
- 【Python学习笔记】-APP图标显示未读消息数目
以小米手机系统为例,当安装的某个APP有未读消息时,就会在该APP图标的右上角显示未读消息的数目.本文主要解说怎样用Python语言实现图标显示未读消息的数目.首先,还是要用到Python中PIL库, ...
- oc10--练习
// // main.m // 练习 #import <Foundation/Foundation.h> @interface Car : NSObject { @public int w ...
- Angular2之路由学习笔记
目前工作中项目的主要技术栈是Angular2 在这里简单记录一下遇到的问题以及解决方案. 这篇笔记主要记录Angular2 的路由. 官方文档链接:https://angular.cn/docs/ts ...
- shp系列(三)——利用C++进行DBF文件的读(打开)
1.DBF文件要点 DBF文件又叫属性文件,也叫dBASE文件,文件后缀是.dbf,实际上ArcGIS打开后的属性表就是DBF的信息.DBF文件遵循以下几个条件: 每个要素在表中必须要包含一个与之相对 ...
- 如何用jQuery实现div随鼠标移动而移动?(详解)----2017-05-12
重点是弄清楚如何获取鼠标现位置与移动后位置,div现在位置与移动后位置: 用jQuery实现div随鼠标移动而移动,不是鼠标自身的位置!!而是div相对于之前位置的移动 代码如下:(注意看绿色部分的解 ...
- LayoutInflater源码解析
Android使用LayoutInflater来进行布局加载,通常获取方式有两种: 第一种: LayoutInflater layoutInflater = LayoutInflater.from(c ...
- (转)Bootstrap3 概述
http://blog.csdn.net/duruiqi_fx/article/details/53285607 注意:HTML5 文档类型 Bootstrap 使用到的某些 HTML 元素和 CSS ...
- 分层、链式分析、url、联系的长度
分层.链式分析.url.联系的长度. 分层结构符合软件处理的工具链性和步骤性: 分层的每一次都是一个节点或步骤: 链式结构普遍存在于自然界,比如食物链: 联系是普遍存在的,不只是两个事物间的联系,而且 ...