Database Administration and Tuning

Similar documents
Chapter 13. Concurrency Control. In This Chapter. c Concurrency Models c Transactions c Locking c Isolation Levels c Row Versioning

Seminar 3. Transactions. Concurrency Management in MS SQL Server

Locking & Blocking Made Simple

Module 15: Managing Transactions and Locks

CSE 344 MARCH 9 TH TRANSACTIONS

CSE 344 MARCH 25 TH ISOLATION

bobpusateri.com heraflux.com linkedin.com/in/bobpusateri. Solutions Architect

L i (A) = transaction T i acquires lock for element A. U i (A) = transaction T i releases lock for element A

Introduction to Data Management CSE 414


Database System Concepts

Database Administration and Tuning

Database System Concepts

Database Systems CSE 414

Chapter 12 : Concurrency Control

Transaction Processing: Concurrency Control. Announcements (April 26) Transactions. CPS 216 Advanced Database Systems

Transaction Concept. Two main issues to deal with:

Concurrency control 12/1/17

Database Systems CSE 414

Graph-based protocols are an alternative to two-phase locking Impose a partial ordering on the set D = {d 1, d 2,..., d h } of all data items.

Introduction to Data Management CSE 344

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

Transaction Management Exercises KEY

! A lock is a mechanism to control concurrent access to a data item! Data items can be locked in two modes :

2 nd Semester 2009/2010

A lock is a mechanism to control concurrent access to a data item Data items can be locked in two modes:

Chapter 15 : Concurrency Control

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

Chapter 7 (Cont.) Transaction Management and Concurrency Control

Transaction Processing: Concurrency Control ACID. Transaction in SQL. CPS 216 Advanced Database Systems. (Implicit beginning of transaction)

Concurrency Control! Snapshot isolation" q How to ensure serializability and recoverability? " q Lock-Based Protocols" q Other Protocols"

Concurrency Control Overview. COSC 404 Database System Implementation. Concurrency Control. Lock-Based Protocols. Lock-Based Protocols (2)

Transaction Concept. Chapter 15: Transactions. Example of Fund Transfer. ACID Properties. Example of Fund Transfer (Cont.)

Advanced Databases (SE487) Prince Sultan University College of Computer and Information Sciences. Dr. Anis Koubaa. Spring 2014

Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition. Chapter 13 Managing Transactions and Concurrency

Transactions and Concurrency Control. Dr. Philip Cannata

Into into Locking and Blocking. Dmitri Korotkevitch ( 1

Introduction to Data Management CSE 344

Database System Concepts

Transactions. Silberschatz, Korth and Sudarshan

Weak Levels of Consistency

Databases - Transactions II. (GF Royle, N Spadaccini ) Databases - Transactions II 1 / 22

Locking, concurrency, and isolation

Chapter 13: Transactions

Chapter 15: Transactions

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 10: Transaction processing. November 14, Lecturer: Rasmus Pagh

Transactions. Lecture 8. Transactions. ACID Properties. Transaction Concept. Example of Fund Transfer. Example of Fund Transfer (Cont.

Implementing Isolation

Chapter 14: Transactions

Chapter 16 : Concurrency Control

Introduction to Data Management CSE 344

CS352 Lecture - Concurrency

Transactions and Isolation

2 Copyright 2015 M. E. Kabay. All rights reserved. 4 Copyright 2015 M. E. Kabay. All rights reserved.

Chapter 15: Transactions

Concurrency Control. Concurrency Control Ensures interleaving of operations amongst concurrent transactions result in serializable schedules

References. Concurrency Control. Administração e Optimização de Bases de Dados 2012/2013. Helena Galhardas e

CMSC 424 Database design Lecture 22 Concurrency/recovery. Mihai Pop

References. Transaction Management. Database Administration and Tuning 2012/2013. Chpt 14 Silberchatz Chpt 16 Raghu

Understanding Isolation Levels and Locking

CSE 530A ACID. Washington University Fall 2013

A transaction is a sequence of one or more processing steps. It refers to database objects such as tables, views, joins and so forth.

CHAPTER 3 RECOVERY & CONCURRENCY ADVANCED DATABASE SYSTEMS. Assist. Prof. Dr. Volkan TUNALI

Transactions Transaction Isolation levels locks Locks Types of Locks Shared Locks(S)

Transaction Management & Concurrency Control. CS 377: Database Systems

CSE 444: Database Internals. Lectures Transactions

Transaction Management

BİL 354 Veritabanı Sistemleri. Transaction (Hareket)

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

CSE 344 MARCH 5 TH TRANSACTIONS

TRANSACTION MANAGEMENT

Transactions. Prepared By: Neeraj Mangla

Transaction Management Chapter 11. Class 9: Transaction Management 1

mywbut.com Concurrency Control

CS352 Lecture - Concurrency

Concurrency Control. Transaction Management. Lost Update Problem. Need for Concurrency Control. Concurrency control

Transactions These slides are a modified version of the slides of the book Database System Concepts (Chapter 15), 5th Ed

Chapter 13 : Concurrency Control

Lock Granularity and Consistency Levels (Lecture 7, cs262a) Ali Ghodsi and Ion Stoica, UC Berkeley February 7, 2018

Lecture 5: Transactions Concurrency Control. Wednesday October 26 th, 2011

Does the Optimistic Concurrency resolve your blocking problems? Margarita Naumova, SQL Master Academy

Comp 5311 Database Management Systems. 14. Timestamp-based Protocols

ICOM 5016 Database Systems. Chapter 15: Transactions. Transaction Concept. Chapter 15: Transactions. Transactions

Mark Broadbent Principal Consultant SQLCloud SQLCLOUD.CO.UK

DATABASE DESIGN I - 1DL300

Lecture 7: Transactions Concurrency Control

IBM C DB Fundamentals.

Phantom Problem. Phantom Problem. Phantom Problem. Phantom Problem R1(X1),R1(X2),W2(X3),R1(X1),R1(X2),R1(X3) R1(X1),R1(X2),W2(X3),R1(X1),R1(X2),R1(X3)

Lesson 11 Transcript: Concurrency and locking

Transaction Management. Chapter 14

Advanced Databases 2006 Based on slides from Kifer et al., Shasha et al. LOCKING

TRANSACTION PROCESSING CONCEPTS

Example: Transfer Euro 50 from A to B

UNIT-IV TRANSACTION PROCESSING CONCEPTS

Transaction Isolation Level in ODI

Database Application Development Oracle PL/SQL, part 2. CS430/630 Lecture 18b

Transactions and Concurrency Control

Monitoring and Resolving Lock Conflicts. Copyright 2004, Oracle. All rights reserved.

DATABASE TRANSACTIONS. CS121: Relational Databases Fall 2017 Lecture 25

Multiversion schemes keep old versions of data item to increase concurrency. Multiversion Timestamp Ordering Multiversion Two-Phase Locking Each

Transcription:

Department of Computer Science and Engineering 2012/2013 Database Administration and Tuning Lab 5 2nd semester In this lab class we will approach the following topics: 1. Important Concepts 1. Transaction execution in SQL Server 2. Isolation levels and modifying the default locking behavior 3. Details about locking and versioning 2. Experiments and Exercises 1. A benchmark experiment for SQL Server 2. Exercises 1.1. Transaction execution in SQL Server The main protocol adopted by SQL Server for managing concurrent transactions involves placing different types of locks on different database objects. SQL Server applies a write lock (i.e., exclusive) when it writes information or a read lock (i.e., shared) when it reads information. Writing information usually refers to inserting, updating, or deleting rows, whereas reading information refers to retrieving rows with a SELECT statement. The database objects that can be locked include tables, database pages, rows, and index entries. As we have seen, a database page is 8 KB in size, and any object resident within these 8 KB is locked implicitly when the database page is locked. Therefore, if a database page is locked, every row held on that page is effectively locked. Similarly, if a table is locked, every row in that table is locked. Besides locking, and since SQL Server 2005, Microsoft has included the ability to use versioning in addition to locking to control the execution of concurrent transactions. In a system using versioning, data is not locked, but instead read operations are executed against an older version of the data being manipulated. 1.2. Isolation levels and modifying the default locking behavior There are two ways in which SQL Server s default locking behavior can be modified. Individual SQL statements can be qualified with a keyword known as a lock hint to modify the locking behavior for that particular statement, or a default locking behavior for the connection can be set with the SET TRANSACTION ISOLATION LEVEL statement. Levels of transaction isolation are specified by the ANSI standard. Each level of isolation is associated to a different type of anomaly that may occur while concurrent transactions are running. The anomalies that may occur when allowing concurrent access to a dabatase table are the following ones: IST/DEI Pág. 1 de 9

Dirty Reads A dirty read occurs when a transaction reads a data item that has been modified by another transaction not yet committed. Nonrepetable Reads Non- repeatable reads happen when a query returns data that would be different if the query were repeated within the same transaction. Non- repeatable reads can occur when other transactions are modifying data that a transaction is reading (e.g. read locks are not acquired when performing a SELECT). Phantoms The phantom problem occurs when, in the course of a transaction, two identical queries are executed, and the collection of rows returned by the second query is different from the first one. This can occur when a concurrent transaction is inserting tuples in the same table as the first one and range locks are not correctly acquired on performing a SELECT. The higher the isolation level, the more stringent the locking protocol with the higher levels being a superset of the lower levels. The transaction isolation levels are as follows and, by default, SQL Server runs at read committed transaction isolation level. Isolation Level Dirty Reads Nonrepetable Reads Phantoms Serializable No No No Repeatable Read No No Yes Read Committed No Yes Yes Read Uncommitted Yes Yes Yes Snapshot No No No Read Committed Snapshot No Yes Yes The first four isolation levels from the table above are implemented through a locking protocol, whereas the last two isolation levels are based on versioning. The keywords available as lock hints, for modifying locking behavior, are as follows: DBLOCK - forces a shared database lock to be obtained when information is read. HOLDLOCK - forces a shared lock on a table to remain until the transaction completes. Key range locking will also be used to prevent phantom inserts. Nonrepeatable reads are also prevented. NOLOCK - allows a dirty read to take place that is, a transaction can read the uncommitted changes made by another transaction. PAGLOCK - forces shared page locks to be taken where otherwise SQL Server may have used a table or row lock. IST/DEI Pág. 2 de 9

READCOMMITTED - ensures that the statement behaves in the same way as if the connection were set to transaction isolation level READ COMMITTED. READPAST - enables a statement to skip rows that are locked by other statements. READUNCOMMITTED - equivalent to the NOLOCK lock hint. REPEATABLEREAD - ensures that the statement behaves in the same way as if the connection were set to transaction isolation level REPEATABLE READ. ROWLOCK - forces the use of rowlocks and is similar in use to PAGLOCK. SERIALIZABLE - forces shared locks to stay until the transaction completes. This is equivalent to specifying the HOLDLOCK hint. TABLOCK - forces a shared table lock to be taken where otherwise SQL Server may have used row locks. UPDLOCK - forces SQL Server to take update locks where otherwise SQL Server would have used shared locks. XLOCK - forces exclusive locks to be taken out. This is typically used with TABLOCK and PAGLOCK. Lock hints are usually used on SELECT statements. The next line provides an example: SELECT balance FROM accounts WITH (READUNCOMMITTED) WHERE account_no = 1000; Beware of the SET IMPLICIT_TRANSACTIONS ON statement. It will automatically start a transaction when Transact- SQL statements such as SELECT, INSERT, UPDATE, and DELETE are used. The transaction will not be committed and its locks will not be released until an explicit COMMIT TRANSACTION statement is executed. 1.3. Details about locking and versioning We shall now analyze in more detail the intrinsics of locking and versioning in SQL Server. Lock granularity Applications require different levels of lock granularity. One application may benefit from page- level locking, while another application may benefit from row- level locking. Locking individual columns provides the highest level of concurrency. By this, we mean that multiple users could be updating different columns in the same row simultaneously and they would not be involved in la ock conflict. If the lock granularity is implemented at the database level, the lowest level of concurrency is achieved. Multiple users could not simultaneously change anything at all in the database. However, the finer the granularity, the more system resources are used. This is why SQL Server and database systems in IST/DEI Pág. 3 de 9

general do not lock at the column level. The amount of system resources used to manage all the locks would be too large. Locks are approximately 100 bytes each in SQL Server. Locking at the column level would probably require tens of thousands of locks in a medium- sized database, which could equate to many megabytes of memory. The CPU resources needed to manage these locks would also be massive. Consequently, SQL Server locks rows, pages, and tables, which is a reasonable approach. The database itself can, of course, be set to single- user mode, which effectively provides locking at the database level. Locking at the row level can be considered to be the default situation. One of the reasons that SQL Server tends to lock at the row level is that it has the capability to escalate locks, but not to de- escalate locks. Therefore, if SQL Server decides that a SQL statement is likely to lock the majority of rows in a table, it may lock at the table level. The same logic is used if SQL Server determines that most of the rows in a page are likely to be locked it may take out a page lock instead of multiple row locks. Intent Locks SQL Server also makes use of a type of lock known as an intent lock. Intent locks are placed over tables and over pages in the tables, when a user locks rows in the table, and they stay in place for the life of the row locks. These locks are used primarily to ensure that a user cannot take out locks on a table or pages in the table that would conflict with another user s row locks. For example, if a user was holding an exclusive row lock and another user wished to take out an exclusive table lock on the table containing the row, the intent lock held on the table by the first user would ensure that its row lock would not be overlooked by the lock manager. Deadlock prevention Once a lock has been placed on an object, it has a lifetime. Suppose a T- SQL statement that causes a row lock is executed inside a user- defined transaction. It is possible in SQL Server to set a lock timeout value for a connection, so that transactions within this connection will only wait for their locks during a predefined period of time, after which the transaction will receive an error message informing it that the timeout period has been exceeded. In this context, a connection is a session of work, opened by a program such as Management Studion to communicate with the SQL Server database server, in order to execute transactions as a specific user. A lock timeout value is set per connection as follows: SET LOCK_TIMEOUT 10000 IST/DEI Pág. 4 de 9

The timeout value is specified in milliseconds. The default value of 1 means wait indefinitely, whereas a value of 0 means do not wait at all. Deadlock resolution A deadlock situation can occur in SQL Server when a user holds a lock on a resource needed by a another user who holds a lock on a resource needed by the first user. This is a deadly embrace, and the users would wait forever if SQL Server did not intervene. SQL Server chooses one of the deadlocked users as a victim and issues a rollback for its transaction. A connection can set its deadlock priority such that, in the event of a transaction within this connection being involved in a deadlock, it will be chosen as the victim, as follows: SET DEADLOCK_PRIORITY LOW To return to the default deadlock handling mechanism, use the following code: SET DEADLOCK_PRIORITY NORMAL Generally, the transaction involved in the deadlock that has accumulated the least amount of CPU time is usually chosen as the victim. Read, Write and Update locks As well as placing shared and exclusive locks on database rows, SQL Server also makes use of a type of lock known as an update lock. These locks are associated with SQL statements that perform update and delete operations, which need to initially read rows before changing or deleting them. These rows have update locks placed on them that are compatible with shared read locks but are not compatible with other update locks or exclusive locks. If the rows must subsequently be updated or deleted, SQL Server attempts to promote the update locks to exclusive locks. If any other shared locks are associated with the rows, SQL Server will not be able to promote the update locks until these are released. Update locks are essentially an optimization to reduce deadlocks. Snapshot isolation levels Since SQL Server 2005, Microsoft has included the ability to use versioning in addition to locking (SET READ_COMMITTED_SNAPSHOT ON and SET TRANSACTION ISOLATION LEVEL SNAPSHOT). In a system using versioning, data is not locked, but instead read operations happen against an older version of the data being manipulated. Depending upon isolation levels, the read operation can either read the latest committed data or the data as it was when the read operation started. Be aware that when an update operation takes place against the data, the data being touched is copied to a separate storage area, incurring in a performance penalty (i.e., do not use versioning if you are doing large batch updates). Moreover, snapshot isolation will also permit write skew anomalies. In a write IST/DEI Pág. 5 de 9

skew anomaly, two transactions (T1 and T2) concurrently read an overlapping data set (e.g. values V1 and V2), concurrently make disjoint updates (e.g. T1 updates V1, T2 updates V2), and finally concurrently commit, neither having seen the update performed by the other. Were the system serializable, such an anomaly would be impossible, as either T1 or T2 would have to occur "first", and be visible to the other. For details about the snapshot isolation level, please consider reading: www.sqlteam.com/article/transaction- isolation- and- the- new- snapshot- isolation- level. 2. Experiments and Exercises 2.1 - A benchmark experiment for SQL Server The Activity Monitor allows you to examine database connections. In SQL Server Management Studio, connect to the server and then start the Activity Monitor. The three available options cover processes, locks by process, and locks by object. The objective of the Activity Monitor and its lock monitoring tools is to allow for locks to be removed, particularly if a lock is causing some kind of performance problem. In extreme cases, a lock involved in a deadlock can even cause a database halt. Students can also test the following steps in SQL Server, in order to see how concurrent transactions are handled. Between each step, be sure to execute the following statement to place the AdventureWorks database with the default values. UPDATE Production.Product SET Name = 'Blade' WHERE ProductID = 316; Case 1 : Using the read uncommitted isolation level - Users can query the value of a row/column that is in the middle of a transaction but not yet completed. USER 1 USER 2 UPDATE Production.Product SET Name = 'Super Blade' WHERE ProductID = 316; -- was previously 'Blade', now 'Super Blade' SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; ProductID = 316; -- WILL RETURN 'Super Blade', even before -- committing the first transaction, IST/DEI Pág. 6 de 9

Case 2 : Using the read committed isolation level The transaction executed by USER 2 will result in a deadlock on the SELECT statement until the COMMIT TRANSACTION of Transaction 1 is executed, because the READ COMMITTED isolation level can only read COMMITTED data. USER 1 USER 2 UPDATE Production.Product SET Name = 'Super Blade' WHERE ProductID = 316; -- was previously 'Blade', now 'Super Blade' SET TRANSACTION ISOLATION LEVEL READ COMMITTED; ProductID = 316; -- Deadlock until the COMMIT finishes for User 1, Case 3 : Using the repeatable read isolation level - Transaction 2 will always read the same piece of data and return the same result, not allowing Transaction 1's UPDATE to complete until Transaction 2 completes successfully. USER 1 USER 2 SET TRANSACTION ISOLATION LEVEL REPEATABLE READ UPDATE Production.Product SET Name = 'Super Blade' WHERE ProductID = 316; -- Deadlock, because User 2 still has a lock on the row SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; BEGIN TRAN ProductID = 316; -- returns 'Blade' ProductID = 316; -- Will still return 'Blade' -- Still in deadlock until User 2 commits COMMIT TRANSACTION -- UPDATE completes we can COMMIT Case 4 : Using the serializable isolation level - Will lock out any insertions that would match the range, until the other transaction is committed, thus avoding phantom reads. IST/DEI Pág. 7 de 9

USER1 SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; INSERT INTO HumanResources.Department (Name,GroupName,ModifiedDate) VALUES ('Test Department','G', '9-04-2010'); -- Deadlock, because this would affect the -- rowcount for User 2 - Still in deadlock until User 2 commits -- INSERT completes we can COMMIT USER2 SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; SELECT COUNT(*) FROM HumanResources.Department; -- returns number of rows in table SELECT COUNT(*) FROM HumanResources.Department; -- STILL returns the same number Case 5 : Using the snapshot isolation level - non- blocking READ COMMITTED, by taking a "snapshot" (thus the name of the isolation level) into the tempdb database. This allows a developer to query against a specific version of the data throughout the entire transaction, without needing to wait for any other locks to clear, and without reading from any "dirty data". The SNAPSHOT isolation level also avoid phantoms, but introduces an anomalie called write skews (i.e. two transactions may each read a value, then change the other's value, but neither sees the result of the other's update). USER1 ALTER DATABASE AdventureWorks SET ALLOW_SNAPSHOT_ISOLATION ON; SET TRANSACTION ISOLATION LEVEL SNAPSHOT; UPDATE Production.Product SET Name = 'Super Blade' WHERE ProductID = 316; -- was previously 'Blade', now 'Super Blade' USER2 SET TRANSACTION ISOLATION LEVEL SNAPSHOT BEGIN TRAN ProductID = 316; -- Still 'Blade' ProductID = 316; -- Still 'Blade' ProductID = 316; -- NOW it's 'Super Blade' 2.2 Exercises 2.2.1. Consider the following 3 transactions and the following schedule S involving the concurrent execution of the three transactions: IST/DEI Pág. 8 de 9

T1: begin(); write(x); read(y); write(z); commit(); T2: begin(); read(x); write(z); commit(); T3: begin(); read(z); write(y); commit(); S: T3:read(z), T2:read(x), T1:write(x), T3:write(y), T1:read(y), T2:write(z), T1:write(z) a) Present the precedence graph for S. b) Is the schedule serializable? Justify. If the schedule is serializable, indicate a corresponding serial schedule. c) Consider the 2 Phase Locking (2PL) Protocol, in which one transaction obtains a lock on a data item before using it. Could schedule S have been produced when using 2PL? Justify. 2.2.2. Consider the following two transactions: T1: read(a); read(b); if A = 0 then B := B + 1; write(b). T2: read(b); read(a); if B = 0 then A := A + 1; write(a). a) Add lock and unlock instructions to transactions T1 and T2, so that they observe the two- phase locking protocol. b) Can the execution of these transactions result in a deadlock? IST/DEI Pág. 9 de 9