1、參數查看
方法一:mysql> show variables like 'tmp_table_size';
方法二:直接查看my.cnf文件tmp_table_size參數值
2、參數配置
方法一:mysql> set global tmp_table_size=16*1024*1024; 重啟后會丟失使用my.cnf參數
方法二:直接修改my.cnf文件tmp_table_size參數值,但需要重啟實例生效
3、參數值意義
tmp_table_size參數配置內部內存臨時表的大小。 此參數不適用用戶創建的MEMORY表,用戶創建的MEMORY表用max_heap_table_size參數配置。
實際限制由tmp_table_size和max_heap_table_size的值中較小的一個確定,如果內存中的臨時表超出限制,MySQL自動將其轉換為磁盤上的MyISAM表。如果要執行許多 GROUP BY查詢,可以增加tmp_table_size的值(或如有必要,也可以使用max_heap_table_size)。
執行計划中Extra字段包含有“Using temporary” 時會產生臨時表。
4、外料
MySQL中臨時表主要有兩類,包括外部臨時表和內部臨時表。外部臨時表是通過語句create temporary table...創建的臨時表,臨時表只在本會話有效,會話斷開后,臨時表數據會自動清理。內部臨時表主要有兩類,一類是information_schema中臨時表,另一類是會話執行查詢時,如果執行計划中包含有“Using temporary”時,會產生臨時表。內部臨時表與外部臨時表的一個區別在於,我們看不到內部臨時表的表結構定義文件frm。而外部臨時表的表定義文件frm,一般是以#sql{進程id}_{線程id}_序列號組成,因此不同會話可以創建同名的臨時表。
臨時表與普通表的主要區別在於是否在實例,會話,或語句結束后,自動清理數據。比如,內部臨時表,我們在一個查詢中,如果要存儲中間結果集,而查詢結束后,臨時表就會自動回收,不會影響用戶表結構和數據。另外就是,不同會話的臨時表可以重名,所有多個會話執行查詢時,如果要使用臨時表,不會有重名的擔憂。5.7引入了臨時表空間后,所有臨時表都存儲在臨時表空間(非壓縮)中,臨時表空間的數據可以復用。臨時表並非只支持Innodb引擎,還支持myisam引擎,memory引擎等。因此,臨時表我們看不到實體(idb文件),但其實不一定是內存表,也可能存儲在臨時表空間中。
臨時表既可以innodb引擎表,也可以是memory引擎表。這里所謂的內存表,是說memory引擎表,通過建表語句create table ...engine=memory,數據全部在內存,表結構通過frm管理,同樣的內部的memory引擎表,也是看不到frm文件中,甚至看不到information_schema在磁盤上的目錄。在MySQL內部,information_schema里面的臨時表就包含兩類:innodb引擎的臨時表和memory引擎的臨時表。比如TABLES表屬於memory臨時表,而columns,processlist,屬於innodb引擎臨時表。內存表所有數據都在內存中,在內存中數據結構是一個數組(堆表),所有數據操作都在內存中完成,對於小數據量場景,速度比較快(不涉及物理IO操作)。但內存畢竟是有限的資源,因此,如果數據量比較大,則不適合用內存表,而是選擇用磁盤臨時表(innodb引擎),這種臨時表采用B+樹存儲結構(innodb引擎),innodb的bufferpool資源是共享的,臨時表的數據可能會對bufferpool的熱數據有一定的影響,另外,操作可能涉及到物理IO。memory引擎表實際上也是可以創建索引的,包括Btree索引和Hash索引,所以查詢速度很快,主要缺陷是內存資源有限。
5、官網信息
Property |
Value |
Command-Line Format | --tmp-table-size=# |
System Variable | tmp_table_size |
Scope | Global, Session |
Dynamic | Yes |
Type | integer |
Default Value | 16777216 |
Minimum Value | 1024 |
Maximum Value | 18446744073709551615 |
The maximum size of internal in-memory temporary tables. This variable does not apply to user-
created MEMORY tables.
The actual limit is determined from whichever of the values of tmp_table_size and
max_heap_table_size is smaller. 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 (and
max_heap_table_size if necessary) if you do many advanced GROUP BY queries and you have
lots of memory.
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 and
Created_tmp_tables variables.
6、針對報錯信息:Table '/mysql/data3001/tmp/#sql_ca3c_0' is marked as crashed and should be repaired