MySQL触发器-5.7.23-0ubuntu0.16.04.1-奇怪的行为并非每次都有效

时间:2018-10-10 13:44:01

标签: mysql triggers

我使用龙卷风框架在python 3上开发了一个应用程序,该框架连接到巨大的MySQL数据库,内部有数百万行。

该数据库是实时同步的,因此可以插入,删除和更新数据,因此创建了3个触发器,用于插入,更新和删除。

但是触发器的行为非常奇怪,有时它们起作用,有时却不起作用,例如当他们决定自己提取数据并执行必须做的事情时,而有时他们只是忽略它。

MySQL版本是此版本5.7.23-0ubuntu0.16.04.1

同样,我插入了数据库中的3个触发器,可能是触发器中存在逻辑错误:

插入

xcode-select --install

更新

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

我还附上了屏幕截图,说明触发器的位置: enter image description here

需要帮助,为什么我的触发器不能正常工作。 预先谢谢你。

1 个答案:

答案 0 :(得分:0)

因此,最终我发现了一种不同的逻辑,在我的情况下无法摆脱死锁,因此,我可以使用触发器在一个时间间隔内更新所有必要的数据,而不是使用cronjob脚本来执行上述触发器。 >