数据库
首页 > 数据库> > 如果我不删除临时内存表,该表将保留多长时间(MySQL)

如果我不删除临时内存表,该表将保留多长时间(MySQL)

作者:互联网

我在MySQL中使用递归存储过程来生成名为id_list的临时表,但是我必须在后续的select查询中使用该过程的结果,因此无法在该过程中删除该临时表…

BEGIN;

/* generates the temporary table of ID's */
CALL fetch_inheritance_groups('abc123',0);

/* uses the results of the SPROC in the WHERE */
SELECT a.User_ID
FROM usr_relationships r 
INNER JOIN usr_accts a ON a.User_ID = r.User_ID 
WHERE r.Group_ID = 'abc123' OR r.Group_ID IN (SELECT * FROM id_list) 
GROUP BY r.User_ID;

COMMIT;

调用该过程时,第一个值是我想要的分支的顶级ID,第二个值是该过程在递归过程中使用的层.在递归循环之前,它检查tier = 0以及是否运行:

DROP TEMPORARY TABLE IF EXISTS id_list;
CREATE TEMPORARY TABLE IF NOT EXISTS id_list (iid CHAR(32) NOT NULL) ENGINE=memory;

所以我的问题是:如果我在过程结束时或在事务中不删除临时MEMORY表,该表将在内存中保留多长时间?会话结束后会自动删除它吗,还是只要连接打开就将它保留在内存中?

** N.B.显而易见的答案可能是在提交语句之前删除临时表,但让我暂时假设我做不到.*

有关此问题的更详细的答案,请参见:https://dba.stackexchange.com/questions/57971/how-long-will-a-temporary-memory-table-persist-if-i-dont-drop-it-mysql,所以我投票决定关闭此问题.

解决方法:

对于manual,在关闭连接时会自动删除临时表(重点是我的):

Temporary Tables

You can use the TEMPORARY keyword when creating a table. A TEMPORARY
table is visible only to the current connection, and is dropped
automatically when the connection is closed
.

标签:temp-tables,memory-table,mysql,stored-procedures
来源: https://codeday.me/bug/20191029/1963824.html