Home > database >  Please help to optimize a stored procedure for concurrent conditions
Please help to optimize a stored procedure for concurrent conditions

Time:05-20

Stored procedure using the base table name: table1, the data quantity is large; For UserID need the incoming parameters, its purpose is to grab the value of the minimum BH field in the table1
I write a stored procedure idea is as follows:
1, lock base table table1
The LOCK TABLE table1 IN EXCLUSIVE MODE;
2, to determine whether the current UserID have unused data, GetScore=1 that did not use the
SELECT COUNT (*) INTO @ RowCount FROM table1 WHERE UserID=pUserID AND GetScore=1;
3, if there is no unused data, then let the current user in the table1 immediately grab a data
IF @ RowCount=0 THEN
The SELECT MIN (BH) INTO @ BH FROM table1 the WHERE condition
The UPDATE table1 SET UserID=pUserID WHERE BH=@ BH
END the IF
4, submit
COMMIT

Application in many places to use to the table1, feel it, efficiency is not high, certainly hope that friends can help me to optimize the, thank you!
  • Related