问题
I have one store procedure, which is doing saving operation in one table. In this store procedure, I am inserting multiple records in table.
here is my table structure
Table : InviteMst
~~~~~~~~~~~~~~~~~
Name DataType Description
-------- ------------ ----------------------
InviteID Int PrimaryKey (Auto Incr)
Date Date
UserID Int (ForignKey)
EMailID varchar(50)
Guid varchar(36)
store procedure : usp_InviteMst
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CREATE PROCEDURE `usp_InviteMst`( IN n_UserID INT, IN EmailList MEDIUMTEXT )
BEGIN
DECLARE d_Date DATETIME DEFAULT NOW();
IF LENGTH(EmailList) > 0 THEN
SET @sqlQuery = "";
SET nPos = INSTR(EmailList, ",");
WHILE (nPos <> 0) DO
SET c_EmailID = TRIM(SUBSTRING(EmailList, 1,nPos - 1));
IF c_EmailID <> "" THEN
SET c_Guid = "";
//Here I am checking that If User already exists with same emailid in same date or note.
SELECT Guid INTO c_Guid
FROM InviteMst
WHERE UserID = n_UserID
AND Date = DATE(d_Date)
AND Guid IS NOT NULL
AND EMailID = c_EmailID
ORDER BY Date DESC
LIMIT 0, 1;
SET c_Guid = COALESCEc_Guid ,"");
IF (c_Guid = "") THEN
SET c_Guid = UUID();
//Here I am preparing statement/query for insert
SET @sqlQuery = CONCAT(@sqlQuery, IF(LENGTH(@sqlQuery) > 0,",",""), "(", n_UserID, ", '", d_Date, "','", c_EmailID, "', c_GuID, "') ");
END IF;
END IF;
SET EmailList = SUBSTRING(EmailList, nPos +1, LENGTH(EmailList));
SET nPos = INSTR(EmailList, ",");
END WHILE;
IF (LENGTH(@sqlQuery) > 0) THEN
//Finally here I am inserting in table
SET @sqlQuery = CONCAT("INSERT INTO InviteMst ( UserID, Date, EmailID, Guid ) VALUES ", @sqlQuery);
PREPARE stmt FROM @sqlQuery;
EXECUTE stmt;
SET @sqlQuery = "";
END IF;
END IF;
END
I am calling this store procedure like this :
call usp_InviteMst( 1, 'a@a.com,b@b.com,c@c.co.in,g@yahoo.com' );
It is working perfect, in may cases, but some time (in my live project) it thoring error 'Lock wait timeout exceeded'. my 'innodb_lock_wait_timeout' is set with 100.
If there any better way to do this. or how can I insert multipul records in single request.
I am using node.js and I am using transaction right now.
来源:https://stackoverflow.com/questions/18267565/lock-wait-timeout-exceeded-in-mysql-amazon-rds