MySQL临时表

注意:只要你的sql语句存在联表GROUP BY操作,explain的结果就会有Using temporary,哪怕就只有几行数据,但是有这个并不代表你的查询就会很慢(如果数据小就不一定慢)

状态参数

show global status like ‘created_tmp%’;

主要涉及3个参数:

  • Created_tmp_disk_tables
  • Created_tmp_files
  • Created_tmp_tables
    每次创建临时表,Created_tmp_tables增加,如果是在磁盘上创建临时表,Created_tmp_disk_tables也增加,Created_tmp_files表示MySQL服务创建的临时文件文件数,比较理想的配置是:

    Created_tmp_disk_tables / Created_tmp_tables * 100% <## 25%

    比如上面的服务器Created_tmp_disk_tables / Created_tmp_tables * 100% = 1.20%,应该相当好了。

配置

show variables where Variable_name in (‘tmp_table_size’, ‘max_heap_table_size’);

主要涉及2个配置:

  • max_heap_table_size

  • tmp_table_size

只有max_heap_table_size设置的数值以下的临时表才能全部放内存,超过的就会用到硬盘临时表。