view以前のバージョンで利用できた sp_lock システム ストアド プロシージャは、SQL Server 2008 R2 でも利用することができますが、あくまでも下位互換性のために用意されたものなので、将来のバージョンでは削除される予定です。

  • Published on
    12-Mar-2018

  • View
    220

  • Download
    2

Embed Size (px)

Transcript

SQL Server 2008 R2 No.16

Published: 2008 5 31

SQL Server 2008 R2 : 2010 6 24

SQL Server 2008 No.1 SQL Server

2

( Microsoft Corporation Microsoft MicrosoftSQL ServerVisual StudioWindowsWindows XPWindows ServerWindows Vista Microsoft Corporation Copyright 2010 Microsoft Corporation. All rights reserved.)

SQL Server 2008 R2 No.16

80

STEP 1. 4

1.1Lock5

1.26

STEP 2.7

2.18

2.2Management Studio 14

2.3SQL dm_tran_locks15

2.4Blocked process report 16

2.520

2.622

2.7Deadlock24

2.8Deadlock graph31

STEP 3. Isolation Level35

3.1 Isolation Level36

3.2 Read UnCommitted40

3.3 Non Repeatable Read44

3.453

3.5 56

3.6Phantom Read57

3.761

STEP 4. 64

4.1 65

4.269

4.3READ_COMMITED_SNAPSHOT70

4.4Snapshot Isolation Level73

4.577

STEP 1.

STEP

STEP

Lock

1

Lock

OS

Windows Server 2003 SP2Service Pack 2 Windows Server 2003 R2 SP2 Windows XP Professional SP3 Windows Vista Windows 7 Windows Server 2008 SP2 Windows Server 2008 R2

SQL Server 2008 R2

OS Windows Server 2008 R2x64 SQL Server 2008 R2 Enterprise x64

STEP 2.

STEP

STEP

Management Studio

dm_tran_locks

Blocked process report

Deadlock graph

SQL Server 2

eXclusive

UPDATEINSERTDELETEWrite

Shared

SELECT UPDATEINSERTDELETESELECTRead

SQL Server

Let's Try

1. Management Studio SQL Server

sampleDBt1

--

CREATE DATABASE sampleDB

go

-- t1

USE sampleDB

CREATE TABLE t1

( a int PRIMARY KEY

,b char(5) )

--

INSERT INTO t1 VALUES(1, 'AAA')

INSERT INTO t1 VALUES(2, 'BBB')

INSERT INTO t1 VALUES(3, 'CCC')

INSERT INTO t1 VALUES(4, 'DDD')

SELECT * FROM t1

1. a=2

USE sampleDB

BEGIN TRAN

UPDATE t1

SET b = 'xxx'

WHERE a = 2

BEGIN TRAN COMMIT TRAN

a=3

USE sampleDB

SELECT * FROM t1

WHERE a = 3

a=2a=3

a=2

USE sampleDB

SELECT * FROM t1

WHERE a = 2

..

Management Studio

Management Studio

IDSUSPENDEDLCK_M_Skeylock mode=X SELECTLCK_M_SLock Mode SharedSUSPENDED

ID

ROLLBACK TRAN

ROLLBACK TRAN

Management Studio

Management Studio

Management Studio sampleDB

UPDATE SELECT

Management Studio

SQL dm_tran_locks

SQL

SQL dm_tran_locks

SELECT * FROM sys.dm_tran_locks

Management Studio dm_tran_locks

Note sp_lock

sp_lock SQL Server 2008 R2 dm_tran_locks

Blocked process report

Blocked process report

Blocked process report SQL Server 2005 SQL Server Profiler

Let's Try

1. Blocked process report blocked process threshold

EXEC sp_configure 'show advanced options', '1'

RECONFIGURE

EXEC sp_configure 'blocked process threshold', '5'

RECONFIGURE

2threshold5

Microsoft SQL Server 2008 R2 SQL Server Profiler

SQL Server

Errors and WarningsBlocked process report

5

1a=2COMMIT TRAN

USE sampleDB

BEGIN TRAN

UPDATE t1

SET b = 'xxx'

WHERE a = 2

a=2

USE sampleDB

SELECT * FROM t1

WHERE a = 2

Blocked process report 5

Blocked process report XML

SQL

Blocked process report

ROLLBACK TRAN

ROLLBACK TRAN

EXEC sp_configure 'blocked process threshold', '0'

RECONFIGURE

EXEC sp_configure 'show advanced options', '0'

RECONFIGURE

SET LOCK_TIMEOUT

10100000-1

Let's Try

1. 1a=2COMMIT TRAN

USE sampleDB

BEGIN TRAN

UPDATE t1

SET b = 'xxx'

WHERE a = 2

10

SET LOCK_TIMEOUT 10000

a=2

USE sampleDB

SELECT * FROM t1

WHERE a = 2

10 ..

SET LOCK_TIMEOUT

ROLLBACK TRAN

ROLLBACK TRAN

Note ADO.NET

VB C# ADO.NET CommandTimeout 30

Dim cn As New SqlConnection("")

cn.Open()

cn.CommandTimeout =

CommandTimeout SET LOCK_TIMEOUT

cn.Open()

cn.ExecuteNonQuery "SET LOCK_TIMEOUT "

SQL Server

100 90SQL Server

Note

SQL Server 8

SQL Server

WITHUPDATE

UPDATE WITH(ROWLOCK)

SET .. WHERE ..

PAGLOCKTABLOCKUPDATEINSERTDELETE SELECT

JOIN

SELECT ..

FROM 1 WITH()

INNER JOIN 2 WITH()

ON .. WHERE ..

SQL Server SQL Server

100 90

Note

Table Lock Escalation/sec

Lock Escalation

Note Oracle

Oracle SQL Server ROWLOCKOracle

SQL Server 1211SQL Server SQL Server 2008 ALTER TABLE

ALTER TABLE

SET ( LOCK_ESCALATION = DISABLE )

Deadlock

2

X A Y BX BY Y AX

2SQL Server 5

SQL Server 1205

Note Oracle

Oracle SQL Server SQL Server Oracle Oracle

Cycle2 A B

1204 DBCC TRANCEON

DBCC TRACEON(1204, -1)

1204 SQL Server

SQL Server DBCC TRACEOFF

Let's Try

1. DBCC TRANCEON 1204

DBCC TRACEON(1204, -1)

1. a=2COMMIT TRAN

USE sampleDB

BEGIN TRAN

UPDATE t1

SET b = 'xxx'

WHERE a = 2

a=4COMMIT TRAN

USE sampleDB

BEGIN TRAN

UPDATE t1

SET b = 'yyy'

WHERE a = 4

a=4

UPDATE t1

SET b = 'yyy'

WHERE a = 4

2a=2

UPDATE t1

SET b = 'xxx'

WHERE a = 2

SQL Server 1205

SQL Server SQL Server

Deadlock encountered..Input Buf

Victim Resource Owner ID Victim

1204 SQL Server

ROLLBACK TRAN

DBCC TRACEON SQL Server DBCC TRACEOFF SQL Server

SQL Server SQL Server Configuration ManagerSQL Server ;-T 1204

SQL Server SQL Server DBCC TRACESTATUS

DBCC TRACESTATUS (1204)

Status 1 1204

SET DEADLOCK_PRIORITY

SET DEADLOCK_PRIORITY n

n -1010

Deadlock graph

Deadlock graph

Deadlock graph SQL Server 2005

Let's Try

1. SQL Server Profiler

1.

SQL Server

LocksDeadlock graph

Deadlock graph

Deadlock graph ID SQL

Deadlock graph

ROLLBACK TRAN

STEP 3. Isolation Level

STEP Isolation Level

STEP

Isolation Level

Read UnCommitted

Non Repeatable Read

Phantom Read

Isolation Level

1Serial

Isolation

1

Isolation Level

SQL ANSI SQL-92Isolation 4 Isolation Level

43

Serializable IsolationSerialize able

SQL Server OracleDB2 Read Committed

SQL Server

SQL Server WITH

SELECT .. FROM WITH()

4

ReadUnCommittedNOLOCK

ReadCommitted

RepeatableRead

SerializableHOLDLOCK

SET

SET TRANSACTION ISOLATION LEVEL

SET

Read UnCommitted

Read Committed

Repeatable Read

Serializable

Note ADO.NET

VB C# ADO.NET 2.0 TransactionScope Serializable SELECT Read Committed TransactionOptions IsolationLevel

Imports System.Data.SqlClient

Imports System.Transactions

Using cn As New SqlConnection("Server=localhost;Database=sampleDB;Integrated Security=SSPI;")

Try

'

Dim txOp As New TransactionOptions

txOp.IsolationLevel = IsolationLevel.ReadCommitted

Using tx As New TransactionScope(TransactionScopeOption.Required, txOp)

cn.Open()

Using cmd As New SqlCommand()

cmd.Connection = cn

cmd.CommandText = "SQL "

cmd.ExecuteNonQuery()

ADO.NET 1.1 SqlTransaction

ADO.NET 1.1 SqlTransaction Read Committed Read UnCommitted SqlConnection IsolationLevel

Imports System.Data.SqlClient

Using cn As New SqlConnection("Server=localhost;Database=sampleDB;Integrated Security=SSPI;")

cn.Open()

'

cn.IsolationLevel = adXactReadUncommitted

Using cmd As New SqlCommand()

cmd.Connection = cn

Dim tx As SqlTransaction = cn.BeginTransaction()

cmd.Transaction = tx

Try

cmd.CommandText = "SQL "

cmd.ExecuteNonQuery()

COM+

COM+ Serializable

Read UnCommitted

Read UnCommitted

Dirty ReadRead UnCommitted UnCommitted

Read UnCommitted

UnCommitted

Let's Try

1. t1 1a=2COMMIT TRAN

USE sampleDB

BEGIN TRAN

UPDATE t1

SET b = 'xxx'

WHERE a = 2

a=2

USE sampleDB

SELECT * FROM t1

WHERE a = 2

Step ..Read CommittedCommitted

Read UnCommitteda=2

SELECT * FROM t1 WITH(ReadUnCommitted)

WHERE a = 2

NoLock

SELECT * FROM t1 WITH(NoLock)

WHERE a = 2

Read UnCommitted NoLock Read UnCommitted

ROLLBACK TRAN

ROLLBACK TRAN

2xxxBBB

Read UnCommitted

Note Read UnCommitted

Read UnCommitted UnCommitted

Read UnCommitted Read UnCommitted

Read UnCommittedWITH(NoLock) Read UnCommitted NoLock

Read CommittedRead Committed UnCommitted

CommittedRead Committed

Read Committed Step 2 SELECT LCK_M_S

Non Repeatable Read

Non Repeatable ReadRead Committed

12

3 2

SELECT

Let's Try

1. sampleDB

USE sampleDB

CREATE TABLE

( int PRIMARY KEY

, varchar(50) )

INSERT INTO VALUES(1, NULL)

INSERT INTO VALUES(2, 'sato_t')

INSERT INTO VALUES(3, NULL)

INSERT INTO VALUES(4, NULL)

SELECT * FROM

1. 2 SELECT COMMIT TRAN

USE sampleDB

BEGIN TRAN

SELECT * FROM

1=3

-- 3

UPDATE

SET = 'xxx'

WHERE = 3

--

COMMIT TRAN

COMMIT TRAN 1=3

2=3

-- 3

UPDATE

SET = 'yyy'

WHERE = 3

--

COMMIT TRAN

2

Non Repeatable Read

=3 NULL

-- 3 NULL

UPDATE

SET = NULL

WHERE = 3

Repeatable Read

Repeatable Read

1121

Let's Try

1. 2 SELECT COMMIT TRAN Repeatable Read

USE sampleDB

BEGIN TRAN

SELECT * FROM WITH(RepeatableRead)

1=3

-- 3

UPDATE

SET = 'xxx'

WHERE = 3

--

COMMIT TRAN

2=3

-- 3

UPDATE

SET = 'yyy'

WHERE = 3

--

COMMIT TRAN

SQL Server

1

SELECT * FROM

Repeatable Read

=314

2

=3 NULL

-- 3 NULL

UPDATE

SET = NULL

WHERE = 3

Repeatable Read

Repeatable Read Serializable

Repeatable ReadUPDATE

4SQL Server 4 1 3 SQL Server 5 1

2 SQL Server 5 111

14 5 3 15 3

Repeatable Read Repeatable Read

Repeatable Read Update Lock

SELECT WITH(UPDLOCK) X Y

Y X

Note Oracle SELECT .. FOR UPDATE

UPDLOCKOracle SELECT .. FOR UPDATE

Let's Try

1. 2 SELECT COMMIT TRAN UPDLOCK

USE sampleDB

BEGIN TRAN

SELECT * FROM WITH(UPDLOCK)

2

1=3

-- 3

UPDATE

SET = 'xxx'

WHERE = 3

--

COMMIT TRAN

2=3

UPDLOCK SELECT

SELECT

=3 NULL

-- 3 NULL

UPDATE

SET = NULL

WHERE = 3

X Y SET LOCK_TIMEOUT

X

ASP ASP.NET Web Web

Phantom Read

Phantom ReadDELETEINSERTPhantom

DELETE Repeatable Read INSERT INSERT

Let's Try

1. 2

1. 1SELECT t1b='AAA'COMMIT TRAN

USE sampleDB

BEGIN TRAN

SELECT * FROM t1

WHERE b = 'AAA'

2 1

USE sampleDB

BEGIN TRAN

INSERT INTO t1 VALUES(5, 'AAA')

COMMIT TRAN

SELECT * FROM t1

1b='AAA'

SELECT * FROM t1

WHERE b = 'AAA'

1 2

Serializable

Serializable

1.

2.

1 Repeatatble Read 2 SerializableSerializable INSERT Range

Let's Try

1. 2

1. 1SELECT t1b='AAA'COMMIT TRAN Serializable

USE sampleDB

BEGIN TRAN

SELECT * FROM t1 WITH(Serializable)

WHERE b = 'AAA'

2 1

USE sampleDB

BEGIN TRAN

INSERT INTO t1 VALUES(6, 'AAA')

COMMIT TRAN

INSERT Serializable

Serializable

Repeatable Read Serizalizable Serizalizable

Note TransactionScope COM+ Serializable

Note TransactionScope COM+ Serizalizable

INSERT UNIQUE

vs.

UPDLOCKPessimistic Concurrency Control

Optimistic Concurrency Control

2

1.

2.

2

1.

UPDATEWHERE

Note ASP.NET

ASP.NET 2.0 ASP.NET 1.x UPDATE

2.

1

GETDATE UPDATE WHERE

Note ADO.NET 0.333

ADO.NET datetime 30010.3332008/6/5 04:30:12.467467 WHERE

300 1 CONVERT

SELECT a, b, CONVERT(char(11),,111) + CONVERT(char(12),,114) As

FROM t1

CONVERT 3111 yy/mm/dd 114 hh:mi:ss:mmm CONVERT datetime Transact-SQL

Step Repeatable Read Serializable

Read Committed

Read UnCommitted NOLOCK

UPDLOCK

Read UnCommitted UPDLOCK

STEP 4.

STEP SQL Server 2005

STEP

READ_COMMITED_SNAPSHOT

Snapshot Isolation Level

SQL Server

t1abXa=7Yb='CCC'SQL Server bb='CCC'

Xa=7bGGGYb='CCC'Ya=7b

1

SQL Server 2005 READ COMMITTED SNAPSHOT 2Oracle

Management Studio

Note Clustered Index Scan

PRIMARY KEY Clustered Index Scan

Clustered Index Scan Clustered Index Seek

6

1.

2. WHERE

3.

4. Repeatable Read Serializable

5. UPDLOCK

6. NOLOCK

46 Step

1.

WHERE b

b='CCC'Index Seek

2

FORCESEEK Index Seek SQL Server 2008

WHERE

READ COMMITTED SNAPSHOT

2. WHERE

WHERE

Oracle SELECT

SQL Server 2005 READ_COMMITTED_SNAPSHOT 2 2SQL Server 2000

Oracle

READ_COMMITED_SNAPSHOT

READ_COMMITED_SNAPSHOT

READ_COMMITED_SNAPSHOT SELECT Oracle

tempdb Oracle UNDO Oracle 8i

READ_COMMITTED_SNAPSHOT

READ_COMMITTED_SNAPSHOT

ALTER DATABASE

SET READ_COMMITTED_SNAPSHOT ON

Let's Try

1. sampleDBREAD_COMMITTED_SNAPSHOT ON

ALTER DATABASE sampleDB

SET READ_COMMITTED_SNAPSHOT ON

sampleDB

1. t1

1. a=2COMMIT TRAN

USE sampleDB

BEGIN TRAN

UPDATE t1

SET b = 'xxx'

WHERE a = 2

1. 2a=2

USE sampleDB

SELECT * FROM t1

WHERE a = 2

BBB

ROLLBACK TRAN

ROLLBACK TRAN

2BBB

READ_COMMITTED_SNAPSHOT Oracle Oracle SQL Server

Snapshot Isolation Level

SQL Server Oracle READ_ONLY Serializable

READ_COMMITTED_SNAPSHOT

READ_COMMITTED_SNAPSHOT SELECT

ALLOW_SNAPSHOT_ISOLATION ON

ALTER DATABASE

SET ALLOW_SNAPSHOT_ISOLATION ON

SELECT Isolation LevelSNAPSHOT

SET TRANSACTION ISOLATION LEVEL SNAPSHOT

Let's Try

1. sampleDB

ALTER DATABASE sampleDB

SET ALLOW_SNAPSHOT_ISOLATION ON

sampleDB

1. t1

1. a=2COMMIT TRAN

USE sampleDB

BEGIN TRAN

UPDATE t1

SET b = 'xxx'

WHERE a = 2

1. 2a=2

--

SET TRANSACTION ISOLATION LEVEL SNAPSHOT

USE sampleDB

BEGIN TRAN

SELECT * FROM t1

WHERE a = 2

READ_COMMITTED_SNAPSHOT

COMMIT TRAN

COMMIT TRAN

2a=2

BBB

2 COMMIT TRAN

COMMIT TRAN

READ_COMMITTED_SNAPSHOT tempdb Version Storetempdb

tempdbVersion Store

tempdb dm_tran_top_version_generatorsdm_tran_version_store 2DMVDynamic Management View

dm_tran_top_version_generators

Store

tempdb

dm_tran_version_store

Version Store Store 1

11 Version Store

READ_COMMITTED_SNAPSHOT

READ_COMMITTED_SNAPSHOT 1

READ_COMMITTED_SNAPSHOT tempdb

tempdb Sort Hash

SQL Server 4

SQL Server Include FILLFACTOR

SQL Server

Transact-SQL

SQL Server Transact-SQLSQL Server SQL Server Transact-SQL

Transact-SQL

Transact-SQL SQL Transact-SQL

http://www.sqlquality.com/

SQLQuality SQL Server SQL Server OS .NET SQL Server 15 100

SQL Server 2...

Recommended

View more >