我在python3上开发了一个应用程序,它使用tornado框架连接到一个巨大的mysql数据库,里面有数百万行。
这个数据库是实时同步的,所以数据被插入、删除和更新,所以创建了3个触发器insert、update和delete。
但是触发器的行为非常奇怪,有时起作用,有时不起作用,比如当他们决定接收数据并做他们必须做的事情时,其他时候他们只是忽略它。
mysql版本是5.7.23-0ubuntu0.16.04.1
我在数据库中插入了3个触发器,可能是触发器中的逻辑错误:
插入
BEGIN
IF (SELECT count(*) FROM USERS_STATS WHERE LOGIN = NEW.LOGIN) = 0 THEN
INSERT INTO USERS_STATS (LOGIN) values (NEW.LOGIN);
END IF;
UPDATE USERS_STATS SET
TOTAL_DEPOSIT = TOTAL_DEPOSIT + IF(NEW.CMD = 6 AND NEW.COMMENT NOT LIKE '%internal%' AND NEW.COMMENT NOT LIKE '%Tr %' AND NEW.COMMENT NOT LIKE '%agent%' AND NEW.COMMENT NOT LIKE '%rebate%' AND NEW.COMMENT NOT LIKE '%MAM:CASH%' AND NEW.PROFIT > 0, NEW.PROFIT, 0),
TOTAL_WITHDRAW = TOTAL_WITHDRAW + IF(NEW.CMD = 6 AND NEW.COMMENT NOT LIKE '%internal%' AND NEW.COMMENT NOT LIKE '%Tr %' AND NEW.COMMENT NOT LIKE '%agent%' AND NEW.COMMENT NOT LIKE '%rebate%' AND NEW.COMMENT NOT LIKE '%MAM:CASH%' AND NEW.PROFIT < 0, NEW.PROFIT, 0),
TOTAL_VOLUME = TOTAL_VOLUME + IF(NEW.CMD IN(0, 1) AND NEW.CLOSE_TIME > '1970-01-01', NEW.VOLUME, 0),
TOTAL_REBATE = TOTAL_REBATE + IF((NEW.COMMENT LIKE '%agent%' OR NEW.COMMENT LIKE '%rebate%' OR NEW.COMMENT LIKE '%MAM:CASH%'), ROUND(NEW.PROFIT, 2), 0)
WHERE LOGIN = NEW.LOGIN;
END
更新
BEGIN
DECLARE old_is_deposit integer;
DECLARE new_is_deposit integer;
DECLARE old_is_withdraw integer;
DECLARE new_is_withdraw integer;
DECLARE old_is_rebate integer;
DECLARE new_is_rebate integer;
SET old_is_deposit := OLD.CMD = 6 AND OLD.COMMENT NOT LIKE '%internal%' AND OLD.COMMENT NOT LIKE '%Tr %' AND OLD.COMMENT NOT LIKE '%agent%' AND OLD.COMMENT NOT LIKE '%rebate%' AND OLD.COMMENT NOT LIKE '%MAM:CASH%' AND OLD.PROFIT > 0;
SET new_is_deposit := NEW.CMD = 6 AND NEW.COMMENT NOT LIKE '%internal%' AND NEW.COMMENT NOT LIKE '%Tr %' AND NEW.COMMENT NOT LIKE '%agent%' AND NEW.COMMENT NOT LIKE '%rebate%' AND NEW.COMMENT NOT LIKE '%MAM:CASH%' AND NEW.PROFIT > 0;
SET old_is_withdraw := OLD.CMD = 6 AND OLD.COMMENT NOT LIKE '%internal%' AND OLD.COMMENT NOT LIKE '%Tr %' AND OLD.COMMENT NOT LIKE '%agent%' AND OLD.COMMENT NOT LIKE '%rebate%' AND OLD.COMMENT NOT LIKE '%MAM:CASH%' AND OLD.PROFIT < 0;
SET new_is_withdraw := NEW.CMD = 6 AND NEW.COMMENT NOT LIKE '%internal%' AND NEW.COMMENT NOT LIKE '%Tr %' AND NEW.COMMENT NOT LIKE '%agent%' AND NEW.COMMENT NOT LIKE '%rebate%' AND NEW.COMMENT NOT LIKE '%MAM:CASH%' AND NEW.PROFIT < 0;
SET old_is_rebate := OLD.COMMENT LIKE '%agent%' OR OLD.COMMENT LIKE '%rebate%' OR OLD.COMMENT LIKE '%MAM:CASH%';
SET new_is_rebate := NEW.COMMENT LIKE '%agent%' OR NEW.COMMENT LIKE '%rebate%' OR NEW.COMMENT LIKE '%MAM:CASH%';
IF (SELECT count(*) FROM USERS_STATS WHERE LOGIN = NEW.LOGIN) = 0 THEN
INSERT INTO USERS_STATS (LOGIN) values (NEW.LOGIN);
END IF;
IF (old_is_deposit = 1 AND new_is_deposit = 1 AND OLD.PROFIT != NEW.PROFIT) THEN
UPDATE USERS_STATS SET
TOTAL_DEPOSIT = TOTAL_DEPOSIT + (NEW.PROFIT - OLD.PROFIT)
WHERE LOGIN = OLD.LOGIN;
ELSEIF (old_is_withdraw = 1 AND new_is_withdraw = 1 AND OLD.PROFIT != NEW.PROFIT) THEN
UPDATE USERS_STATS SET
TOTAL_WITHDRAW = TOTAL_WITHDRAW + (NEW.PROFIT - OLD.PROFIT)
WHERE LOGIN = OLD.LOGIN;
ELSEIF (old_is_deposit = 1 AND new_is_withdraw = 1) THEN
UPDATE USERS_STATS SET
TOTAL_DEPOSIT = TOTAL_DEPOSIT - OLD.PROFIT,
TOTAL_WITHDRAW = TOTAL_WITHDRAW + NEW.PROFIT
WHERE LOGIN = OLD.LOGIN;
ELSEIF (old_is_withdraw = 1 AND new_is_deposit = 1) THEN
UPDATE USERS_STATS SET
TOTAL_DEPOSIT = TOTAL_DEPOSIT + NEW.PROFIT,
TOTAL_WITHDRAW = TOTAL_WITHDRAW - OLD.PROFIT
WHERE LOGIN = OLD.LOGIN;
ELSEIF (old_is_deposit = 0 AND new_is_deposit = 1) THEN
UPDATE USERS_STATS SET
TOTAL_DEPOSIT = TOTAL_DEPOSIT + NEW.PROFIT
WHERE LOGIN = OLD.LOGIN;
ELSEIF (old_is_deposit = 1 AND new_is_deposit = 0) THEN
UPDATE USERS_STATS SET
TOTAL_DEPOSIT = TOTAL_DEPOSIT - OLD.PROFIT
WHERE LOGIN = OLD.LOGIN;
ELSEIF (old_is_withdraw = 0 AND new_is_withdraw = 1) THEN
UPDATE USERS_STATS SET
TOTAL_WITHDRAW = TOTAL_WITHDRAW + NEW.PROFIT
WHERE LOGIN = OLD.LOGIN;
ELSEIF (old_is_withdraw = 1 AND new_is_withdraw = 0) THEN
UPDATE USERS_STATS SET
TOTAL_WITHDRAW = TOTAL_WITHDRAW - OLD.PROFIT
WHERE LOGIN = OLD.LOGIN;
END IF;
IF (OLD.VOLUME != NEW.VOLUME) THEN
UPDATE USERS_STATS SET
TOTAL_VOLUME = TOTAL_VOLUME + (NEW.VOLUME - OLD.VOLUME)
WHERE LOGIN = OLD.LOGIN;
END IF;
IF (old_is_rebate = 1 AND new_is_rebate = 1 AND OLD.PROFIT != NEW.PROFIT) THEN
UPDATE USERS_STATS SET
TOTAL_REBATE = TOTAL_REBATE + (ROUND(NEW.PROFIT, 2) - ROUND(OLD.PROFIT, 2))
WHERE LOGIN = OLD.LOGIN;
ELSEIF (old_is_rebate = 0 AND new_is_rebate = 1) THEN
UPDATE USERS_STATS SET
TOTAL_REBATE = TOTAL_REBATE + ROUND(NEW.PROFIT, 2)
WHERE LOGIN = OLD.LOGIN;
ELSEIF (old_is_rebate = 1 AND new_is_rebate = 0) THEN
UPDATE USERS_STATS SET
TOTAL_REBATE = TOTAL_REBATE - ROUND(OLD.PROFIT, 2)
WHERE LOGIN = OLD.LOGIN;
END IF;
END
删除
BEGIN
IF (SELECT count(*) FROM USERS_STATS WHERE LOGIN = OLD.LOGIN) = 0 THEN
INSERT INTO USERS_STATS (LOGIN) values (OLD.LOGIN);
END IF;
UPDATE USERS_STATS SET
TOTAL_DEPOSIT = TOTAL_DEPOSIT - IF(OLD.CMD = 6 AND OLD.COMMENT NOT LIKE '%internal%' AND OLD.COMMENT NOT LIKE '%Tr %' AND OLD.COMMENT NOT LIKE '%agent%' AND OLD.COMMENT NOT LIKE '%rebate%' AND OLD.COMMENT NOT LIKE '%MAM:CASH%' AND OLD.PROFIT > 0, OLD.PROFIT, 0),
TOTAL_WITHDRAW = TOTAL_WITHDRAW - IF(OLD.CMD = 6 AND OLD.COMMENT NOT LIKE '%internal%' AND OLD.COMMENT NOT LIKE '%Tr %' AND OLD.COMMENT NOT LIKE '%agent%' AND OLD.COMMENT NOT LIKE '%rebate%' AND OLD.COMMENT NOT LIKE '%MAM:CASH%' AND OLD.PROFIT < 0, OLD.PROFIT, 0),
TOTAL_VOLUME = TOTAL_VOLUME - IF(OLD.CMD IN(0, 1) AND OLD.CLOSE_TIME > '1970-01-01', OLD.VOLUME, 0),
TOTAL_REBATE = TOTAL_REBATE - IF((OLD.COMMENT LIKE '%agent%' OR OLD.COMMENT LIKE '%rebate%' OR OLD.COMMENT LIKE '%MAM:CASH%'), ROUND(OLD.PROFIT, 2), 0)
WHERE LOGIN = OLD.LOGIN;
END
我还附上了触发器的屏幕截图:
为什么我的触发器不能正常工作需要帮助。先谢谢你。
1条答案
按热度按时间ix0qys7i1#
所以最后我发现了一个不同的逻辑,在我的例子中,它不能从死锁中逃脱,所以我可以用cronjob脚本代替上面的触发器,在触发器执行的一个时间间隔内更新所有必要的数据。