Bài giảng Microsoft SQL server - Bài 11: Quản lý giao tác và khóa trình bày một tập hợp các câu lệnh được kết hợp thành một giao dịch, hãy chèn các câu lệnh Transact-SQL để bắt đầu, cam kết và khôi phục giao dịch một cách rõ ràng.
Lecture 11 How to manage transactions and locking Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide Objectives Applied Given a set of statements to be combined into a transaction, insert the TransactSQL statements to explicitly begin, commit, and roll back the transaction. Knowledge Describe the use of implicit transactions. Describe the use of explicit transactions. Describe the use of the COMMIT TRAN statement and the @@TRANCOUNT function within nested transactions. Describe the use of save points. Define these types of concurrency problems: lost updates, dirty reads, nonrepeatable reads, and phantom reads. Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide Objectives (cont.) Describe the way locking and the transaction isolation level help to prevent concurrency problems. Describe the way SQL Server manages locking in terms of granularity, lock escalation, shared locks, exclusive locks, and lock promotion. Describe deadlocks and the way SQL Server handles them. Describe four coding techniques that can reduce deadlocks. Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide The SQL script that creates the AP database CREATE DATABASE AP; GO USE AP; CREATE TABLE Terms (TermsID TermsDescription TermsDueDays INT VARCHAR(50) SMALLINT NOT NULL PRIMARY KEY, NOT NULL, NOT NULL); CREATE TABLE GLAccounts (AccountNo INT AccountDescription VARCHAR(50) NOT NULL PRIMARY KEY, NOT NULL); Murach's SQL Server 2012, C11 © 2012, Mike Murach & Associates, Inc Slide 4 The SQL script (cont.) CREATE TABLE Vendors (VendorID PRIMARY KEY, VendorName VendorAddress1 VendorAddress2 VendorCity VendorState VendorZipCode VendorPhone VendorContactLName VendorContactFName DefaultTermsID DefaultAccountNo Murach's SQL Server 2012, C11 INT NOT NULL IDENTITY VARCHAR(50) NOT NULL, VARCHAR(50) NULL, VARCHAR(50) SPARSE NULL, VARCHAR(50) NOT NULL, CHAR(2) NOT NULL, VARCHAR(20) NOT NULL, VARCHAR(50) NULL, VARCHAR(50) NULL, VARCHAR(50) NULL, INT NOT NULL REFERENCES Terms(TermsID), INT NOT NULL REFERENCES GLAccounts(AccountNo)); © 2012, Mike Murach & Associates, Inc Slide 5 The SQL script (cont.) CREATE TABLE Invoices (InvoiceID PRIMARY KEY, VendorID InvoiceNumber InvoiceDate InvoiceTotal PaymentTotal CreditTotal TermsID InvoiceDueDate PaymentDate Murach's SQL Server 2012, C11 INT NOT NULL IDENTITY INT NOT NULL REFERENCES Vendors(VendorID), VARCHAR(50) NOT NULL, SMALLDATETIME NOT NULL, MONEY NOT NULL, MONEY NOT NULL DEFAULT 0, MONEY NOT NULL DEFAULT 0, INT NOT NULL REFERENCES Terms(TermsID), SMALLDATETIME NOT NULL, SMALLDATETIME NULL); © 2012, Mike Murach & Associates, Inc Slide 6 The SQL script (cont.) CREATE TABLE InvoiceLineItems (InvoiceID INT NOT NULL REFERENCES Invoices(InvoiceID), InvoiceSequence SMALLINT NOT NULL, AccountNo INT NOT NULL REFERENCES GLAccounts(AccountNo), InvoiceLineItemAmount MONEY NOT NULL, InvoiceLineItemDescription VARCHAR(100) NOT NULL, PRIMARY KEY (InvoiceID, InvoiceSequence)); Murach's SQL Server 2012, C11 © 2012, Mike Murach & Associates, Inc Slide 7 Transactions INSERT statements that work with related data DECLARE @InvoiceID int; INSERT Invoices VALUES (34,'ZXA-080','2012-04-30',14092.59, 0,0,3,'2012-05-30',NULL); SET @InvoiceID = @@IDENTITY; INSERT InvoiceLineItems VALUES (@InvoiceID,1,160,4447.23,'HW upgrade'); INSERT InvoiceLineItems VALUES (@InvoiceID,2,167,9645.36,'OS upgrade'); Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide The same statements coded as a transaction DECLARE @InvoiceID int; BEGIN TRY BEGIN TRAN; INSERT Invoices VALUES (34,'ZXA-080','2012-04-30',14092.59, 0,0,3,'2012-05-30',NULL); SET @InvoiceID = @@IDENTITY; INSERT InvoiceLineItems VALUES (@InvoiceID,1,160,4447.23,'HW upgrade'); INSERT InvoiceLineItems VALUES (@InvoiceID,2,167,9645.36,'OS upgrade'); COMMIT TRAN; END TRY BEGIN CATCH ROLLBACK TRAN; END CATCH; Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide When to use explicit transactions When you code two or more action queries that affect related data When you update foreign key references When you move rows from one table to another table When you code a SELECT query followed by an action query and the values inserted in the action query are based on the results of the SELECT query When a failure of any set of SQL statements would violate data integrity Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide 10 Two transactions that retrieve and then modify the same row (continued) The initial values for the row The values after transaction A executes The values after transaction B executes Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide 21 The four types of concurrency problems Problem Lost updates Description Occur when two transactions select the same row and then update the row based on the values originally selected. Dirty reads Occur when a transaction selects data that (uncommitted isn’t committed by another transaction. dependencies) Nonrepeatable reads Occur when two SELECT statements of the (inconsistent analysis) same data result in different values because another transaction has updated the data in the time between the two statements. Phantom reads Occur when you perform an update or delete on a set of rows when another transaction is performing an insert or delete that affects one or more rows in that same set of rows. Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide 22 The syntax of the SET TRANSACTION ISOLATION LEVEL statement SET TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED|READ COMMITTED|REPEATABLE READ| SNAPSHOT|SERIALIZABLE} Concurrency problems prevented by each transaction isolation level Isolation level READ UNCOMMITTED READ COMMITTED REPEATABLE READ SNAPSHOT SERIALIZABLE Murach's SQL Server 2012, C17 Dirty reads Allows Prevents Prevents Prevents Prevents Lost updates Allows Allows Prevents Prevents Prevents © 2012, Mike Murach & Associates, Inc Nonrepeatable reads Allows Allows Prevents Prevents Prevents Slide 23 Phantom reads Allows Allows Allows Prevents Prevents Terms Concurrency Locks Transaction isolation level Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide 24 Manage locking The ten levels of lockable resources Granularity Coarse Fine Murach's SQL Server 2012, C17 Resource Locks… Database An entire database. Allocation unit A collection of pages that contains a particular type of data. Metadata The data in the system catalog. File An entire database file. Table An entire table, including indexes. Heap or Btree The index pages (Btree) for a table with a clustered index or the data pages (heap) for a table with no clustered index. Extent A contiguous group of eight pages. Page One page (8 KB) of data. Key A key or range of keys in an index. Row A single row within a table. © 2012, Mike Murach & Associates, Inc Slide 25 Common SQL Server lock modes Category Shared Lock mode Schema Stability (SchS) Intent Shared (IS) Shared (S) Update (U) Exclusive Shared with Intent Exclusive (SIX) Intent Exclusive (IX) Exclusive (X) Bulk Update (BU) Schema Modification (SchM) Murach's SQL Server 2012, C17 What the lock owner can Compile a query Read but not change data Read but not change data Read but not change data until promoted to an Exclusive (X) lock Read and change data Read and change data Read and change data Bulkcopy data into a table Modify the database schema © 2012, Mike Murach & Associates, Inc Slide 26 Terms Lockable resources Coarsegrain lock Finegrain lock Lock manager Lock escalation Lock mode Lock promotion Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide 27 Compatibility between lock modes Current lock mode Schema Stability Intent Shared Shared Update Shared w/Intent Exclusive Intent Exclusive Exclusive Bulk Update Schema Modification Murach's SQL Server 2012, C17 SchS IS S U SIX IX X BU SchM Requested lock mode Sch-S IS S U SIX IX X BU Sch-M √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ © 2012, Mike Murach & Associates, Inc Slide 28 Prevent deadlocks Two transactions that deadlock A SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; DECLARE @InvoiceTotal money; BEGIN TRAN; SELECT @InvoiceTotal = SUM(InvoiceLineItemAmount) FROM InvoiceLineItems WHERE InvoiceID = 101; WAITFOR DELAY '00:00:05'; UPDATE Invoices SET InvoiceTotal = @InvoiceTotal WHERE InvoiceID = 101; COMMIT TRAN; The response from the system (1 row(s) affected) Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide 29 Two transactions that deadlock (continued) B SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; DECLARE @InvoiceTotal money; BEGIN TRAN; SELECT @InvoiceTotal = InvoiceTotal FROM Invoices WHERE InvoiceID = 101; UPDATE InvoiceLineItems SET InvoiceLineItemAmount = @InvoiceTotal WHERE InvoiceID = 101 AND InvoiceSequence = 1; COMMIT TRAN; The response from the system Msg 1205, Level 13, State 51, Line 11 Transaction (Process ID 53) was deadlocked on lock resources with another process and has been chosen as the deadlock victim Rerun the transaction Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide 30 How the deadlock occurs Transaction A requests and acquires a shared lock on the InvoiceLineItems table. Transaction B requests and acquires a shared lock on the Invoices table. Transaction A tries to acquire an exclusive lock on the Invoices table to perform the update. Since transaction B already holds a shared lock on this table, transaction A must wait for the exclusive lock. Transaction B tries to acquire an exclusive lock on the InvoiceLineItems table, but must wait because transaction A holds a shared lock on that table. Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide 31 Terms Deadlock Deadlock victim Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide 32 Coding techniques that prevent deadlocks Don’t allow transactions to remain open for very long Keep transactions short. Keep SELECT statements outside of the transaction except when absolutely necessary. Never code requests for user input during an open transaction. Use the lowest possible transaction isolation level The default level of READ COMMITTED is almost always sufficient. Reserve the use of higher levels for short transactions that make changes to data where integrity is vital. Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide 33 Coding techniques that prevent deadlocks (continued) Make large changes when you can be assured of nearly exclusive access If you need to change millions of rows in an active table, don’t do so during hours of peak usage. If possible, give yourself exclusive access to the database before making large changes. Consider locking when coding your transactions If you need to code two or more transactions that update the same resources, code the updates in the same order in each transaction. Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide 34 UPDATE statements that transfer money between two accounts From savings to checking UPDATE Savings SET Balance = Balance - @TransferAmt; UPDATE Checking SET Balance = Balance + @TransferAmt; From checking to savings UPDATE Checking SET Balance = Balance - @TransferAmt; UPDATE Savings SET Balance = Balance + @TransferAmt; From checking to savings in reverse order to prevent deadlocks UPDATE Savings SET Balance = Balance + @TransferAmt; UPDATE Checking SET Balance = Balance - @TransferAmt; Murach's SQL Server 2012, C17 © 2012, Mike Murach & Associates, Inc Slide 35 ... related data DECLARE @InvoiceID int; INSERT Invoices VALUES (34,''ZXA-080'',''201 2-0 4-3 0'',14092.59, 0,0,3,''201 2-0 5-3 0'',NULL); SET @InvoiceID = @@IDENTITY; INSERT InvoiceLineItems VALUES (@InvoiceID,1,160,4447.23,''HW... @InvoiceID int; BEGIN TRY BEGIN TRAN; INSERT Invoices VALUES (34,''ZXA-080'',''201 2-0 4-3 0'',14092.59, 0,0,3,''201 2-0 5-3 0'',NULL); SET @InvoiceID = @@IDENTITY; INSERT InvoiceLineItems VALUES (@InvoiceID,1,160,4447.23,''HW... AccountDescription VARCHAR(50) NOT NULL PRIMARY KEY, NOT NULL); Murach''s? ?SQL? ? Server 2012, C11 © 2012, Mike Murach & Associates, Inc Slide 4 The SQL script (cont.) CREATE TABLE Vendors (VendorID PRIMARY KEY,