Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are tables created with "CREATE TEMPORARY TABLE" in memory or on disk?

Tags:

memory

mysql

disk

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.

like image 677
Nate Weiner Avatar asked Sep 26 '11 20:09

Nate Weiner


People also ask

Are temp tables stored on disk?

Temp tables will be stored in ram as much as possible but they spill over to disc when needed. temp tables are always stored on disk.

Are SQL temp tables in-memory?

Only if you specify ENGINE=MEMORY in the CREATE TABLE statement, the table will be in memory. Otherwise the temporary table will be created with the default storage engine, which is most likely MyISAM or INNODB, and saved on disk.

Where do temp tables get created?

The local temporary tables are created in the tempdb database with a unique name because they can be created with the same name by the other connections.

Which database is used to store temporary tables and data?

SQL Server uses the TempDB database for working storage of temporary tables and temporary stored procedures.


1 Answers

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. 
like image 189
Mark Byers Avatar answered Sep 16 '22 16:09

Mark Byers