http://dba.stackexchange.com/questions/30505/why-does-mysql-produce-so-many-temporary-myd-files

Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

On a Debian Linux server, hosting many PHP/MySQL websites (photo galleries), sometimes I have "many" files like /tmp/#sql_6405_58.MYD.

For example today :

[2012-12-15 15:18:11] /tmp/#sql_6405_6.MYD : 88MB
[2012-12-15 15:18:11] /tmp/#sql_6405_3.MYD : 22MB
[2012-12-15 15:18:11] /tmp/#sql_6405_4.MYD : 138MB
[2012-12-15 15:18:11] /tmp/#sql_6405_10.MYD : 88MB
...
[2012-12-15 15:18:11] /tmp/#sql_6405_9.MYD : 15MB
[2012-12-15 15:18:11] /tmp/#sql_6405_65.MYD : 49MB
[2012-12-15 15:18:11] /tmp/#sql_6405_44.MYD : 69MB

(59 files at the same time, for more than 6GB... yes I monitor big files in /tmp)

Unfortunately, /tmp is on the same partition than / and it temporary breaks the web server, because / is full I suppose. Then files disappear and the server is back to normal.

All the file names follow the #sql_6405_*.MYD pattern. I would like to understand which MySQL operation implies so many temporary files. I have approximately 2000 databases on this server. Is it possible to know which database is concerned?

asked Dec 15 '12 at 20:21
 
plegall

migrated from stackoverflow.com Dec 16 '12 at 2:08

This question came from our site for professional and enthusiast programmers.

 
1  
I'm guessing they're temp data for large operations that use filesort, and 6405 is the PID of mysql to keep temp files from different server instances separate.
– 
Marc B
Dec 15 '12 at 20:27
    
Should I ask an admin to move my question?
– 
plegall
Dec 15 '12 at 22:06

3 Answers

There are some options that can cause temp tables to materialize as
MyISAM tables or can be configured to delay it. Keep in mind that for
disk-based temp tables, there are no .frm files, but only .MYD and .MYI files (of course. the .MYI file is never used since it is impossible index an internal temp table).

Here are the options:

You should also consider the MySQL Documentation on Internal Temp Table Usage

The situations where in-memory temp tables are made are

  • If there is an ORDER BY clause and a different GROUP BY clause, or
    if the ORDER BY or GROUP BY contains columns from tables other than the
    first table in the join queue, a temporary table is created.
  • DISTINCT combined with ORDER BY may require a temporary table.
  • 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.

When an in-memory temp table exceeded the minimum of (tmp_table_size or max_heap_table_size), mysqld does the following:

  • Suspends the query
  • Copies the in-memory table's contents into a MyISAM temp table
  • Discards the in-memory table
  • Continues the query, sending the temp data into the MyISAM temp table

The situations where in-memory temp tables are bypassed in favor of disk are

  • Presence of a BLOB or TEXT column in the table
  • Presence of any column in a GROUP BY or DISTINCT clause larger than 512 bytes
  • Presence of any column larger than 512 bytes in the SELECT list, if UNION or UNION ALL is used

Some due diligence is required to reduce temp table creation on disk

  • Setting join_buffer_size bigger
  • Setting sort_buffer_size bigger
  • Setting tmp_table_size and max_heap_table_size bigger
  • Tuning queries to minimize or even prevent temp tables
  • Creating indexes to create presorted view of data from individual tables
  • Installing additional RAM to accommodate large in-memory temp tables

If after such due diligence, there are still temp tables being formed
on Disk, here is one desperate move: Mapping disk-based temp table
creation to memory.

Here is a quick-and-dirty way to set up a 16GB RAM Disk using tmpdir

STEP01) Create RAM Disk Folder

mkdir /var/mysql_tmpfs

STEP02) Add this to my.cnf

[mysqld]
tmpdir=/var/mysql_tmpfs

STEP03) Add this to /etc/fstab

echo "none /var/mysql_tmpfs tmpfs defaults,size=16g 1 2" >> /etc/fstab

STEP04) Reload /etc/fstab

mount -a

STEP05) service mysql restart

After this, all temp table that become MyISAM are written to the RAM Disk. This should speed disk-based temp table creation.

Give it a Try !!!

answered Dec 17 '12 at 23:19
RolandoMySQLDBA
63.4k1062148
 

These are queries that are rolling over onto disk because the results are too large for memory.

When the query is done, the space clears.

There's no way to match these temp files definitively to queries, but
you can get clues to make a good guess from SHOW FULL PROCESSLIST; or
SHOW INNODB STATUS; or by looking in your error log if the queries fail.

answered Dec 16 '12 at 5:50

 

Whenever we use alter statements on table It creates the
#sql_6405_3.MYD temporay files and once it's done throws the output and
disappears.

Alter on tables make MySQL to copy whole data into temporary files
#sql.xxx.MYD and make changes to created temporary files then drop
original data files tablename.MYD and renames the temporay files to
table name.

Also for some kinda sorting queries it creates temporary files.

As I traced out. This thing happens.

answered Dec 16 '12 at 13:48

Vinay
358
 

Your Answer

最新文章

  1. SSH项目里面 忘记密码的邮件发送功能
  2. MySQL pdo预处理能防止sql注入的原因
  3. Qt 环境下的activex控件编程-------1
  4. Windows 上使用 cygwin 连接到 docker toolbox
  5. 软件测试技术(五)——Software Review
  6. Linux 学习笔记 文件权限
  7. Logstash conf.d 多个配置文件
  8. java带图片的邮件发送方法实现
  9. 使用HAProxy、PHP、Redis和MySQL支撑每周10亿请求
  10. PHP入门,clone和__clone
  11. Java经典编程题50道之二十七
  12. HashMap和Hashtable的异同点
  13. 20190211 模拟训练 A. 大猫咪
  14. Spring再接触 Annotation part1
  15. python with as 的用法
  16. 检测Android手机的IP地址
  17. 【转载】基于MFC的ActiveX控件开发(2)
  18. 关于zip伪加密
  19. HDU 1999 不可摸数 (模拟)
  20. Mimiktaz抓取本机密码

热门文章

  1. bzoj 1753: [Usaco2005 qua]Who's in the Middle【排序】
  2. [ZJOI2006]Book书架
  3. BFS POJ 2251 Dungeon Master
  4. 关于ds添加datarow
  5. (转)全文检索技术学习(二)——配置Lucene的开发环境
  6. acedssget F 方式
  7. ThinkPHP---rbac权限管理
  8. java的标识符和关键词
  9. react-router 4.x 路由按需加载
  10. 【计算几何】二维凸包——Graham's Scan法