SQL Server - Bài
Managing Transaction and LockVu Tuyet Trinhtrinhvt-fit@mail.hut.edu.vnHanoi University of Technology1 MicrosoftMicrosoftTransactionA sequence of tasks that together constitute a logical unit of workEnsuring the highest level of integrity and consistency within a databaseUsed when statements are processed MicrosoftMicrosoftACID PropertiesAtomicityAll or noneConsistencyLeave data in a consistent stateIsolationDon’t show data that’s ½ doneDurabilityModifications are permanent MicrosoftMicrosoftOverview√TransactionReview of Transaction LogIntroduction to Transactions and LocksManaging TransactionsSQL Server Locking & Managing Locks MicrosoftMicrosoftTransaction Log ArchitectureWrite-Ahead transaction logFlushing the page and dirty PagesUse of checkpointsMinimize what must be processed to recoverTruncating the transaction logShrinking the transaction logDBCC SHRINKDATABASEDBCC SHRINKFILE MicrosoftMicrosoftTransaction Recovery and Checkpoints Transaction Recovery Action RequiredNoneCheckpoint System Failure1122334455Roll forwardRoll backRoll forwardRoll back MicrosoftMicrosoftConcurrency ControlPessimistic concurrency controlHigh contention for dataOptimistic concurrency controlLow contention for data MicrosoftMicrosoftLockable Resources ItemItem DescriptionDescriptionRID Row identifierKey Row lock within an indexPageExtentTableData page or index pageGroup of pagesEntire tableDatabase Entire database MicrosoftMicrosoftTypes of LocksBasic locksShared (LS)Exclusive (LX)Special situation locksIntentUpdateSchemaBulk update MicrosoftMicrosoftLock CompatibilityLocks may or may not be compatible with other LocksExamplesShared locks are compatible with all locks except exclusiveExclusive locks are not compatible with any other locksUpdate locks are compatible only with shared locksfalsefalseLXfalsetrueLSLXLS [...]... blocking each other Deadlock How SQL server ends a deadlock How to minimize deadlocks Access objects in same order Keep transactions short Use low isolation level Use bound connections How to customize the lock time-out setting Microsoft SET LOCK_TIMEOUT function Remarks Keep transactions short Design transactions to minimize deadlocks Use SQL Server defaults for locking Be careful...Managing Locks Session-level locking options – READ UNCOMMITTED – READ COMMITTED (default) – REPEATABLE READ – SNAPSHOT – SERIALIZABLE Locking timeout – Limits time waiting for a locked resource – SET LOCK_TIMEOUT Microsoft Viewing Locking Information • Activity Monitor • sys.dm_tran_locks Dynamic Management View • EnumLocks • SQL Server Profiler • System Monitor Microsoft Considerations... ON ALTER DATABASE CREATE DELETE DROP FETCH GRANT ∙ ∙ ∙ ∙ ∙ ∙ INSERT OPEN REVOKE SELECT TRUNCATE TABLE UPDATE Transaction must be explicitly completed with COMMIT or ROLLBACK TRANSACTION Microsoft SQL Server Locking Microsoft Concurrency Problems Prevented by Locks Dealing with Deadlocks Concurrency Problems Prevented by Locks Lost update Uncommitted dependency (Dirty Read) Selecting... EnumLocks • SQL Server Profiler • System Monitor Microsoft Considerations for Using Transactions Transaction guidelines Keep transactions as short as possible Use caution with certain Transact -SQL statements Avoid transactions that require user interaction Issues in nesting transactions Allowed, but not recommended Use @@trancount to determine nesting level Microsoft Transaction Types . and LocksManaging Transactions SQL Server Locking & Managing Locks MicrosoftMicrosoftTransaction Log ArchitectureWrite-Ahead transaction logFlushing. Activity Monitor • sys.dm_tran_locks Dynamic Management View • EnumLocks• SQL Server Profiler • System Monitor MicrosoftMicrosoftConsiderations for Using