Download presentation
Presentation is loading. Please wait.
Published byElizabeth Gallagher Modified over 8 years ago
2
Maciej Pilecki Project Botticelli Ltd. Session Code: DAT402
3
SELECT About FROM Speakers WHERE Name = ’Maciej Pilecki’ Over 10 years of SQL Server experience SQL Server MVP since Jan 2006 Specializing in SQL Server database development, administration, performance tuning and troubleshooting Delivering consulting services around the world (special discounted rate for TechEd attendees) Frequent speaker at many international conferences Living in Wroclaw, Poland
4
Agenda The basics of transaction processing Transaction logging and recovery Isolation levels Transactions and performance Error handling Nested transactions Distributed transactions Coding transactions
5
The Basics In database world, everything is a transaction!
6
Meet our hero: Little XACT INSERT INTO T1 VALUES (1,'ABC')
7
ACID properties Atomicity Consistency Isolation Durability
8
ACID properties Atomicity – transaction management Consistency – data integrity Isolation – locks, isolation levels Durability – logging and recovery
9
Anatomy of a data modification Source: SQL 2008 Books Online
10
Write-ahead logging (WAL) Log records written to disk before data pages Allows database recovery Ensures D in ACID (Durability) Log writes use synchronous I/O Data page writes use asynchronous I/O
11
Transaction log So, is your transaction log important? Even more than the data files! There is no database without the log Disk performance crucial (sequential writes) Backup frequently! Don't let it run out of space (KB 873235) Every transaction is logged, but… sys.fn_dblog(NULL, NULL) (undocumented)
12
Recovery Models Behavior of logging depends on Recovery Model of the database Full Bulk-logged Simple Special Recovery Model for tempdb
13
Database Recovery Process of bringing the database to a consistent state (after crash, restart, restore) Runs every time you bring the database online Rolls forward (redo) committed transactions Rolls back (undo) un-committed transactions Not to confuse with Backup Restore
14
Tran 5 Recovery Tran 1 Tran 2 Tran 3 Tran 4 Tran 4 CrashCheckpoint Undo tran 4 Undo tran 5 Redo Tran 2 Redo Tran 2 Redo Tran 3 Redo Tran 3
15
Types of transactions Autocommit transactions Explicit transactions Implicit transactions Batch-scoped transactions (MARS) Distributed transactions
16
Little XACT grows bigger BEGIN TRANSACTION INSERT INTO T1 VALUES (1,'ABC') INSERT INTO T2 VALUES (2,'EFG') COMMIT TRANSACTION
17
Concurrency effects Lost update Dirty read Nonrepeatable read Phantom reads Missing or double-read rows
18
Isolation levels Lock-based: Read uncommitted Read committed (default) Repeatable read Serializable Version-based (SQL 2005+) Read committed snapshot Snapshot
19
Isolation Concurrency effects in isolation levels Isolation levelDirty Read Nonrepeatable read Phantom readMissing rows Read uncommittedYesYes Read committed (inc. snapshot) NoYes Yes Repeatable readNoNo YesNo SerializableNoNo SnapshotNoNo
20
Transactions and performance Commit requires synchronous disk write Transaction log I/O throughput important! Number and size of transactions will affect performance (YMMV) Higher isolation = more locking = less concurrency Need to find a balance Don’t depend on auto-commit Control the size of your transactions!
21
Error handling No atomicity guaranteed without error handling Pre-2005, use @@ERROR (ugly but works) 2005 and later, use TRY/CATCH SET XACT_ABORT for auto rollback @@TRANCOUNT and XACT_STATE() Make sure you have a retry logic for deadlocks Application-side error handling important too! Good read: http://www.sommarskog.se/error-handling-I.htm
22
Deadlocks Can not really (dependably) be avoided Well unless: Set database read-only Set database single-user But proven techniques exists to minimize chances SET DEADLOCK_PRIORITY SQL Profiler Deadlock Graph helps Handle error 1204
23
Little XACT learns a lesson SET XACT_ABORT ON BEGIN TRY BEGIN TRANSACTION INSERT INTO T1 VALUES (1,'ABC') INSERT INTO T2 VALUES (2,'EFG') COMMIT TRANSACTION END TRY BEGIN CATCH IF XACT_STATE() = -1 ROLLBACK TRANSACTION END CATCH
24
Nested transactions Outer transaction: BEGIN TRAN COMMIT TRAN ROLLBACK TRAN Outer transaction: BEGIN TRAN COMMIT TRAN ROLLBACK TRAN Inner transaction: BEGIN TRAN INSERT … UPDATE … COMMIT TRAN ROLLBACK TRAN Inner transaction: BEGIN TRAN INSERT … UPDATE … COMMIT TRAN ROLLBACK TRAN CommitCommit CommitCommitRollbackRollback RollbackRollback
25
Nesting transactions Tricky! The outer-most transaction controls everything COMMIT applies to inner-most transaction ROLLBACK applies to outer-most transaction Careful with SPs and triggers Need to control @@TRANCOUNT Will generate error 266 (SP) or 3609 (trigger) Use Savepoints to control what is rolled back
26
Autonomous transactions No support for autonomous transactions yet No way to commit inner transaction independently of the outer transaction Connect item to vote for: http://connect.microsoft.com/SQLServer/feedback/ViewFeedback. aspx?FeedbackID=296870 Table variables survive transaction rollback (can be used to hold the data)
27
Distributed transactions Any transaction that spans databases is a distributed transaction Within one instance, handled internally Across instances, uses Distributed Transaction Coordinator (DTC) Use two-phase commit (2PC): Prepare phase Commit phase in-doubt xact resolution option
28
Little XACT visits a friend SET XACT_ABORT ON BEGIN TRY BEGIN TRANSACTION INSERT INTO T1 VALUES (1,'ABC') INSERT INTO SERVER2.MyDB.dbo.T2 VALUES (2,'EFG') COMMIT TRANSACTION END TRY BEGIN CATCH IF XACT_STATE() = -1 ROLLBACK TRANSACTION END CATCH
29
Coding efficient transactions Keep it short! No user input or interaction! Limit the amount of data accessed Use appropriate isolation level Use locking hints to customize Don’t forget error handling
30
Ways to get some interesting info sys.dm_tran_active_transactions sys.dm_tran_database_transactions sys.dm_tran_session_transactions sys.dm_tran_locks DBCC OPENTRAN SQLTransaction Profiler event
31
Summary The basics of transaction processing Transaction logging and recovery Isolation levels Transactions and performance Error handling Nested transactions Distributed transactions Coding transactions
33
Where to find me Come to my last session: DAT404 Masterclass: Microsoft SQL Server Execution Plans, from Compilation to Caching to Reuse – Friday 13:00-14:15 New York 2 Come to TLC to ask questions: Friday, 11:30 – 12:30 By email: maciej@projectbotticelli.com
34
www.microsoft.com/teched Sessions On-Demand & Community http://microsoft.com/technet Resources for IT Professionals http://microsoft.com/msdn Resources for Developers www.microsoft.com/learning Microsoft Certification & Training Resources Resources Required Slide Speakers, TechEd 2009 is not producing a DVD. Please announce that attendees can access session recordings at TechEd Online. Required Slide Speakers, TechEd 2009 is not producing a DVD. Please announce that attendees can access session recordings at TechEd Online.
35
Complete an evaluation on CommNet and enter to win an Xbox 360 Elite!
36
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. Required Slide
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.