gpt4 book ai didi

mysql - 存储过程中的 MySQL 临时表与服务器的一次连接的最佳方法

转载 作者:行者123 更新时间:2023-11-29 18:28:56 25 4
gpt4 key购买 nike

根据MySQL引用手册

You can use the TEMPORARY keyword when creating a table. A TEMPORARY table is visible only within the current session, and is dropped automatically when the session is closed. This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.)

由于服务器(在我的场景中为 NodeJS)仅维护与服务器的一个连接,因此它肯定会与 中使用的 TEMPORARY TABLES 发生冲突(对不同的过程调用使用相同的临时表)当对同一个存储过程进行异步调用时,存储过程对吗?

如果是,克服这个问题的最佳方法是什么?随机表名?

最佳答案

解决方案是在临时表中使用额外的字段并使用随机数:

declare rnd float;

create temporary table if not exists temporary_table(
...
rand_number float
);

set rnd = rand();

insert into temporary_table (... ,rand_number)
values (... , rnd);

/*do other stuffs using always rand_number field */


/*finally clear the temporary data*/


delete from temporary_table
where rand_number = rnd;

rand() 可能会返回一个现有值,您可以检查是否存在以创建另一个值

关于mysql - 存储过程中的 MySQL 临时表与服务器的一次连接的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45892713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com