当我调用过程时,收到以下错误消息:
Lock wait timeout exceeded; try restarting transaction
我的查询:
DROP PROCEDURE IF EXISTS SP_Convert_Members_to_Users_With_Credits;
CREATE PROCEDURE `SP_Convert_Members_to_Users_With_Credits`()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
START TRANSACTION ;
DROP TEMPORARY TABLE IF EXISTS tb_tmp_sp_convert_members_to_users_with_credits;
CREATE TEMPORARY TABLE tb_tmp_sp_convert_members_to_users_with_credits
(INDEX tm_converts_index (username), UNIQUE tm_unique_converts_index (username))
SELECT m.username AS username,
maddress.mob AS mobile,
m.name AS name,
m.family AS family,
m.email AS email,
m.n_code AS national_id,
m.sex AS sex,
maddress.tel AS tel,
FROM_UNIXTIME(m.birthday) AS birthday,
FROM_UNIXTIME(m.wedding_date) AS wedding_date,
FROM_UNIXTIME(m.spouse_birthday) AS wife_birthday,
maddress.address AS address,
'fa' AS lang,
'real' AS type,
102 AS country_id,
IF(maddress.province_id, (SELECT id FROM province_old_province WHERE p_id = maddress.province_id),
NULL) AS province_id,
IF(maddress.province_id, (SELECT id FROM city_old_city WHERE c_id = maddress.city_id),
NULL) AS city_id,
IF(morders.total_price, morders.total_price, 0) AS total_price_user,
c.credit AS credit,
FROM_UNIXTIME(m.reg_at) AS created_at
FROM members m
LEFT JOIN (SELECT * FROM address WHERE address != '' GROUP BY user_id) maddress
ON maddress.user_id = m.username
LEFT JOIN (SELECT SUM(totalPrice) AS total_price, user_id
FROM orders
WHERE pay_status = 1
AND order_status = 4
GROUP BY user_id) morders
ON morders.user_id = m.username
LEFT JOIN (SELECT *
FROM credit
INNER JOIN (SELECT max(cr_id) AS maxId FROM credit GROUP BY username ORDER BY cr_id) A
ON A.maxId = credit.cr_id) c ON c.username = m.username
WHERE m.level = 'customer'
ORDER BY m.user_id DESC LIMIT 1;
SELECT COUNT(*) FROM tb_tmp_sp_convert_members_to_users_with_credits INTO n;
SET i = 0;
WHILE i < n
DO
SET @username = '';
SET @credit = 0;
SET @total_price_user = 0;
SET @level_id = 1;
SELECT username, credit, total_price_user
INTO @username,@credit,@total_price_user
FROM tb_tmp_sp_convert_members_to_users_with_credits
LIMIT i,1;
SET @USER_ID = 0;
IF (EXISTS(SELECT user_id FROM Tb_Users WHERE username = @username))
THEN
SELECT user_id INTO @USER_ID FROM Tb_Users WHERE username = @username;
IF (NOT EXISTS(SELECT id FROM Tb_Factors WHERE customer_id = @USER_ID))
THEN
IF (EXISTS(SELECT * FROM Tb_Credits WHERE user_id = @USER_ID))
THEN
UPDATE Tb_Credits
SET type_id = 14,
value = @credit / 10000,
`last_value`=@credit / 10000,
creator_id = 8402
WHERE user_id = @USER_ID;
ELSE
INSERT INTO Tb_Credits (user_id, type_id, value, `last_value`, creator_id, created_at)
VALUES (@USER_ID, 14, @credit / 10000, @credit / 10000, 8402, CURRENT_TIME());
END IF;
END IF;
ELSE
SET @total_price_user_level = @total_price_user / 10000;
IF (@total_price_user_level <= 170)
THEN
SET @level_id = 1;
ELSEIF (@total_price_user_level > 170 AND @total_price_user_level <= 1600)
THEN
SET @level_id = 2;
ELSEIF (@total_price_user_level > 1600 AND @total_price_user_level <= 1700)
THEN
SET @level_id = 3;
ELSEIF (@total_price_user_level > 1700 AND @total_price_user_level <= 50000)
THEN
SET @level_id = 4;
ELSE
SET @level_id = 5;
END IF;
INSERT INTO Tb_Users(username, mobile, name, family, email, national_id, sex, tel, birthday,
wedding_date, wife_birthday, address, lang, type, country_id,
province_id, city_id, created_at, password, level_id, creator_id)
SELECT username, mobile, name, family, email, national_id, sex, tel, birthday,
wedding_date, wife_birthday, address, lang, type, country_id, province_id, city_id,
created_at, NULL, @level_id, 8402
FROM tb_tmp_sp_convert_members_to_users_with_credits
LIMIT i,1;
SET @LAST_ID_INSERTED_USER_ID = 0;
SET @LAST_ID_INSERTED_USER_ID = LAST_INSERT_ID();
INSERT INTO Tb_Credits (user_id, type_id, value, `last_value`, creator_id, created_at)
VALUES (@LAST_ID_INSERTED_USER_ID, 14, (@credit / 10000), (@credit / 10000), 8402, CURRENT_TIME());
INSERT INTO Tb_Role_User (user_id, role_id, roleable_id, roleable_type, active)
VALUES (@LAST_ID_INSERTED_USER_ID, 17, NULL, NULL, 1);
END IF;
SET i = i + 1;
END WHILE;
COMMIT;
SELECT 1;
END
我的临时表只有一行(受限制),并且成功完成了。
但我不知道我的while
还没结束(我认为)。
我使用了SET innodb_lock_wait_timeout = 1200
,但在1200秒后出现了相同的错误。