是否使用“CREATE TEMPORARY TABLE”创建的表?在内存中还是在磁盘上?

发布于 2024-12-06 21:35:25 字数 128 浏览 1 评论 0原文

MySQL中,当您创建临时表时,例如CREATE TEMPORARY TABLE ...,该表是创建并保存在内存中还是磁盘上?

我已经阅读了文档并用谷歌搜索了它,但还没有找到答案。

In MySQL, when you create a temporary table, for example, CREATE TEMPORARY TABLE ..., is that table created and held in memory or on the disk?

I have read through the docs and Google'd it and have not come up with an answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坠似风落 2024-12-13 21:35:25

这取决于您指定的引擎。默认情况下,表数据将存储在磁盘上。如果指定MEMORY引擎,数据将只存储在内存中。

应该可以实际找到创建临时表时在文件系统中创建的文件。运行以下命令后:

CREATE TABLE test.table_myisam (x int) ENGINE=MyISAM;
CREATE TABLE test.table_memory (x int) ENGINE=MEMORY;
CREATE TEMPORARY TABLE test.temp_table_myisam (x int) ENGINE=MyISAM;
CREATE TEMPORARY TABLE test.temp_table_memory (x int) ENGINE=MEMORY;

然后检查目录:C:\ProgramData\MySQL\MySQL Server 5.5\data\test(在 Windows 上),存在的文件为:

table_innodb.frm   # Table definition.
table_innodb.MYD   # MyISAM table data file.
table_innodb.MYI   # MyISAM table index file.

table_memory.frm   # No MYD or MYI file for the MEMORY engine.

临时表存储在 C:\Windows\Temp 中并具有不常见的名称,但内部数据以相同的方式存储。

#sql9a0_7_d.frm    # This is the MyISAM temporary table.
#sql9a0_7_d.MYD    # MyISAM data file for temporary table.
#sql9a0_7_d.MYI    # MyISAM index file for temporary table.

#sql9a0_7_c.frm    # This is the MEMORY engine file. No MYD or MYI.

It depends on what engine you specify. By default the table data will be stored on disk. If you specify the MEMORY engine, the data will only be stored in memory.

It should be possible to actually find the files that are created in the filesystem when the temporary tables are created. After running the following commands:

CREATE TABLE test.table_myisam (x int) ENGINE=MyISAM;
CREATE TABLE test.table_memory (x int) ENGINE=MEMORY;
CREATE TEMPORARY TABLE test.temp_table_myisam (x int) ENGINE=MyISAM;
CREATE TEMPORARY TABLE test.temp_table_memory (x int) ENGINE=MEMORY;

I then checked the directory: C:\ProgramData\MySQL\MySQL Server 5.5\data\test (on Windows) and the files present were:

table_innodb.frm   # Table definition.
table_innodb.MYD   # MyISAM table data file.
table_innodb.MYI   # MyISAM table index file.

table_memory.frm   # No MYD or MYI file for the MEMORY engine.

The temporary tables are stored in C:\Windows\Temp and have unusual names, but internally the data is stored in the same way.

#sql9a0_7_d.frm    # This is the MyISAM temporary table.
#sql9a0_7_d.MYD    # MyISAM data file for temporary table.
#sql9a0_7_d.MYI    # MyISAM index file for temporary table.

#sql9a0_7_c.frm    # This is the MEMORY engine file. No MYD or MYI.
耀眼的星火 2024-12-13 21:35:25

就像马克所说,这取决于你告诉它使用什么引擎。如果您不提供引擎,它会做“某事”,但不一定将其保留在内存中。如果你想强制表位于内存中,你必须显式定义它:

CREATE TEMPORARY TABLE foobar (id int) ENGINE=MEMORY;

但是,MEMORY-engine 的使用受到限制。有关更多信息,请参阅 内部临时表使用MySQL

Like Mark said, it depends on what ENGINE you tell it to use. If you don't give an ENGINE, it will do "something", but not necessarily keep it just in memory. If you want to force the table to be in memory, you have to define it explicitly:

CREATE TEMPORARY TABLE foobar (id int) ENGINE=MEMORY;

However, the use of MEMORY-engine is restricted. For more information have a look at Internal Temporary Table Use in MySQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文