HOW MYSQL USES INTERNAL TEMPORARY TABLES
HOW MYSQL USES INTERNAL TEMPORARY TABLES
Table of Contents [hide]
通过阅读MySQL的官方手册,我们可以知道出现下面情况的时候,MySQL可能会使用临时表,下面的例子都在MySQL5.6版本下面进行测试,创建测试数据表结构:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | mysql> show create table Test_Update_Table \G *************************** 1. row ***************************        Table: Test_Update_Table Create Table: CREATE TABLE `Test_Update_Table` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `registerdate` date NOT NULL DEFAULT '0000-00-00',   `key1` varchar(10) NOT NULL DEFAULT '',   `key2` varchar(10) NOT NULL DEFAULT '',   `key3` varchar(10) NOT NULL DEFAULT '',   `key_4` varchar(255) DEFAULT NULL,   `ke5` timestamp NULL DEFAULT CURRENT_TIMESTAMP,   PRIMARY KEY (`id`),   KEY `idx_key2` (`key2`),   KEY `idx_key1_key1` (`key1`,`key2`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 1 row in set (0.00 sec) mysql> show create table t1 \G *************************** 1. row ***************************        Table: t1 Create Table: CREATE TABLE `t1` (   `i1` int(11) NOT NULL DEFAULT '0',   `i2` int(11) NOT NULL DEFAULT '0',   `d` date DEFAULT NULL,   `remark_1` varchar(128) DEFAULT NULL,   `remark_2` varchar(513) DEFAULT NULL,   PRIMARY KEY (`i1`,`i2`),   KEY `k_d` (`d`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) mysql> | 
1)UNION queries
UNION queries use temporary tables.使用UNION语句的时候会使用临时表,在MySQL5.5版本以前explain的extra是不显示Using temporary信息的,但在5.6版本以后会有该信息显示,无论哪个版都都会增加.eg:
| 1 2 3 4 5 6 7 8 9 | mysql> desc select 1 union select 1; +----+--------------+------------+------+---------------+------+---------+------+------+-----------------+ | id | select_type  | table      | type | possible_keys | key  | key_len | ref  | rows | Extra           | +----+--------------+------------+------+---------------+------+---------+------+------+-----------------+ |  1 | PRIMARY      | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used  | |  2 | UNION        | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used  | | NULL | UNION RESULT | <union1,2> | ALL  | NULL          | NULL | NULL    | NULL | NULL | Using temporary | +----+--------------+------------+------+---------------+------+---------+------+------+-----------------+ 3 rows in set (0.00 sec) | 
2)Some views
Some views require temporary tables, such those evaluated using the TEMPTABLE algorithm, or that use UNION or aggregation.一些使用临时表算法或者Union,聚合函数,临时表算法的视图,eg:
1、Union 算法
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | mysql> create view v_Test_Update_Table as select * from Test_Update_Table where key1 =1 union  select * from Test_Update_Table where key1 =22; Query OK, 0 rows affected (0.06 sec) mysql> flush status     -> ; Query OK, 0 rows affected (0.00 sec) mysql> desc select * from v_Test_Update_Table; +----+--------------+-------------------+------+---------------+------+---------+------+------+-----------------+ | id | select_type  | table             | type | possible_keys | key  | key_len | ref  | rows | Extra           | +----+--------------+-------------------+------+---------------+------+---------+------+------+-----------------+ |  1 | PRIMARY      | <derived2>        | ALL  | NULL          | NULL | NULL    | NULL |    6 | NULL            | |  2 | DERIVED      | Test_Update_Table | ALL  | idx_key1_key1 | NULL | NULL    | NULL |    3 | Using where     | |  3 | UNION        | Test_Update_Table | ALL  | idx_key1_key1 | NULL | NULL    | NULL |    3 | Using where     | | NULL | UNION RESULT | <union2,3>        | ALL  | NULL          | NULL | NULL    | NULL | NULL | Using temporary | +----+--------------+-------------------+------+---------------+------+---------+------+------+-----------------+ 4 rows in set (0.00 sec) | 
2、聚合函数,可能在explain中看不到Using temporary状态,其实derived2就是一个临时表,一定要注意
| 1 2 3 4 5 6 7 8 9 10 11 | mysql> create view v_ag_Test_Update_Table as select key1, count(*) as cnt from Test_Update_Table group by key1; Query OK, 0 rows affected (0.05 sec) mysql> desc select * from v_ag_Test_Update_Table; +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------------+ | id | select_type | table             | type  | possible_keys | key           | key_len | ref  | rows | Extra       | +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------------+ |  1 | PRIMARY     | <derived2>        | ALL   | NULL          | NULL          | NULL    | NULL |    3 | NULL        | |  2 | DERIVED     | Test_Update_Table | index | idx_key1_key1 | idx_key1_key1 | 64      | NULL |    3 | Using index | +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------------+ 2 rows in set (0.00 sec) | 
3、临时表算法
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | mysql> create view  v_all as select * from Test_Update_Table; Query OK, 0 rows affected (0.04 sec) mysql> create ALGORITHM=TEMPTABLE view v_all_temp as select * from Test_Update_Table; Query OK, 0 rows affected (0.03 sec) mysql> desc select * from v_all; +----+-------------+-------------------+------+---------------+------+---------+------+------+-------+ | id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra | +----+-------------+-------------------+------+---------------+------+---------+------+------+-------+ |  1 | SIMPLE      | Test_Update_Table | ALL  | NULL          | NULL | NULL    | NULL |    3 | NULL  | +----+-------------+-------------------+------+---------------+------+---------+------+------+-------+ 1 row in set (0.00 sec) mysql> desc select * from v_all_temp; +----+-------------+-------------------+------+---------------+------+---------+------+------+-------+ | id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra | +----+-------------+-------------------+------+---------------+------+---------+------+------+-------+ |  1 | PRIMARY     | <derived2>        | ALL  | NULL          | NULL | NULL    | NULL |    3 | NULL  | |  2 | DERIVED     | Test_Update_Table | ALL  | NULL          | NULL | NULL    | NULL |    3 | NULL  | +----+-------------+-------------------+------+---------------+------+---------+------+------+-------+ 2 rows in set (0.00 sec) | 
3)SQL_SMALL_RESULT
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.使用SQL_SMALL_RESULT选项的query,SELECT语句中指定了SQL_SMALL_RESULT关键字 SQL_SMALL_RESULT的意思就是告诉MySQL,结果会很小,请直接使用内存临时表,不需要使用索引排序 SQL_SMALL_RESULT必须和GROUP BY、DISTINCT或DISTINCTROW一起使用 一般情况下,我们没有必要使用这个选项,让MySQL服务器选择即可,这个例子我没有做出来
4) Multiple-table UPDATE
Multiple-table UPDATE statements,多表的update 查询,该查询在extra也不显示Using temporary的状态信息
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |  mysql> show status like 'create%'; +-------------------------+-------+ | Variable_name           | Value | +-------------------------+-------+ | Created_tmp_disk_tables | 0     | | Created_tmp_files       | 0     | | Created_tmp_tables      | 0     | +-------------------------+-------+ 3 rows in set (0.00 sec) mysql> desc  update Test_Update_Table a,t1 b set  b.d =current_date() where a.id = b.i1 and a.id=1; +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+ | id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra       | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+ |  1 | SIMPLE      | a     | const | PRIMARY       | PRIMARY | 4       | const |    1 | Using index | |  1 | SIMPLE      | b     | ref   | PRIMARY       | PRIMARY | 4       | const |    5 | NULL        | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+ 2 rows in set (0.00 sec) mysql>  update Test_Update_Table a,t1 b set  b.d =current_date() where a.id = b.i1 and a.id=1; Query OK, 5 rows affected (0.01 sec) Rows matched: 5  Changed: 5  Warnings: 0 mysql> show status like 'create%'; +-------------------------+-------+ | Variable_name           | Value | +-------------------------+-------+ | Created_tmp_disk_tables | 0     | | Created_tmp_files       | 0     | | Created_tmp_tables      | 2     | +-------------------------+-------+ 3 rows in set (0.00 sec) | 
5)Derived tables
Derived tables (subqueries in the FROM clause).使用Derived tables,一般情况子查询会出现该情况,不过5.6版本有改进,可以查阅相关文档
| 1 2 3 4 5 6 7 8 | mysql> desc select * from (select * from Test_Update_Table )a; +----+-------------+-------------------+------+---------------+------+---------+------+------+-------+ | id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra | +----+-------------+-------------------+------+---------------+------+---------+------+------+-------+ |  1 | PRIMARY     | <derived2>        | ALL  | NULL          | NULL | NULL    | NULL |    3 | NULL  | |  2 | DERIVED     | Test_Update_Table | ALL  | NULL          | NULL | NULL    | NULL |    3 | NULL  | +----+-------------+-------------------+------+---------------+------+---------+------+------+-------+ 2 rows in set (0.00 sec) | 
6)subquery or semi-join
Tables created for subquery or semi-join , 这个主要是为子查询和物化semi-join创建的表,这个例子也没有做出来
7)order by,group by,distinct
最终要的就是group by ,distinct, order by这几组关键字的配合,会出现临时表,文件排序,我来总结一下:
1、单表情况
1.单表无索引(order by,group by,distinct列无可用索引),order by 单列或者多列都不使用临时表,只用到Using filesort, show status like ‘Created_tmp_tables’的计数器也不变,group by, distinct 不管是单列还是多列,均使用临时表,下面是例子
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | mysql> flush status; Query OK, 0 rows affected (0.00 sec) mysql> desc select * from `Test_Update_Table` order by key_4 ; +----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+ | id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra          | +----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+ |  1 | SIMPLE      | Test_Update_Table | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using filesort | +----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+ 1 row in set (0.00 sec) mysql> desc select * from `Test_Update_Table` order by key_4,ke5 ; +----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+ | id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra          | +----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+ |  1 | SIMPLE      | Test_Update_Table | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using filesort | +----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+ 1 row in set (0.00 sec) mysql> desc select key_4 from `Test_Update_Table` group by key_4 ; +----+-------------+-------------------+------+---------------+------+---------+------+------+---------------------------------+ | id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra                           | +----+-------------+-------------------+------+---------------+------+---------+------+------+---------------------------------+ |  1 | SIMPLE      | Test_Update_Table | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using temporary; Using filesort | +----+-------------+-------------------+------+---------------+------+---------+------+------+---------------------------------+ 1 row in set (0.00 sec) mysql> desc select key_4 from `Test_Update_Table` group by key_4,ke5 ; +----+-------------+-------------------+------+---------------+------+---------+------+------+---------------------------------+ | id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra                           | +----+-------------+-------------------+------+---------------+------+---------+------+------+---------------------------------+ |  1 | SIMPLE      | Test_Update_Table | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using temporary; Using filesort | +----+-------------+-------------------+------+---------------+------+---------+------+------+---------------------------------+ 1 row in set (0.00 sec) mysql> desc select distinct key_4 from `Test_Update_Table`; +----+-------------+-------------------+------+---------------+------+---------+------+------+-----------------+ | id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra           | +----+-------------+-------------------+------+---------------+------+---------+------+------+-----------------+ |  1 | SIMPLE      | Test_Update_Table | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using temporary | +----+-------------+-------------------+------+---------------+------+---------+------+------+-----------------+ 1 row in set (0.00 sec) mysql> desc select distinct key_4 ,ke5 from `Test_Update_Table`; +----+-------------+-------------------+------+---------------+------+---------+------+------+-----------------+ | id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra           | +----+-------------+-------------------+------+---------------+------+---------+------+------+-----------------+ |  1 | SIMPLE      | Test_Update_Table | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using temporary | +----+-------------+-------------------+------+---------------+------+---------+------+------+-----------------+ 1 row in set (0.00 sec) | 
2.单表有索引情况(order by,group by,distinct列有可用索引)在能够使用索引的情况下,那么都不会出现使用临时表的状态
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | mysql> desc select * from Test_Update_Table group by key1; +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------+ | id | select_type | table             | type  | possible_keys | key           | key_len | ref  | rows | Extra | +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------+ |  1 | SIMPLE      | Test_Update_Table | index | idx_key1_key1 | idx_key1_key1 | 64      | NULL |    3 | NULL  | +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------+ 1 row in set (0.00 sec) mysql> desc select * from Test_Update_Table group by key1,key2; +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------+ | id | select_type | table             | type  | possible_keys | key           | key_len | ref  | rows | Extra | +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------+ |  1 | SIMPLE      | Test_Update_Table | index | idx_key1_key1 | idx_key1_key1 | 64      | NULL |    3 | NULL  | +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------+ 1 row in set (0.00 sec) mysql> desc select distinct key1 ,key2 from Test_Update_Table; +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------------+ | id | select_type | table             | type  | possible_keys | key           | key_len | ref  | rows | Extra       | +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------------+ |  1 | SIMPLE      | Test_Update_Table | index | idx_key1_key1 | idx_key1_key1 | 64      | NULL |    3 | Using index | +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------------+ 1 row in set (0.00 sec) mysql> desc select distinct key1  from Test_Update_Table; +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------------+ | id | select_type | table             | type  | possible_keys | key           | key_len | ref  | rows | Extra       | +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------------+ |  1 | SIMPLE      | Test_Update_Table | index | idx_key1_key1 | idx_key1_key1 | 64      | NULL |    3 | Using index | +----+-------------+-------------------+-------+---------------+---------------+---------+------+------+-------------+ 1 row in set (0.00 sec) mysql> desc select *  from Test_Update_Table order by key1; +----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+ | id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra          | +----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+ |  1 | SIMPLE      | Test_Update_Table | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using filesort | +----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+ 1 row in set (0.00 sec) mysql> desc select *  from Test_Update_Table order by key1,key2; +----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+ | id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra          | +----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+ |  1 | SIMPLE      | Test_Update_Table | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using filesort | +----+-------------+-------------------+------+---------------+------+---------+------+------+----------------+ 1 row in set (0.00 sec) | 
通过上面的例子,我们可以看出来,无论有无索引可以使用,在单表情况下,order by是不影响临时表的使用的,对于 group by, distinct后面只要有非使用上的索引列的字段,必然会使用临时表
2)多表情况
1.多表无可用索引(order by,group by,distinct列无可用索引),只有对驱动表字段排序事才会不使用临时表,其他情况均要使用临时表
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | mysql> desc select * from t1 a,Test_Update_Table b where a.i1 =b.id order by a.i2; +----+-------------+-------+------+---------------+---------+---------+-------------+------+---------------------------------+ | id | select_type | table | type | possible_keys | key     | key_len | ref         | rows | Extra                           | +----+-------------+-------+------+---------------+---------+---------+-------------+------+---------------------------------+ |  1 | SIMPLE      | b     | ALL  | PRIMARY       | NULL    | NULL    | NULL        |    3 | Using temporary; Using filesort | |  1 | SIMPLE      | a     | ref  | PRIMARY       | PRIMARY | 4       | testdb.b.id |    2 | NULL                            | +----+-------------+-------+------+---------------+---------+---------+-------------+------+---------------------------------+ 2 rows in set (0.00 sec) mysql> desc select * from t1 a,Test_Update_Table b where a.i1 =b.id order by b.key3; +----+-------------+-------+------+---------------+---------+---------+-------------+------+----------------+ | id | select_type | table | type | possible_keys | key     | key_len | ref         | rows | Extra          | +----+-------------+-------+------+---------------+---------+---------+-------------+------+----------------+ |  1 | SIMPLE      | b     | ALL  | PRIMARY       | NULL    | NULL    | NULL        |    3 | Using filesort | |  1 | SIMPLE      | a     | ref  | PRIMARY       | PRIMARY | 4       | testdb.b.id |    2 | NULL           | +----+-------------+-------+------+---------------+---------+---------+-------------+------+----------------+ 2 rows in set (0.00 sec) mysql> desc select b.key3 from t1 a,Test_Update_Table b where a.i1 =b.id group by b.key3; +----+-------------+-------+------+---------------+---------+---------+-------------+------+---------------------------------+ | id | select_type | table | type | possible_keys | key     | key_len | ref         | rows | Extra                           | +----+-------------+-------+------+---------------+---------+---------+-------------+------+---------------------------------+ |  1 | SIMPLE      | b     | ALL  | PRIMARY       | NULL    | NULL    | NULL        |    3 | Using temporary; Using filesort | |  1 | SIMPLE      | a     | ref  | PRIMARY       | PRIMARY | 4       | testdb.b.id |    2 | Using index                     | +----+-------------+-------+------+---------------+---------+---------+-------------+------+---------------------------------+ 2 rows in set (0.00 sec) mysql> desc select a.i2 from t1 a,Test_Update_Table b where a.i1 =b.id group by a.i2; +----+-------------+-------+-------+---------------+----------+---------+-------------+------+----------------------------------------------+ | id | select_type | table | type  | possible_keys | key      | key_len | ref         | rows | Extra                                        | +----+-------------+-------+-------+---------------+----------+---------+-------------+------+----------------------------------------------+ |  1 | SIMPLE      | b     | index | PRIMARY       | idx_key2 | 32      | NULL        |    3 | Using index; Using temporary; Using filesort | |  1 | SIMPLE      | a     | ref   | PRIMARY,k_d   | PRIMARY  | 4       | testdb.b.id |    2 | Using index                                  | +----+-------------+-------+-------+---------------+----------+---------+-------------+------+----------------------------------------------+ 2 rows in set (0.00 sec) mysql> desc select distinct key3 from t1 a,Test_Update_Table b where a.i1 =b.id ; +----+-------------+-------+------+---------------+---------+---------+-------------+------+-----------------------+ | id | select_type | table | type | possible_keys | key     | key_len | ref         | rows | Extra                 | +----+-------------+-------+------+---------------+---------+---------+-------------+------+-----------------------+ |  1 | SIMPLE      | b     | ALL  | PRIMARY       | NULL    | NULL    | NULL        |    3 | Using temporary       | |  1 | SIMPLE      | a     | ref  | PRIMARY       | PRIMARY | 4       | testdb.b.id |    2 | Using index; Distinct | +----+-------------+-------+------+---------------+---------+---------+-------------+------+-----------------------+ 2 rows in set (0.00 sec) mysql> desc select distinct i2 from t1 a,Test_Update_Table b where a.i1 =b.id ; +----+-------------+-------+-------+---------------+----------+---------+-------------+------+------------------------------+ | id | select_type | table | type  | possible_keys | key      | key_len | ref         | rows | Extra                        | +----+-------------+-------+-------+---------------+----------+---------+-------------+------+------------------------------+ |  1 | SIMPLE      | b     | index | PRIMARY       | idx_key2 | 32      | NULL        |    3 | Using index; Using temporary | |  1 | SIMPLE      | a     | ref   | PRIMARY,k_d   | PRIMARY  | 4       | testdb.b.id |    2 | Using index                  | +----+-------------+-------+-------+---------------+----------+---------+-------------+------+------------------------------+ 2 rows in set (0.00 sec) | 
2.多表有可用索引(order by,group by,distinct列有可用索引)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | mysql> desc select * from t1 a,Test_Update_Table b where a.i1 =b.id order by b.key2,key3; +----+-------------+-------+------+---------------+---------+---------+-------------+------+----------------+ | id | select_type | table | type | possible_keys | key     | key_len | ref         | rows | Extra          | +----+-------------+-------+------+---------------+---------+---------+-------------+------+----------------+ |  1 | SIMPLE      | b     | ALL  | PRIMARY       | NULL    | NULL    | NULL        |    3 | Using filesort | |  1 | SIMPLE      | a     | ref  | PRIMARY       | PRIMARY | 4       | testdb.b.id |    2 | NULL           | +----+-------------+-------+------+---------------+---------+---------+-------------+------+----------------+ 2 rows in set (0.00 sec) mysql> desc select * from t1 a,Test_Update_Table b where a.i1 =b.id order by b.key2,a.i2; +----+-------------+-------+------+---------------+---------+---------+-------------+------+---------------------------------+ | id | select_type | table | type | possible_keys | key     | key_len | ref         | rows | Extra                           | +----+-------------+-------+------+---------------+---------+---------+-------------+------+---------------------------------+ |  1 | SIMPLE      | b     | ALL  | PRIMARY       | NULL    | NULL    | NULL        |    3 | Using temporary; Using filesort | |  1 | SIMPLE      | a     | ref  | PRIMARY       | PRIMARY | 4       | testdb.b.id |    2 | NULL                            | +----+-------------+-------+------+---------------+---------+---------+-------------+------+---------------------------------+ 2 rows in set (0.00 sec) mysql> desc select * from t1 a,Test_Update_Table b where a.i1 =b.id order by a.i2; +----+-------------+-------+------+---------------+---------+---------+-------------+------+---------------------------------+ | id | select_type | table | type | possible_keys | key     | key_len | ref         | rows | Extra                           | +----+-------------+-------+------+---------------+---------+---------+-------------+------+---------------------------------+ |  1 | SIMPLE      | b     | ALL  | PRIMARY       | NULL    | NULL    | NULL        |    3 | Using temporary; Using filesort | |  1 | SIMPLE      | a     | ref  | PRIMARY       | PRIMARY | 4       | testdb.b.id |    2 | NULL                            | +----+-------------+-------+------+---------------+---------+---------+-------------+------+---------------------------------+ 2 rows in set (0.00 sec) mysql> desc  select key1,key2,key3 from t1 a,Test_Update_Table b where a.i1 =b.id group  by key1 ; +----+-------------+-------+------+-----------------------+---------+---------+-------------+------+---------------------------------+ | id | select_type | table | type | possible_keys         | key     | key_len | ref         | rows | Extra                           | +----+-------------+-------+------+-----------------------+---------+---------+-------------+------+---------------------------------+ |  1 | SIMPLE      | b     | ALL  | PRIMARY,idx_key1_key1 | NULL    | NULL    | NULL        |    3 | Using temporary; Using filesort | |  1 | SIMPLE      | a     | ref  | PRIMARY               | PRIMARY | 4       | testdb.b.id |    2 | Using index                     | +----+-------------+-------+------+-----------------------+---------+---------+-------------+------+---------------------------------+ 2 rows in set (0.00 sec) mysql> desc  select key1,key2 from t1 a,Test_Update_Table b where a.i1 =b.id group  by key1,key2 ; +----+-------------+-------+-------+-----------------------+---------------+---------+-------------+------+-------------+ | id | select_type | table | type  | possible_keys         | key           | key_len | ref         | rows | Extra       | +----+-------------+-------+-------+-----------------------+---------------+---------+-------------+------+-------------+ |  1 | SIMPLE      | b     | index | PRIMARY,idx_key1_key1 | idx_key1_key1 | 64      | NULL        |    3 | Using index | |  1 | SIMPLE      | a     | ref   | PRIMARY               | PRIMARY       | 4       | testdb.b.id |    2 | Using index | +----+-------------+-------+-------+-----------------------+---------------+---------+-------------+------+-------------+ 2 rows in set (0.00 sec) mysql> desc  select i1 from t1 a,Test_Update_Table b where a.i1 =b.id group  by i1 ; +----+-------------+-------+-------+---------------+----------+---------+-------------+------+----------------------------------------------+ | id | select_type | table | type  | possible_keys | key      | key_len | ref         | rows | Extra                                        | +----+-------------+-------+-------+---------------+----------+---------+-------------+------+----------------------------------------------+ |  1 | SIMPLE      | b     | index | PRIMARY       | idx_key2 | 32      | NULL        |    3 | Using index; Using temporary; Using filesort | |  1 | SIMPLE      | a     | ref   | PRIMARY,k_d   | PRIMARY  | 4       | testdb.b.id |    2 | Using index                                  | +----+-------------+-------+-------+---------------+----------+---------+-------------+------+----------------------------------------------+ 2 rows in set (0.00 sec) mysql> desc select distinct i1 from t1 a,Test_Update_Table b where a.i1 =b.id; +----+-------------+-------+-------+---------------+----------+---------+-------------+------+------------------------------+ | id | select_type | table | type  | possible_keys | key      | key_len | ref         | rows | Extra                        | +----+-------------+-------+-------+---------------+----------+---------+-------------+------+------------------------------+ |  1 | SIMPLE      | b     | index | PRIMARY       | idx_key2 | 32      | NULL        |    3 | Using index; Using temporary | |  1 | SIMPLE      | a     | ref   | PRIMARY,k_d   | PRIMARY  | 4       | testdb.b.id |    2 | Using index                  | +----+-------------+-------+-------+---------------+----------+---------+-------------+------+------------------------------+ 2 rows in set (0.00 sec) mysql> desc select distinct key1 from t1 a,Test_Update_Table b where a.i1 =b.id; +----+-------------+-------+-------+-----------------------+---------------+---------+-------------+------+------------------------------+ | id | select_type | table | type  | possible_keys         | key           | key_len | ref         | rows | Extra                        | +----+-------------+-------+-------+-----------------------+---------------+---------+-------------+------+------------------------------+ |  1 | SIMPLE      | b     | index | PRIMARY,idx_key1_key1 | idx_key1_key1 | 64      | NULL        |    3 | Using index; Using temporary | |  1 | SIMPLE      | a     | ref   | PRIMARY               | PRIMARY       | 4       | testdb.b.id |    2 | Using index; Distinct        | +----+-------------+-------+-------+-----------------------+---------------+---------+-------------+------+------------------------------+ 2 rows in set (0.00 sec) | 
从上面来看,多表操作的时候, 当order by ,group by 后面出现了非驱动表的可使用索引字段,那么必然会使用临时表,而select后面的字段出现了非可用索引字段的时候,也要使用到临时表,select后面的字段不影响order by的索引使用情况
对于distinct,除非通过group有效的消除,否则多表连接必然会出现使用临时表.
参考文章:
1) http://dev.mysql.com/doc/refman/5.6/en/internal-temporary-tables.html
HOW MYSQL USES INTERNAL TEMPORARY TABLES的更多相关文章
- Internal Temporary Tables
		8.4.4 How MySQL Uses Internal Temporary Tables 这是MySQL手册中的一节,尝试补充了一些解释.用的版本是MySQL5.6.15社区版 In some c ... 
- How MySQL Opens and Closes Tables  refuse connections  拒绝连接的原因  file descriptors
		MySQL :: MySQL 5.7 Reference Manual :: 8.4.3.1 How MySQL Opens and Closes Tables https://dev.mysql.c ... 
- Temporary Tables and the TableType Property [AX 2012]
		Temporary Tables and the TableType Property [AX 2012] 1 out of 1 rated this helpful - Rate this topi ... 
- Part 17 Temporary tables in SQL Server
		Temporary tables in SQL Server 
- An item with the same key has already been added. Key: Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal.MySqlOptionsExtension
		An item with the same key has already been added. Key: Pomelo.EntityFrameworkCore.MySql.Infrastructu ... 
- Temporary Tables临时表
		1简介 ORACLE数据库除了可以保存永久表外,还可以建立临时表temporary tables.这些临时表用来保存一个会话SESSION的数据, 或者保存在一个事务中需要的数据.当会话退出或者用户提 ... 
- mysql performance_schema 和information_schema.tables了解
		这个是关于mysql的系统表,性能表,核心表操作的一些介绍,深入算不上 我们一般很少去动 mysql information_schema 信息相关 performance_schema 性能相关 ... 
- mysql存储过程----临时表 temporary
		在存储过程中可以使用临时表,下面有一个分割字符串的例子 语法 1.创建:create temporary table 表名(列信息); 2.删除:drop table 表名; 3.清空:truncat ... 
- MySQL Installation of system tables failed!
		刚开始学习Linux,就遇到了问题. 当在RedHat下安装MySQL时提示如下错误,请高手给点指点,谢谢: rpm -vih MySQL-server-community-5.0.96-1.rhe ... 
随机推荐
- codeforces 678D Iterated Linear Function 矩阵快速幂
			矩阵快速幂的题要多做 由题可得 g[n]=A*g[n-1]+B 所以构造矩阵 { g[n] } = {A B} * { g[n-1]} { 1 } {0 1 ... 
- [Web API] 如何让 Web API 统一回传格式以及例外处理[转]
			[Web API] 如何让 Web API 统一回传格式以及例外处理 前言 当我们在开发 Web API 时,一般的情况下每个 API 回传的数据型态或格式都不尽相同,如果你的项目从头到尾都是由你一个 ... 
- aspx与ascx,ashx的用法详细的总结介绍
			这篇文章主要是对aspx与ascx,ashx的用法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助 做asp.net开发的对.aspx,.ascx和.ashx都不会陌生.关于它们,网 ... 
- dbms_file_transfer使用简介
			dbms_file_transfer这个包可以在两个位置传输文件,分别可以有以下位置: a 从一个asm diskgroup传输到另外一个asm diskgroup b 从一个asm diskgrou ... 
- ASM基本操作
			1. 添加一个磁盘组 SQL> create diskgroup recover external redundancy disk 'ORCL:kel3'; Diskgroup created. ... 
- kendoui-grid篇
			kendo确实是个好东西,能够让我们专注于后端开发,无需在效果呈现上花大力气,唯一的缺点,它是收费的,但是我目前还没发现为嘛要掏钱,因为free的也满足了我的需求. kendoUI For asp.m ... 
- Some_sort_algorithms
			复习下数据结构常用的排序算法,更多内容上wiki 快速排序(不稳定 O(n log n)) 快速排序(Quicksort)是对冒泡排序的一种改进.由C. A. R. Hoare在1962年提出.它的基 ... 
- 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇06:计分》
			6.计分 计分概述: 分值计量直接反应玩家在游戏中获得的成就感.因此,计分系统在游戏中显得尤为重要,有的反应在直接获取的分数上,有的反应在杀敌数量上等. 计分原理: 原理图,如图6-1所示. 图6-1 ... 
- http响应Last-Modified和ETag(转)
			基础知识 1) 什么是”Last-Modified”? 在浏览器第一次请求某一个URL时,服务器端的返回状态会是200,内容是你请求的资源,同时有一个Last-Modified的属性标记此文件在服务期 ... 
- Web Service 与 EJB 的分布式的区别
			EJB的分布式:一个业务逻辑可能会调用分布在多台服务器上的 EJB 组件,但是这么多的组件调用必须纳入一个事务范围之中.也就是说如果需要调用三个 EJB 组件,第一个调用成功,第二个调用成功,但第三个 ... 
