MySQL 临时表是共享资源吗?

2021-12-28 00:00:00 concurrency mysql temp-tables

我有一个使用临时表的 MySQL 存储过程.假设我的表名是temp",我用它来存储一些中间数据.它会在程序开始时创建,并在程序结束时删除.

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;

问题是这个存储过程可能被不同的用户同时使用,所以我想知道这是否会导致任何问题(即临时表中插入数据的任何冲突).换句话说,临时表是对同一 SP 的不同调用中的共享资源吗?

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?

推荐答案

不,临时表仅限于您的数据库连接范围.在同一个数据库连接期间,您可以在对过程的后续调用中使用临时表,但其他连接无法访问它.他们可以创建一个同名的表,但每个临时表都是独立的.当您关闭连接时,临时表会消失.

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.

相关文章