Real case of SQL Server Phantom read

Source: Internet
Author: User

The database has a table [01_subjectivescoreinfo], to implement the data in the table is only detected once, the table data is large, there are 3 million or 4 million data. The table structure is also really not very reasonable, unable to modify the table structure, even if a new field will have a considerable amount of modification.

Because there are a large number of INSERT INTO SELECT * Statements in the previous code, adding a field to do nothing will also cause the entire project to be paralyzed, but I don't want to discuss the code quality of the predecessors.

So I added a new table [01_subjectivescoreinfoflag] to record the record ID that was taken. Then there is the following code:

BEGIN TRAN                                        SET TRANSACTION Isolation  Level SERIALIZABLE; INSERT  into [01_subjectivescoreinfoflag](ID)SELECT TOP  -ss.id from [01_subjectivescoreinfo]  asSsINNER JOINSubjectiveiteminfo asSI
onSs. Testcode=SI. Testcode andSs. Majorquestionid=SI. Majorquestionid andSs. Minorquestionid=SI. MinorquestionidWHERESs. Testcode=" "+ @TestCode +" " andSI. Questiongroupcode=" "+ @QuestionGroupCode +" " and(SI. Minorquestioncount=0 ORSI. Minorquestionid>0) and not EXISTS ( SELECT TOP 1 1 from [01_subjectivescoreinfoflag] WHEREId=ss.id)COMMIT TRAN

The transaction is used here, and the isolation level is specified. This is a must, we have not specified before, single-threaded, no matter how you call this code to run is very reliable, but multithreaded simulation of concurrent calls there is a very serious duplication.

The above code also does not solve the problem, the reason is that the lock is not used, multi-threaded directly on the emergence of deadlock phenomenon. Having had little experience with the isolation level before, I once thought that the problem was non-solvable until it was suddenly solved today.

According to my own understanding, because the level of locks is not enough, causing the resource scramble.

The equivalent of the toilet, be sure to obtain a complete lock, close the door to let others come in, otherwise if the other people came in, although he did not occupy the seat, but he took the toilet paper. You occupy the seat but no toilet paper, he took the toilet paper but no seat, the two sides do not give, who can not complete the toilet affairs, stalemate, and lead to deadlock. At this point you need to come to the toilet management, or force you to let go of the seat, do not manage you wipe your butt, or grab the toilet paper to get rid of him, let him pull on the trousers.

The following code is then available:

BEGIN TRAN                                        SET TRANSACTION Isolation  Level SERIALIZABLE; INSERT  into [' [email protected]+ ' _subjectivescoreinfoflag](ID)SELECT TOP  -ss.id from [' [email protected]+ ' _subjectivescoreinfo]  asSs with(UPDLOCK)INNER JOINSubjectiveiteminfo asSI with(UPDLOCK) onSs. Testcode=SI. Testcode andSs. Majorquestionid=SI. Majorquestionid andSs. Minorquestionid=SI. MinorquestionidWHERESs. Testcode=" "+ @TestCode +" "                           andSI. Questiongroupcode=" "+ @QuestionGroupCode +" "                          and(SI. Minorquestioncount=0                         ORSI. Minorquestionid>0)                       and  not EXISTS                      (                        SELECT TOP 1 1                          from [' [email protected]+ ' _subjectivescoreinfoflag]  with(UPDLOCK)WHEREId=ss.id)COMMIT TRAN

Uplock is the lock, so there is no deadlock.

Real case of SQL Server Phantom read

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.