昨天写了一个存储过程,大概是这样的
DROP PROCEDURE pr_backstate_update_temp_tbl_to_real_tbl;
CREATE DEFINER=`root`@`%` PROCEDURE `pr_backstate_update_temp_tbl_to_real_tbl`()
BEGIN
declare _error tinyint default 0;
declare continue handler for sqlexception set _error = 1;
start transaction;
#更新游戏表。
update game_cfg_tbl a, temp_game_config_tbl b set a.system_tax = b.system_tax,
a.prize_pool_tax = b.reward_tax, a.specail_tax = b.special_tax,a.min_player = b.min_player,
a.max_player = b.max_player where a.game_id = b.game_id;
#更新房间表,房间名字在下面统一更新
update gameroom_cfg_tbl a, temp_room_config_tbl b set a.rob_multiple = b.rob_banker,
a.minimum_chip = b.base_note, a.bet_multiple = b.bet_beishu, a.area_limit = b.bet_limit
where a.game_id = b.game_id and a.room_id = b.room_id;
#更新库存表。库存名字在下面统一更新
update room_stock_value_tbl a, temp_stock_config_tbl b set a.init_value = b.init_stock,
a.cur_value = b.current_stock where a.room_id = b.room_id and a.stock_id = b.stock_id;
#更新成就表
update achieve_cfg_tbl a, temp_achieve_config_tbl b set a.achieve_name = b.achieve_name,
a.achieve_type = b.achieve_type,a.related_game_id = b.game_id,a.target = b.target
where a.achieve_id = b.achieve_id;
create temporary table if not exists temp_function_name_tbl
select main_type_id,farther_type_id,son_type_id,function_name from convention_config_tbl;
#更新渠道名字
update channel_cfg_tbl a,temp_function_name_tbl b set a.channel_name = b.function_name
where b.main_type_id = 1 and a.channel_id = b.son_type_id;
#更新游戏名
update game_cfg_tbl a, temp_function_name_tbl b set a.game_name = b.function_name
where b.main_type_id = 3 and a.game_id = b.son_type_id;
#更新房间名字
update gameroom_cfg_tbl a, temp_function_name_tbl b set a.room_name = b.function_name
where b.main_type_id = 4 and a.room_id = b.son_type_id;
#更新库存名称
update room_stock_value_tbl a,temp_function_name_tbl b set a.stock_name = b.function_name
where b.main_type_id = 5 and a.room_id = b.farther_type_id and a.stock_id = b.son_type_id;
#更新排行榜名称
update ranking_cfg_tbl a, temp_function_name_tbl b set a.ranking_name = b.function_name
where b.main_type_id = 6 and a.ranking_type = b.son_type_id;
#更新活动名称
update activity_cfg_tbl a, temp_function_name_tbl b set a.activity_name = b.function_name
where b.main_type_id = 7 and a.activity_type = b.son_type_id;
#更新支付平台名称
update pay_platform_config_tbl a, temp_function_name_tbl b set a.pay_platform_name = b.function_name
where b.main_type_id = 9 and a.pay_platform_type = b.son_type_id;
#更新支付名称
update recharge_platform_tbl a,temp_function_name_tbl b set a.pay_name = b.function_name
where b.main_type_id = 10 and a.pay_way = b.son_type_id;
#更新成就名称
update achieve_cfg_tbl a, temp_function_name_tbl b set a.achieve_name = b.function_name
where b.main_type_id = 12 and a.achieve_id = b.son_type_id;
drop table if exists temp_game_config_tbl;
drop table if exists temp_room_config_tbl;
drop table if exists temp_stock_config_tbl;
drop table if exists temp_achieve_config_tbl;
drop table if exists temp_function_name_tbl;
if _error = 0 then
COMMIT;
select 200 as code;
else
ROLLBACK;
select 500 as code;
end if;
END
这个存储过程使用到了事务。现在重点的不是事务的使用,而是叫大家注意里面我临时创建了一个临时表 temp_function_name_tbl。代码如下
create temporary table if not exists temp_function_name_tbl
select main_type_id,farther_type_id,son_type_id,function_name from convention_config_tbl;
先描述一下我遇到的问题。修改了表内容之后,然后调用这个存储过程,想实时同步多张表的数据,可是操作多次之后,会突然出现数据不对的情况,最新的数据不是我想更新的,而是前几次更新的数据,这个问题困扰了我差不多一个晚上。于是今天就开始各种找问题。
最后我在存储过程的后面,显示的调用如下代码,强制将这个临时表删除了,居然解决问题了。
drop table if exists temp_function_name_tbl;
然后上网查看了一下,说临时表是在sql没有断开的时候存在,是存在缓存的,如果不显示删除,那么有可能依然是去前几次的数据。这是因为临时表是单个会话独享的,换句话说,多个会话,都有自己的临时表,这时,如果不强制显示删除临时表,就有可能更新到别的数据了。
我上面的代码还是先判断临时表是否存在,如果不存在才会创建更新数据进去,这个也是一个坑,如果不显示删除临时表,当你重新调用存储过程的时候,那么就有可能这个临时表是存在的,这就会造成没能将最新的数据更新到临时表上来了。
补充说明,
下面这四张表不是临时表,因为可以发现不在本回话中看到创建,如果是临时表的话,这4张表是不存在的。
drop table if exists temp_game_config_tbl;
drop table if exists temp_room_config_tbl;
drop table if exists temp_stock_config_tbl;
drop table if exists temp_achieve_config_tbl;
在存储过程中创建的临时表temp_function_name_tbl若不显式删除,可能导致数据混乱。由于临时表在SQL会话未结束时保留在缓存中,多次操作后可能会读取到旧数据。解决方案是在存储过程末尾显式删除临时表,以确保每次使用的是最新数据。此外,即使检查临时表是否存在,如果不删除,再次调用存储过程时,临时表可能仍存在,从而影响数据同步。
878

被折叠的 条评论
为什么被折叠?



