Why does MySQL produce so many temporary MYD files?

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.

Why does MySQL produce so many temporary MYD files?


up vote 3 down vote favorite

3

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?

mysql temp-tables


shareimprove this question

edited Dec 17 ‘12 at 23:34

RolandoMySQLDBA
63.4k1062148

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

add a comment

3 Answers

active
oldest
votes

up vote
6
down vote

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 !!!


shareimprove this answer

answered Dec 17 ‘12 at 23:19

RolandoMySQLDBA
63.4k1062148

 

add a comment

up vote
1
down vote

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.


shareimprove this answer

answered Dec 16 ‘12 at 5:50

Valerie Parham-Thompson
38625

 

add a comment

up vote
0
down vote

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.


shareimprove this answer

answered Dec 16 ‘12 at 13:48

Vinay
358

 

add a comment

Your Answer

时间: 2024-11-08 21:34:02

Why does MySQL produce so many temporary MYD files?的相关文章

MySQL调优 —— Using temporary

DBA发来一个线上慢查询问题, SQL如下(为突出重点省略部分内容): select distinct article0_.id, 等字段 from article_table article0_, hits_table articlehit1_ where article0_.id=articlehit1_.id order by hits; EXPLAIN结果:耗时4.03S 出乎意料, 竟然会有Using temporary, order by只用到了一张表, 正常情况下不会出现借助辅助表

mysql using filesort Using temporary

using filesort 一般人的回答是: "当行数据太大,导致内存无法容下这些数据产生的临时表时,他们就会被放入磁盘中排序."  很不幸,这个答案是错的 ,临时表在太大的时候确实会到磁盘离去,但是EXPLAIN不会显示这些. The truth is, filesort is badly named. Anytime a sort can't be performed from an index, it's a filesort. It has nothing to do wit

关于mysql 删除数据后(.MYD,MYI)物理空间未释放

关于mysql 删除数据后物理空间未释放 OPTIMIZE TABLE 当您的库中删除了大量的数据后,您可能会发现数据文件尺寸并没有减小.这是因为删除操作后在数据文件中留下碎片所致.OPTIMIZE TABLE 是指对表进行优化.如果已经删除了表的一大部分数据,或者如果已经对含有可变长度行的表(含有 VARCHAR . BLOB 或 TEXT 列的表)进行了很多更改,就应该使用 OPTIMIZE TABLE 命令来进行表优化.这个命令可以将表中的空间碎片进行合并,并且可以消除由于删除或者更新造成

PHP with MySQL essential training-10.5including and require files

1# 主要讲了四个外接文件的函数,分别是include,include_once,require,require_once. include and include_once :if there is something wrong,they will not throw an error, they will just be ingored. requre and require_once :if there is something wrong ,thye will throw a fata

Chapter 5 MySQL Server Administration_1

Chapter 5 MySQL Server Administration Table of Contents 5.1 The MySQL Server 5.1.1 Configuring the Server 5.1.2 Server Configuration Defaults 5.1.3 Server Option and Variable Reference 5.1.4 Server Command Options 5.1.5 Server System Variables 5.1.6

MYSQL术语表

MYSQL术语表 http://dev.mysql.com/doc/refman/5.6/en/glossary.html MySQL Glossary These terms are commonly used in information about the MySQL database server. This glossary originated as a reference for terminology about the InnoDB storage engine, and th

MySQL 5.6 Reference Manual-14.4 InnoDB Configuration

14.4 InnoDB Configuration 14.4.1 InnoDB Initialization and Startup Configuration 14.4.2 Configuring InnoDB for Read-Only Operation 14.4.3 InnoDB Buffer Pool Configuration 14.4.4 Configuring the Memory Allocator for InnoDB 14.4.5 Configuring InnoDB Ch

MySQL学习总结(摘抄)

1.数据库概述 简 而言之,数据库(DataBase)就是一个存储数据的仓库.为了方便数据的存储和管理,将数据按照特定的规律存储在磁盘上.通过数据库管理系统,可以有 效的组织和管理存储在数据库中的数据.如今,已经存在的Oracle.SQLServer.MySQL等诸多优秀的数据库. 详解内容: 数据存储方式 数据库在开发中的作用 数据库访问技术 MySQL数据库的介绍 数据库泛型 SQL语言 常见数据库系统 如果学习数据库 1.1 数据库理论基础 数据库能够将数据按照特定的规律组织起来.那么,数

MySQL 採用Xtrabackup对数据库进行全库备份

1,xtrabackup简单介绍 关于数据库备份以及备份工具.參考:http://blog.itpub.net/26230597/viewspace-1460065/,这里来介绍xtrabackup已经怎样使用xtrabackup进行对数据库的全备份已经恢复. 2,xtrabackup下载 下载地址为:http://www.percona.com/downloads/XtraBackup/ Chorm浏览器以下.在"DOWNLOADPERCONA XTRABACKUP"以下,选择版本号