© Dennis Shasha, Philippe Bonnet 2001 Lock Tuning.

Slides:



Advertisements
Similar presentations
2 - Tuning the Guts © Dennis Shasha, Philippe Bonnet Tuning the Guts Concurrency Control –How to minimize lock contention? Recovery –How to manage.
Advertisements

Chapter 16 Concurrency. Topics in this Chapter Three Concurrency Problems Locking Deadlock Serializability Isolation Levels Intent Locking Dropping ACID.
1 Concurrency Control Chapter Conflict Serializable Schedules  Two actions are in conflict if  they operate on the same DB item,  they belong.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Accessing data Transactions. Agenda Questions from last class? Transactions concurrency Locking rollback.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Principles of Transaction Management. Outline Transaction concepts & protocols Performance impact of concurrency control Performance tuning.
Concurrency Control Amol Deshpande CMSC424. Approach, Assumptions etc.. Approach  Guarantee conflict-serializability by allowing certain types of concurrency.
Tuning Dennis Shasha and Philippe Bonnet, 2013.
Lock Tuning. AOBD 2007/08 H. Galhardas Concurrency Control Goals Performance goals  Reduce blocking One transaction waits for another to release its.
Lock Tuning. overview © Dennis Shasha, Philippe Bonnet 2001 Sacrificing Isolation for Performance A transaction that holds locks during a screen interaction.
1 Supplemental Notes: Practical Aspects of Transactions THIS MATERIAL IS OPTIONAL.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. –Because disk accesses are.
Allowing Multi-user Access Grant – GRANT ON TO |WITH GRANT OPTION | –GRANT TO | WITH ADMIN OPTION| – can be PUBLIC or a role – can be ALL Revoke – REVOKE.
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
Transaction Management and Concurrency Control
Transaction Management and Concurrency Control
Transaction Management and Concurrency Control
Transaction Management and Concurrency Control
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Transaction Management
H.Lu/HKUST L07: Lock Tuning. L07-Lock Tuning - 2 H.Lu/HKUST Lock Tuning  The key is to combine the theory of concurrency control with practical DBMS.
Chapter 9 Transaction Management and Concurrency Control
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Transaction Management and Concurrency Control
Transaction Management Chapter 9. What is a Transaction? A logical unit of work on a database A logical unit of work on a database An entire program An.
1 Transaction Management Overview Chapter Transactions  A transaction is the DBMS’s abstract view of a user program: a sequence of reads and writes.
08_Transactions_LECTURE2 DBMSs should guarantee ACID properties (Atomicity, Consistency, Isolation, Durability). This is typically done by guaranteeing.
© Dennis Shasha, Philippe Bonnet – 2013 Communicating with the Outside.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Chapterb19 Transaction Management Transaction: An action, or series of actions, carried out by a single user or application program, which reads or updates.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
Databases Illuminated
ITEC 3220M Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220m.htm
1 Performance Tuning Next, we focus on lock-based concurrency control, and look at optimising lock contention. The key is to combine the theory of concurrency.
© Dennis Shasha, Philippe Bonnet 2001 Log Tuning.
1 Concurrency Control II: Locking and Isolation Levels.
11/7/2012ISC329 Isabelle Bichindaritz1 Transaction Management & Concurrency Control.
1 Concurrency Control Lecture 22 Ramakrishnan - Chapter 19.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
1 Advanced Database Concepts Transaction Management and Concurrency Control.
Module 11: Managing Transactions and Locks
Transaction Management and Concurrent Control
10 1 Chapter 10_B Concurrency Control Database Systems: Design, Implementation, and Management, Rob and Coronel.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
10 Transaction Management and Concurrency Control MIS 304 Winter 2005.
©Bob Godfrey, 2002, 2005 Lecture 17: Transaction Integrity and Concurrency BSA206 Database Management Systems.
3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control.
Lock Tuning. Overview Data definition language (DDL) statements are considered harmful DDL is the language used to access and manipulate catalog or metadata.
Database Isolation Levels. Reading Database Isolation Levels, lecture notes by Dr. A. Fekete, resentation/AustralianComputer.
Jinze Liu. ACID Atomicity: TX’s are either completely done or not done at all Consistency: TX’s should leave the database in a consistent state Isolation:
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Transactions and Concurrency Control. 2 What is a Transaction?  Any action that reads from and/or writes to a database may consist of  Simple SELECT.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
1 Concurrency Control. 2 Why Have Concurrent Processes? v Better transaction throughput, response time v Done via better utilization of resources: –While.
1 Chapter 2. Universal Tuning Considerations Spring 2002 Prof. Sang Ho Lee School of Computing, Soongsil University
Transaction Management and Concurrency Control
LAB: Web-scale Data Management on a Cloud
Concurrency Control More !
Transaction Management
March 21st – Transactions
Transaction Management
March 9th – Transactions
Transactions, Locking and Query Optimisation
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Transactions and Concurrency
Transaction Management
Transation Management
Database Systems (資料庫系統)
Presentation transcript:

© Dennis Shasha, Philippe Bonnet 2001 Lock Tuning

© Dennis Shasha, Philippe Bonnet 2001 Concurrency Control Goals Performance goals –Reduce blocking One transaction waits for another to release its locks –Avoid deadlocks Transactions are waiting for each other to release their locks Correctness goals –Serializability: each transaction appears to execute in isolation –The programmer ensures that serial execution is correct. Trade-off between correctness and concurrency

© Dennis Shasha, Philippe Bonnet 2001 Ideal Transaction Acquires few locks and favors shared locks over exclusive locks –Reduce the number of conflicts -- conflicts are due to exclusive locks Acquires locks with fine granularity –Reduce the scope of each conflict Holds locks for a short time –Reduce waiting

Lock Tuning 1. Use special system facilities for long reads. 2. Eliminate locking when it is unnecessary. 3. Take advantage of transactional context to chop transactions into small pieces. 4. Weaken isolation guarantees when the application allows it. 5. Select the appropriate granularity of locking. 6. Change your data description data during quiet periods only. (Data Definition Language statements are considered harmful.) 7. Think about partitioning. 8. Circumvent hot spots. 9. Tune the deadlock interval. © Dennis Shasha, Philippe Bonnet 2001

Use facilities for long reads Oracle provides snapshot isolation, whereby read-only queries hold no locks yet appear to execute serializably A read-only transaction R reads the database as the database appeared when R began © Dennis Shasha, Philippe Bonnet 2001

Snapshot isolation Read-only queries suffer no locking overhead. Read-only queries can execute in parallel and on the same data as short update transactions without causing blocking or deadlocks. When extended to read/write transactions, snapshot isolation does not guarantee correctness –For example: T 1 : x := y T 2 : y := x The lesson is: use snapshot isolation for read- only transactions © Dennis Shasha, Philippe Bonnet 2001

Eliminate unnecessary locking Locking is unnecessary in two cases. –1. When only one transaction runs at a time, for example, when loading the database –2. When all transactions are read-only, for example, when doing decision support queries on archival databases © Dennis Shasha, Philippe Bonnet 2001

Make your transaction short How long should a transaction be? –The more locks a transaction requests, the more likely it is that it will have to wait for some other transaction to release a lock. –The longer a transaction T executes, the more time another transaction will have to wait if it is blocked by T. © Dennis Shasha, Philippe Bonnet 2001

Example: Simple Purchases Purchase item I for price P 1.If cash < P then roll back transaction (constraint) 2.Inventory(I) := inventory(I)+P 3.Cash := Cash – P Two purchase transaction P1 and P2 –P1 has item I for price 50 –P2 has item I for price 75 –Cash is 100

© Dennis Shasha, Philippe Bonnet 2001 Example: Simple Purchases If as one transaction then one of P1, P2 rolls back. If 1, 2, 3 as three distinct transactions: –P1 checks that cash > 50. It is. –P2 checks that cash > 75. It is. –P1 completes. Cash = 50. –P2 completes. Cash = - 25.

© Dennis Shasha, Philippe Bonnet 2001 Example: Simple Purchases Orthodox solution –Make whole program a single transaction Cash becomes a bottleneck! Chopping solution –Find a way to rearrange and then chop up the transactions without violating serializable isolation level.

© Dennis Shasha, Philippe Bonnet 2001 Example: Simple Purchases Chopping solution: 1.If Cash < P then roll back. Cash := Cash – P. 2.Inventory(I) := inventory(I) + P Chopping execution: –P11: 100 > 50. Cash := 50. –P21: 75 > 50. Rollback. –P12: inventory := inventory + 50.

© Dennis Shasha, Philippe Bonnet 2001 Transaction Chopping Execution rules: –When pieces execute, they follow the partial order defined by the transactions. –If a piece is aborted because of a conflict, it will be resubmitted until it commits –If a piece is aborted because of an abort, no other pieces for that transaction will execute.

© Dennis Shasha, Philippe Bonnet 2001 Transaction Chopping Let T1, T2, …, Tn be a set of transactions. A chopping partitions each Ti into pieces ci1, ci2, …, cik. A chopping of T is rollback-safe if (a)T does not contain any abort commands or (b) if the abort commands are in the first piece.

© Dennis Shasha, Philippe Bonnet 2001 Correct Chopping Chopping graph (variation of the serialization graph): –Nodes are pieces –Edges: C-edges: C stands for conflict. There is a C-edge between two pieces from different transactions if they contain operations that access the same data item and one operation is a write. S-edges: S stands for siblings. There is an S-edge between two pieces, iff they come from the same transaction. A chopping graph contains an S-C cycle if it contains a cycle that includes at least one S-edge and one C-edge.

© Dennis Shasha, Philippe Bonnet 2001 Correct Chopping A chopping is correct if it is rollback safe and its chopping graph contains no SC- cycle. T1: r(x) w(x) r(y) w(y) T2: r(x) w(x) T3: r(y) w(y) T1 T2T3 T11: r(x) w(x) T12: r(y) w(y) T11T12 T3T2 S C C T11: r(x) T12: w(x) T13: r(y) w(y) T12T13 T3T2 S C C T11 S C CORRECT NOT CORRECT S

© Dennis Shasha, Philippe Bonnet 2001 Chopping Example T1: RW(A) RW (B) T2: RW(D) RW(B) T3: RW(E) RW(C) T4: R(F) T5: R(E) T6: R(A) R(F) R(D) R(B) R(E) R(G) R(C)

© Dennis Shasha, Philippe Bonnet 2001 Chopping Example T1T2 T4 T5 T3 T62T61 T61: R(A) R(F) R(D) R(B) T62: R(E) R(G) R(C) CC C C C S

© Dennis Shasha, Philippe Bonnet 2001 Finest Chopping A private chopping of transaction Ti, denoted private(Ti) is a set of pieces {ci1, ci2, …, cik} such that: –{ci1, ci2, …, cik} is a rollback safe chopping –There is no SC-cycle in the graph whose nodes are {T1, …, Ti-1, ci1, ci2, …, cik, Ti+1, … Tn} The chopping consisting of {private(T1), private(T2), …, private(T2)} is rollback-safe and has no SC-cycles.

© Dennis Shasha, Philippe Bonnet 2001 Finest Chopping In: T, {T1,.. Tn-1} Initialization –If there are abort commands then p_1 := all writes of T (and all non swappable reads)that may occur before or concurrently with any abort command in T else p_1 := first database access –P := {x | x is a database operation not in p_1} –P := P {p_1}

© Dennis Shasha, Philippe Bonnet 2001 Finest Chopping Merging pieces –Construct the connected components of the graph induced by C edges alone on all transactions {T1, …, Tn-1} and on the pieces in P. –Update P based on the following rule: If p_j and p_k are in the same connected component and j < k, then –add the accesses from p_k to p_j –delete p_k from P

© Dennis Shasha, Philippe Bonnet 2001 Sacrificing Isolation for Performance A transaction that holds locks during a screen interaction is an invitation to bottlenecks –Airline Reservation 1.Retrieve list of seats available 2.Talk with customer regarding availability 3.Secure seat Single transaction is intolerable, because each customer would hold lock on seats available. Keep user interaction outside a transactional context Problem: ask for a seat but then find it’s unavailable. More tolerable.

© Dennis Shasha, Philippe Bonnet 2001 Isolation Levels Read Uncomitted (No lost update) –Exclusive locks for write operations are held for the duration of the transactions –Lock for writes until commit time. No locks for reads Read Committed (No inconsistent retrieval) –Lock for writes until commit time. –Shared locks are released as soon as the read operation terminates. Repeatable Read (no unrepeatable reads) –Strict two phase locking: lock for writes and reads until commit time. Serializable (no phantoms) –Table locking or index locking to avoid phantoms

© Dennis Shasha, Philippe Bonnet 2001 Value of Serializability -- Data Settings: accounts( number, branchnum, balance); create clustered index c on accounts(number); – rows –Cold buffer; same buffer size on all systems. –Row level locking –Isolation level (SERIALIZABLE or READ COMMITTED) –SQL Server 7, DB2 v7.1 and Oracle 8i on Windows 2000 –Dual Xeon (550MHz,512Kb), 1Gb RAM, Internal RAID controller from Adaptec (80Mb), 4x18Gb drives (10000RPM), Windows 2000.

© Dennis Shasha, Philippe Bonnet 2001 Value of Serializability -- transactions Concurrent Transactions: –T1: summation query [1 thread] select sum(balance) from accounts; –T2: swap balance between two account numbers (in order of scan to avoid deadlocks) [N threads] valX:=select balance from accounts where number=X; valY:=select balance from accounts where number=Y; update accounts set balance=valX where number=Y; update accounts set balance=valY where number=X;

2 - Tuning the Guts © Dennis Shasha, Philippe Bonnet Value of Serializability -- results With SQL Server and DB2 the scan returns incorrect answers if the read committed isolation level is used (default setting) With Oracle correct answers are returned (snapshot isolation), but beware of swapping

2 - Tuning the Guts © Dennis Shasha, Philippe Bonnet Cost of Serializability Because the update conflicts with the scan, correct answers are obtained at the cost of decreased concurrency and thus decreased throughput.

Weaken isolation guarantees carefully Begin with the highest degree of isolation (serializable in relational systems). If a given transaction (usually a long one) either suffers extensive deadlocks or causes significant blocking, consider weakening the degree of isolation, but do so with the awareness that the answers may be off slightly. © Dennis Shasha, Philippe Bonnet 2001

Control the granularity of locking DBMS offers different granularities of locks –Record/row-level, table-level, page-level, Intuitively, Record-level locking is better, since it permit two different transactions to access different records on the same page. However, in real DBMS, row lock is rarely more expensive than table lock, since the recovery overhead dominate the overall overhead Sql server and DB2 provide lock escalation –Automatically upgrades row-level locks into a single table-level lock when the number f row-level locks reaches a predefined threshold –Escalation may lead to deadlock –The lesson is :Long transactions should use table locks mostly to avoid deadlocks, and short transactions should use record locks to enhance concurrency –a long transaction is one that accesses nearly all the pages of the table © Dennis Shasha, Philippe Bonnet 2001

A If object not found in hash table, it is unlocked Lock info for A A... H Lock Table

© Dennis Shasha, Philippe Bonnet 2001 Locking in SQL Server 7 syslockinfo dbidobjid lock granularity lock ownerlock waiter lock mode 1117RID X PAG IX TAB IX RID S LO1 LO2, LO3 LW2 LW3 spid 10 Lock – 32 bytes Lock owner block – 32 bytes Lock waiter block – 32 bytes LW1, LW4

© Dennis Shasha, Philippe Bonnet 2001 Locking in Oracle 8i Data page row Interested transaction list (fixed array - INITRANS – MAXTRANS) T1 T1 lock Enqueue resource structure (fixed array – default 4 entries per transaction) T1 Enqueued locks array T2 lock Enqueue wait (time out ~ 3sec) Process T3 lock Deadlock detection H

© Dennis Shasha, Philippe Bonnet 2001 Locking Overhead -- data Settings: accounts( number, branchnum, balance); create clustered index c on accounts(number); – rows –Cold buffer –SQL Server 7, DB2 v7.1 and Oracle 8i on Windows 2000 –No lock escalation on Oracle; Parameter set so that there is no lock escalation on DB2; no control on SQL Server. –Dual Xeon (550MHz,512Kb), 1Gb RAM, Internal RAID controller from Adaptec (80Mb), 4x18Gb drives (10000RPM), Windows 2000.

© Dennis Shasha, Philippe Bonnet 2001 Locking Overhead -- transactions No Concurrent Transactions: –Update [ updates] update accounts set balance = Val; –Insert [ inserts], e.g. typical one: insert into accounts values(664366,72255, );

2 - Tuning the Guts © Dennis Shasha, Philippe Bonnet Locking Overhead Row locking is barely more expensive than table locking because recovery overhead is higher than locking overhead –Exception is updates on DB2 where table locking is distinctly less expensive than row locking. (DB2 use logical logging, which logs the operation that caused change instead of image of changed data)

Data definition language (DDL) statements are considered harmful DDL is the language used to access and manipulate catalog or metadata The catalog can easily become a hot spot and therefore a bottleneck The lesson is: avoid updates to the system catalog during heavy system activity, especially if you are using dynamic SQL © Dennis Shasha, Philippe Bonnet 2001

Think about partitioning Problem: insert a collection records into history file or security file, or log file –Time dependent insertion –Last page of the file may become a concurrency bottleneck Solution: partition insertions to the file across different pages and possibly different disks –Set up many insertion points and insert into them randomly –Set up a clustering index based on some attribute that is not correlated with the time of insertion. –Hash the time of insertion and use that as the clustering key. © Dennis Shasha, Philippe Bonnet 2001

Multiple insertion points and page locking “sequential” a clustered index defined on an attribute whose value increases (or even decreases) with time “Nonsequential” denotes that independent of time © Dennis Shasha, Philippe Bonnet 2001

Multiple insertion points and row locking Row locking avoids contention between successive insertions © Dennis Shasha, Philippe Bonnet 2001

Circumventing hot spots A hot spot is a piece of data that is accessed by many transactions and is updated by some. Hot spots cause bottlenecks because each updating transaction must complete before any other transaction can obtain a lock on the hot data item. Solution –Use partitioning to eliminate it –Access the hot spot as late as possible in the transaction –Use special database management facilities © Dennis Shasha, Philippe Bonnet 2001

2 - Tuning the Guts © Dennis Shasha, Philippe Bonnet Logical Bottleneck: Sequential Key generation Consider an application in which one needs a sequential number to act as a key in a table, e.g. invoice numbers for bills. Ad hoc approach: a separate table holding the last invoice number. Fetch and update that number on each insert transaction. –The last invoice number becomes a bottleneck due to two-phase locking Counter approach: use facility such as Sequence (Oracle)/Identity(SQL Server).

© Dennis Shasha, Philippe Bonnet 2001 Latches and Locks Locks are used for concurrency control –Is held until commit the transaction –Requests for locks are queued Priority queue –Lock data structure Locking mode, lock granularity, transaction id. Lock table Latches are used for mutual exclusion –A latch is released immediately after access –Requests for latch succeeds or fails Active wait (spinning) on latches on multiple CPU. –Single location in memory Test and set for latch manipulation

© Dennis Shasha, Philippe Bonnet 2001 Counter Facility -- data Settings: –default isolation level: READ COMMITTED; Empty tables –Dual Xeon (550MHz,512Kb), 1Gb RAM, Internal RAID controller from Adaptec (80Mb), 4x18Gb drives (10000RPM), Windows accounts( number, branchnum, balance); create clustered index c on accounts(number); counter ( nextkey ); insert into counter values (1);

© Dennis Shasha, Philippe Bonnet 2001 Counter Facility -- transactions No Concurrent Transactions: –System [ inserts, N threads] SQL Server 7 (uses Identity column) insert into accounts values (94496,2789); Oracle 8i insert into accounts values (seq.nextval,94496,2789); –Ad-hoc [ inserts, N threads] begin transaction NextKey:=select nextkey from counter; update counter set nextkey = NextKey+1; commit transaction begin transaction insert into accounts values(NextKey,?,?); commit transaction

© Dennis Shasha, Philippe Bonnet 2001 Avoid Bottlenecks: Counters System generated counter (system) much better than a counter managed as an attribute value within a table (ad hoc). Counters may miss ids, however which is tolerable in many real apps