存储过程超过锁定等待超时尝试重新启动mysql存储过程中的事务'错误?如何汉德尔呢?

时间:2016-12-27 13:09:03

标签: mysql stored-procedures mariadb mariasql

大家好我想手动验证otp验证。因为我已经写了这个存储过程。当我开始这个代码工作正常但是一段时间后它开始给出错误。执行大约50秒需要很长时间之后它给出了错误锁等待超时。所以有人能告诉我为什么它给出这样的错误以及如何解决它?

CREATE DEFINER=`xxxxx`@`xxxx` PROCEDURE `new_mobile_authentication`(
    IN in_macID VARCHAR(500),IN in_otp INT(5),OUT in_msg VARCHAR(100))
    BEGIN
    DECLARE userCount INT(10);
    DECLARE emailID VARCHAR(100);
    DECLARE mobileNumber BIGINT(11);
    DECLARE checkmatched INT(5);

    DELETE FROM mob_user WHERE NOW()>end_time;
    SELECT COUNT(*),email,mobile,otp into userCount,emailID,mobileNumber,checkmatched FROM  mob_user WHERE  mac_id=in_macID ;
    SET @checkEmailPresent=(SELECT COUNT(*) FROM table A WHERE email_id=emailID);

    IF(userCount!=0 AND @checkEmailPresent!=0)THEN
      IF(checkmatched=in_otp)THEN

        UPDATE table A SET auth='YES',mac_id=in_macID,mobile_num=mobileNumber WHERE email=emailID;
        SET @affRow=(SELECT ROW_COUNT());
        DELETE FROM mob_user WHERE mac_id=in_macID;
        SELECT @affRow AS affRow,email FROM table A WHERE mac_id=in_macID;
      ELSE
        SELECT 'invalid otp' INTO in_msg;
      END IF;

    ELSEIF(userCount!=0 AND @checkEmailPresent=0)THEN
      IF(checkmatched=in_otp)THEN

        INSERT INTO table A(email,mobile_num,mac_id) VALUE (emailID,mobileNumber,in_macID,); 
        SET @affRow=(SELECT ROW_COUNT());
        DELETE FROM mob_user WHERE mac_id=in_macID;
        SELECT  @affRow AS affRow,email FROM table A WHERE mac_id=in_macID;
      ELSE
        SELECT 'invalid otp' INTO in_msg;
      END IF;
    ELSE
      SELECT 'session expired' INTO in_msg;
    END IF;
    END

1 个答案:

答案 0 :(得分:0)

通过添加索引和/或重新构建查询来修复超时。

mob_user需要INDEX(end_time)INDEX(mac_id)

没有SELECT COUNT(*), this, that ...

GROUP BY没有意义。使用GROUP BY也不会正常工作。你有什么期待?

SET @checkEmailPresent=(SELECT COUNT(*) FROM table A WHERE email_id=emailID)可以重写SELECT @checkEmailPresent := COUNT(*) FROM table A WHERE email_id=emailID)。请注意:=。该表需要INDEX(email_id)

IF(checkmatched=in_otp)THEN没有意义,因为checkmatched无法设定。不,SELECT没有设置它。

什么是ROW_COUNT()?我不认为它是MySQL的功能。