Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is MySQL Temporary table a shared resource?

I have a MySQL stored procedure that uses a temporary table. Assume that my table name is 'temp' and I use it to store some middle data. It will create at the beginning of procedure, and will drop at the end.

CREATE PROCEDURE p() BEGIN  CREATE TEMPORARY TABLE \`temp\`(...);  INSERT INTO \`temp\` VALUES(...);  DROP TEMPORARY TABLE \`temp\`;  END; 

The problem is that this stored procedure may be used by different users concurrently, so I want to know if this can cause any problems (i.e. any conflict in inserted data in temp table). In other word is temp table a shared resource within different calls to the same SP?

like image 268
Ehsan Khodarahmi Avatar asked Jul 04 '11 05:07

Ehsan Khodarahmi


People also ask

Are temp tables shared?

According to my knowledge if a temporary table is called from different processes they will only be shared if they are defined using a double hash ##Table. Otherwise they are only visible to the current process and processes spawned by that process.

What is MySQL temporary table?

In MySQL, a temporary table is a special type of table that allows you to store a temporary result set, which you can reuse several times in a single session. A temporary table is very handy when it is impossible or expensive to query data that requires a single SELECT statement with the JOIN clauses.

Where are MySQL temporary tables stored?

An internal temporary table can be held in memory and processed by the MEMORY storage engine, or stored on disk by the InnoDB or MyISAM storage engine. If an internal temporary table is created as an in-memory table but becomes too large, MySQL automatically converts it to an on-disk table.

What is the difference between view and temporary table in MySQL?

Views are stored queries for existing data in existing tables. Temporary table needs to be populated first with data, and population is the main preformance-concerned issue. So the data in views already exists and so views are faster than temporary table.


1 Answers

No, a temp table is limited to the scope of your database connection. You can use the temp table in subsequent calls to the procedure during the same database connection, but other connections cannot access it. They can create a table by the same name, but each temp table will be independent. Temp tables go away when you close your connection.

like image 175
Bill Karwin Avatar answered Sep 27 '22 17:09

Bill Karwin