MySQL临时表是共享资源吗?
我有一个使用临时表的 MySQL 存储过程。假设我的表名称是“temp”,我用它来存储一些中间数据。它将在程序开始时创建,并在程序结束时删除。
CREATE PROCEDURE p()
BEGIN
CREATE TEMPORARY TABLE \`temp\`(...);
INSERT INTO \`temp\` VALUES(...);
DROP TEMPORARY TABLE \`temp\`;
END;
问题是这个存储过程可能被不同的用户同时使用,所以我想知道这是否会导致任何问题(即临时表中插入的数据有任何冲突)。换句话说,临时表是对同一 SP 的不同调用中的共享资源吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,临时表仅限于数据库连接的范围。您可以在同一数据库连接期间对过程的后续调用中使用临时表,但其他连接无法访问它。他们可以创建同名的表,但每个临时表都是独立的。当您关闭连接时,临时表就会消失。
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.
临时表仅对当前会话可见。
因此,如果您同时有多个会话 - 每个会话都将拥有自己独立的同名临时表。
文档: http://dev.mysql.com/doc/refman /5.1/en/create-table.html, ctrl+f 表示“您可以使用临时表”
Temporary table is visible only for current session.
So if you have several simultaneuous sessions - each one will have its own independent temporary table with the same name.
Documentation: http://dev.mysql.com/doc/refman/5.1/en/create-table.html, ctrl+f for "You can use the TEMPORARY"