简体   繁体   English

MySQL 温度表位置

[英]MySQL Temp Table Location

I'm getting this error when running a query:运行查询时出现此错误:

[ERROR] [MY-013132] [Server] The table '/tmp/#sql1127b_9_0' is full!

I go into the /tmp directory but I can't see the table, I'm presuming it gets deleted when the query has finished running.我 go 进入/tmp目录但我看不到表,我假设它在查询完成运行后被删除。

So I go into the directory when the query is running (the query takes a few minutes to run - against 40 million records).因此,当查询运行时,我将 go 放入目录中(查询需要几分钟才能运行 - 针对 4000 万条记录)。 I don't see the table.我没有看到桌子。 I refresh the directory, I still don't see it.我刷新目录,我仍然没有看到它。 How can I see it?我怎么能看到它? I want to see it and establish its location so I can figure out why it's filling up when I have lots of free diskspace.我想查看它并确定它的位置,这样我就可以弄清楚为什么当我有很多可用磁盘空间时它会被填满。

I'm using MySQL 8.0.23 - I previously used MySQL 5.7.33 on the same machine with the same database and same query, never got a problem.我正在使用 MySQL 8.0.23 - 我以前在具有相同数据库和相同查询的同一台机器上使用 MySQL 5.7.33,从来没有遇到过问题。

This is my diskspace:这是我的磁盘空间:

Filesystem      Size  Used Avail Use% Mounted on
tmpfs           3.2G  3.7M  3.2G   1% /run
/dev/sdc3       215G   38G  167G  19% /
tmpfs            16G     0   16G   0% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           4.0M     0  4.0M   0% /sys/fs/cgroup
/dev/sdb6       107G   81G   21G  80% /media/Kingston_SSD_120GB
/dev/sda        459G  335G  101G  77% /media/Hitachi
/dev/sdc2        33M  7.8M   25M  24% /boot/efi
tmpfs           3.2G  120K  3.2G   1% /run/user/1001

I didn't set any diskspace for /tmp - as can be seen, I have 167GB free space on / .我没有为/tmp设置任何磁盘空间 - 可以看出,我在/上有 167GB 的可用空间。

MySQL uses a trick that has been part of POSIX systems forever. MySQL 使用了一个永远是 POSIX 系统一部分的技巧。 It opens the temp file, and immediately unlinks it.它打开临时文件,并立即取消链接。 Therefore it's not viewable in any directory listing.因此,它在任何目录列表中均不可见。 But POSIX systems like UNIX and Linux shouldn't actually remove an unlinked file while a process has an open file handle to it.但是像 UNIX 和 Linux 这样的 POSIX 系统实际上不应该在进程具有打开的文件句柄时删除未链接的文件。 So once the query using the temp table finishes, it will close the file handle, and then the OS will automatically remove the file and free the storage it was using.因此,一旦使用临时表的查询完成,它将关闭文件句柄,然后操作系统将自动删除文件并释放它正在使用的存储空间。

This is generally better than requiring the server code remember to remove the tempfile when it's done with it.这通常比要求服务器代码记住在使用完临时文件时删除它要好。 It also accounts for like the thread terminating, or mysqld crashing.它还解释了线程终止或 mysqld 崩溃之类的原因。 At least it won't leave stale temp files littering your filesystem.至少它不会让过时的临时文件乱扔文件系统。

You can view the size of unlinked files with lsof -s .您可以使用lsof -s查看未链接文件的大小。 I'll leave it to you to look up examples of how to use that command (Google is your friend here).我将留给您查找如何使用该命令的示例(Google 是您的朋友)。

It's thinly possible that a temp file uses up your 167GB of free space.临时文件很可能会用完 167GB 的可用空间。

Or it could be that the temp file only uses 8GB, but you may have 20 threads doing the same query at the same time.或者可能是临时文件仅使用 8GB,但您可能有 20 个线程同时执行相同的查询。 I have seen that happen once.我见过这种情况发生一次。

But it's probably more likely that you have a value of tmp_table_size that is constraining the size of the temp table.但您可能更有可能拥有一个限制临时表大小的tmp_table_size值。

If you hit the limit, you can raise that configuration option, either as a session variable when you need it, or globally in the my.cnf .如果达到限制,您可以提高该配置选项,或者在需要时作为 session 变量,或者在my.cnf中全局设置。

But I would first try to optimize the query.但我会首先尝试优化查询。 Why does it need to create such large temp tables?为什么需要创建这么大的临时表? Could it be optimized to examine fewer rows, or perhaps avoid creating a temp table altogether?是否可以对其进行优化以检查更少的行,或者完全避免创建临时表?

from the official documentation:来自官方文档:

On Unix, MySQL uses the value of the TMPDIR environment variable as the path name of the directory in which to store temporary files.在 Unix 上,MySQL 使用 TMPDIR 环境变量的值作为存储临时文件的目录的路径名。 If TMPDIR is not set, MySQL uses the system default, which is usually /tmp, /var/tmp, or /usr/tmp.如果没有设置 TMPDIR,MySQL 使用系统默认值,通常是 /tmp、/var/tmp 或 /usr/tmp。

so if you didnt configure TMPDIR, then your file is needed to be under /tmp, /var/tmp, or /usr/tmp所以如果你没有配置 TMPDIR,那么你的文件需要在/tmp, /var/tmp, or /usr/tmp

IF you are getting this error instantly (within a second or two), I suspect that this is a permission issue.如果您立即(在一两秒内)收到此错误,我怀疑这是一个权限问题。 But otherwise, It looks like the temporary table really filling the disk.但除此之外,看起来临时表真的填满了磁盘。

EDIT: try looking for the file while executing the query.编辑:尝试在执行查询时查找文件。 Because The file is getting deleted after the SQL error因为该文件在 SQL 错误后被删除

Also this post can help you这篇文章也可以帮助你

How can I limit the size of temporary tables? 如何限制临时表的大小?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM