mysql innodb temporary table btmp1 file is too large

One day, the production environment (database instance) alarms, and the disk usage rate is too high!

The inspection found that the ibtmp1 file in the mysql data directory was too large, reaching 30GB

1. What does ibtmp1 file do?

mysql official document: https://dev.mysql.com/doc/refman/5.7/en/innodb-temporary-tablespace.html

It is used to store the data when querying the temporary table.

 

2. What is the reason for the growth of ibtmp1?

Mainly related to SQL, especially a large number of grouping aggregation, sorting, join query SQL.

Usually the following conditions will cause iptmp1 to rise:

  1. The query statement will first query the amount of temp_table_size (memory allocation), and when the amount of temporary storage exceeds this parameter limit, it will apply for space in iptmp1.
  2. When the select order group by GROUP BY has no index field or the clause fields of group by + order by are different.
  3. select (select) subquery
  4. insert into select ... from ... table data copy
  5. select union select union statement
     

Note: After the temporary table is released, the space will be released, but the disk space will not be released, and the free space can be reused. Freeing up disk space can only be restarted

 

Three, the solution

1. Check sql!

Find the slow SQL through the slow query log (the SQL that contains the sub-query focuses on), make sure that the result set in the sub-query is not too large (returning too many rows), and you can reduce the result set through the where condition of the sub-query.

Or query through show processlist, as shown in the figure:

I am here because sql is too slow and the result set of the subquery is too large, which causes the mysql thread to get stuck. After optimization, the file size of iptmp1 is normal.

2. My.cnf configures temporary file size limit

[mysqld]
innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:500M

It is not recommended, because once the temporary file grows to 500M, if you perform SQL queries (such as subqueries) that require temporary tables, an error will be reported!

 

 

 

Guess you like

Origin blog.csdn.net/sumengnan/article/details/113135843