DAT 342 Advanced SQL Server Performance and Tuning Bren Newman Program Manager SQL Server...
-
Upload
kamron-fallows -
Category
Documents
-
view
246 -
download
5
Transcript of DAT 342 Advanced SQL Server Performance and Tuning Bren Newman Program Manager SQL Server...
DAT 342DAT 342 Advanced SQL Server Advanced SQL Server Performance and TuningPerformance and Tuning
Bren NewmanBren NewmanProgram ManagerProgram ManagerSQL Server DevelopmentSQL Server DevelopmentMicrosoft CorporationMicrosoft Corporation
AgendaAgenda
Performance OverviewPerformance Overview Performance ToolsetPerformance Toolset Resource QueuingResource Queuing Concurrency issuesConcurrency issues Q & AQ & A
Performance OverviewPerformance Overview
When under load, When under load, everyevery system has a system has a bottleneck that limits performancebottleneck that limits performance May depend on workload, time of dayMay depend on workload, time of day E.g. OLTP and Batch may have different profiles E.g. OLTP and Batch may have different profiles
and bottlenecksand bottlenecks
Fix one bottleneck, and another Fix one bottleneck, and another surfaces under loadsurfaces under load CPU, I/O, Locking (concurrency), NetworkCPU, I/O, Locking (concurrency), Network
In a perfect world, CPU is the last In a perfect world, CPU is the last bottleneckbottleneck
Performance ToolsetPerformance Toolset
PerfmonPerfmon Virtual tables (and Stored Procs)Virtual tables (and Stored Procs) FileStatsFileStats DBCCDBCC ProfilerProfiler
Perf Toolset: PERFMONPerf Toolset: PERFMON
System objects such asSystem objects such as System, processor, IO, cache, memorySystem, processor, IO, cache, memory
SQL Server objectsSQL Server objects Overall SQL Server performanceOverall SQL Server performance Examples: data access, buffer, locks, Examples: data access, buffer, locks,
cache, memory, statistics, etc.cache, memory, statistics, etc.
Perf Toolset: Virtual tablesPerf Toolset: Virtual tables
SyslockinfoSyslockinfo Dbid, object, index, type, statusDbid, object, index, type, status
SysprocessesSysprocesses Waittype, waittime, lastwaittypeWaittype, waittime, lastwaittype
SysperfinfoSysperfinfo SQL counters exposed to PERFMONSQL counters exposed to PERFMON
Perf Toolset: FileStatsPerf Toolset: FileStats
::fn_virtualfilestats::fn_virtualfilestats Physical IO per filePhysical IO per file
SQL Server IO onlySQL Server IO only
Compare SQL IO to Compare SQL IO to PERFMONPERFMON PhysicalDisk:Disk PhysicalDisk:Disk
Reads/sec Reads/sec PhysicalDisk:Disk PhysicalDisk:Disk Writes/secWrites/sec
PhysicalDisk:Disk PhysicalDisk:Disk Read Bytes/secRead Bytes/sec
PhysicalDisk:Disk PhysicalDisk:Disk Write Bytes/secWrite Bytes/sec
select DbIdselect DbId,FileId,FileId
,TimeStamp,TimeStamp
,NumberReads,NumberReads
,NumberWrites,NumberWrites
,BytesRead,BytesRead
,BytesWritten,BytesWritten
,IoStallMS ,IoStallMS
from from ::fn_VirtualFileStats::fn_VirtualFileStats
(DbId,FileId)(DbId,FileId)
-- IoStallMS <= 20ms for log-- IoStallMS <= 20ms for log
Perf Toolset: DBCCPerf Toolset: DBCC
DBCC SQLPERFDBCC SQLPERF LogspaceLogspace Umsstats – sql thread managementUmsstats – sql thread management Waitstats – resources, wait typesWaitstats – resources, wait types
Other DBCC commandsOther DBCC commands
Relevant DBCC CommandsRelevant DBCC Commands DBCC MEMORYSTATUSDBCC MEMORYSTATUS DBCC SQLMGRSTATSDBCC SQLMGRSTATS DBCC CACHESTATSDBCC CACHESTATS DBCC SHOW_STATISTICSDBCC SHOW_STATISTICS DBCC INPUTBUFFERDBCC INPUTBUFFER DBCC DROPCLEANBUFFERSDBCC DROPCLEANBUFFERS DBCC FREEPROCCACHEDBCC FREEPROCCACHE
DBCC Commands OverviewDBCC Commands Overview Allows gathering of more in-depth information Allows gathering of more in-depth information
that may not be available otherwisethat may not be available otherwise Do not use them unless you have a specific Do not use them unless you have a specific
area you want to investigatearea you want to investigate Do not use them unless you know how to properly Do not use them unless you know how to properly
interpret the outputinterpret the output Do not get distracted with information you do not Do not get distracted with information you do not
needneed
DBCC Command output represents a DBCC Command output represents a snapshot in timesnapshot in time May need to capture a series of output to analyze May need to capture a series of output to analyze
the trendthe trend
DBCC MEMORYSTATUSDBCC MEMORYSTATUS Provides a snapshot of the current memory Provides a snapshot of the current memory
status of SQL Server status of SQL Server Describes the distribution of 8k buffers Describes the distribution of 8k buffers
among various components of SQL Serveramong various components of SQL Server Output contains seven sections:Output contains seven sections:
Shows distribution of committed memoryShows distribution of committed memory Shows size and makeup of buffer poolShows size and makeup of buffer pool Describes makeup of the procedure cacheDescribes makeup of the procedure cache Shows distribution of Dynamic Memory UsageShows distribution of Dynamic Memory Usage Shows distribution of various global memory usageShows distribution of various global memory usage Displays query memory grant informationDisplays query memory grant information Shows concurrent query optimization statusShows concurrent query optimization status
DBCC SQLMGRSTATSDBCC SQLMGRSTATS
Useful in checking the effectiveness of Useful in checking the effectiveness of caching Ad-hoc and prepared SQL statementscaching Ad-hoc and prepared SQL statements
Reports three values:Reports three values: Memory (8k pages) UsedMemory (8k pages) Used
If the amount of memory used is very large it is an If the amount of memory used is very large it is an indication that some connection(s) is preparing a lot of indication that some connection(s) is preparing a lot of SQL statements but not un-preparing itSQL statements but not un-preparing it
Number of SQL EntriesNumber of SQL Entries Indicates the number of SQL statements cachedIndicates the number of SQL statements cached
Number of False Hits Number of False Hits SQL Statements are identified via object_id which is a SQL Statements are identified via object_id which is a
hash value. Therefore, we can have false hits when using hash value. Therefore, we can have false hits when using hash values to look for specific SQL statement. This hash values to look for specific SQL statement. This value should be low.value should be low.
DBCC CACHESTATSDBCC CACHESTATS
Provides more detail on the contents and Provides more detail on the contents and performance of the cache performance of the cache
Output is categorized by Object Type and Output is categorized by Object Type and contains the following columns:contains the following columns: Hit Ratio: represent how frequently pages are found in cacheHit Ratio: represent how frequently pages are found in cache Object Count: show the number of objects cachedObject Count: show the number of objects cached Avg. Cost: an internal measure based on time taken to Avg. Cost: an internal measure based on time taken to
compile and memory used by the compiled plan. This compile and memory used by the compiled plan. This controls whether a plan is cached or not.controls whether a plan is cached or not.
Avg. Pages: number of 8k pages used by a cache object.Avg. Pages: number of 8k pages used by a cache object. LW Object Count, LW Avg Cost, LW Avg Stay, LW Avg Use: LW Object Count, LW Avg Cost, LW Avg Stay, LW Avg Use:
These columns correspond to cache objects thrown out of These columns correspond to cache objects thrown out of the cache by the Lazy Writer. These are supposed to give us the cache by the Lazy Writer. These are supposed to give us an idea of the value of cache objects thrown out.an idea of the value of cache objects thrown out.
DBCC SHOW_STATISTICSDBCC SHOW_STATISTICS
Displays the current distribution statistics for Displays the current distribution statistics for the specified target (Index Name or Collection) the specified target (Index Name or Collection) on the specified tableon the specified table
Contains the Following Columns:Contains the Following Columns: Updated: Date and time the statistics were last updated. Updated: Date and time the statistics were last updated. Rows: Number of rows in the table. Rows: Number of rows in the table. Rows Sampled: Number of rows sampled for statistics Rows Sampled: Number of rows sampled for statistics
information. information. Steps: Number of distribution steps. Steps: Number of distribution steps. Density: Selectivity of the first index column prefix (non-Density: Selectivity of the first index column prefix (non-
frequent). frequent). Average key length: Average length of the first index column Average key length: Average length of the first index column
prefix.prefix.
DBCC INPUTBUFFER (spid)DBCC INPUTBUFFER (spid)
Displays the last statement sent from a User Displays the last statement sent from a User Connection Connection
spid represents the system process ID of the spid represents the system process ID of the useruser
Output contains three columns:Output contains three columns: EventType: RPC, Language or No EventEventType: RPC, Language or No Event Parameters: 0 = text, 1-n = parametersParameters: 0 = text, 1-n = parameters EventInfo:EventInfo:
RPC – contains procedure nameRPC – contains procedure name Language or No Event – First 255 characters of the eventLanguage or No Event – First 255 characters of the event
DBCC DROPCLEANBUFFERSDBCC DROPCLEANBUFFERS
Flush data cache buffers from the Buffer PoolFlush data cache buffers from the Buffer Pool Not all data cache buffers can be flushed. In Not all data cache buffers can be flushed. In
order to flush a buffer, the buffer:order to flush a buffer, the buffer: Can normally be flushed by Lazy WriterCan normally be flushed by Lazy Writer Is not part of procedure cacheIs not part of procedure cache Cannot be KEPT, DIRTY, or PINNEDCannot be KEPT, DIRTY, or PINNED Cannot be part of an outstanding I/O requestCannot be part of an outstanding I/O request Is not already latchedIs not already latched
Once a buffer is flushed, it is returned to the Once a buffer is flushed, it is returned to the free list and is available for reusefree list and is available for reuse
DBCC FREEPROCCACHEDBCC FREEPROCCACHE
Free all procedure cache buffers not Free all procedure cache buffers not currently in use by active connections currently in use by active connections
Forces all statements and procedures to Forces all statements and procedures to be recompiled on next execution be recompiled on next execution
DBCC FLUSHPROCINDB(<DBCC FLUSHPROCINDB(<dbiddbid>) clears >) clears plans from one databaseplans from one database
Do Do notnot use on production systems use on production systems
Perf Toolset: ProfilerPerf Toolset: Profiler
Captures SQL eventsCaptures SQL events E.g. re-compilation, scans, errors & E.g. re-compilation, scans, errors &
warnings, autostats, execution plans, warnings, autostats, execution plans, cache hits & misses, user events cache hits & misses, user events (sp_trace_generateevent)(sp_trace_generateevent)
Detail on specific query performanceDetail on specific query performance Statement level resource consumptionStatement level resource consumption
CPU, Reads, Writes, DurationCPU, Reads, Writes, Duration
Using System Stored Procedures Using System Stored Procedures Create A Trace DefinitionCreate A Trace Definition
sp_trace_createsp_trace_create
Specify black box, shutdown on Error, Rollover optionSpecify black box, shutdown on Error, Rollover option
Add, remove an event or data column to Add, remove an event or data column to trace trace sp_trace_setevent sp_trace_setevent
Applies a filter to a traceApplies a filter to a trace sp_trace_setfilter sp_trace_setfilter
Modify the current state of the specified Modify the current state of the specified tracetrace sp_trace_setstatussp_trace_setstatus Create a user-defined eventCreate a user-defined event sp_trace_generateeventsp_trace_generateevent
Trace Procedures – Trace Procedures – ExampleExampleDECLARE @RC int, @TraceID int, @on DECLARE @RC int, @TraceID int, @on BITBIT
EXEC @rc = sp_trace_create @TraceID output, 0, N'C:\test' EXEC @rc = sp_trace_create @TraceID output, 0, N'C:\test'
SELECT RC = @RC, TraceID = @TraceID, @on = 1SELECT RC = @RC, TraceID = @TraceID, @on = 1
exec sp_trace_setevent @TraceID, 10, 1, @onexec sp_trace_setevent @TraceID, 10, 1, @on
exec sp_trace_setevent @TraceID, 10, 2, @onexec sp_trace_setevent @TraceID, 10, 2, @on
exec sp_trace_setevent @TraceID, 10, 3, @onexec sp_trace_setevent @TraceID, 10, 3, @on
exec sp_trace_setevent @TraceID, 10, 6, @onexec sp_trace_setevent @TraceID, 10, 6, @on
exec sp_trace_setevent @TraceID, 10, 7, @onexec sp_trace_setevent @TraceID, 10, 7, @on
exec sp_trace_setevent @TraceID, 10, 8, @onexec sp_trace_setevent @TraceID, 10, 8, @on
exec sp_trace_setevent @TraceID, 10, 9, @onexec sp_trace_setevent @TraceID, 10, 9, @on
exec sp_trace_setevent @TraceID, 10, 10, @onexec sp_trace_setevent @TraceID, 10, 10, @on
-- Set the Filter-- Set the Filter
EXEC sp_trace_setfilter 1, 10, 0, 6, N'MS%‘EXEC sp_trace_setfilter 1, 10, 0, 6, N'MS%‘
-- Start Trace (status 1 = start)-- Start Trace (status 1 = start)
exec @RC = sp_trace_setstatus @TraceID, 1exec @RC = sp_trace_setstatus @TraceID, 1
Tracing for Performance IssuesTracing for Performance Issues Create a SQL profiler baseline when SQL Create a SQL profiler baseline when SQL
Server is running fineServer is running fine Events to Capture:Events to Capture:
CursorOpen, CursorExecute and CursorOpen, CursorExecute and CursorImplicitConversionCursorImplicitConversion
SP:RecompileSP:Recompile AttentionAttention Audit Login and Logout, SessionsAudit Login and Logout, Sessions Exec Prepared SQL and Prepare SQLExec Prepared SQL and Prepare SQL Errors and WarningsErrors and Warnings SQL:StmtCompleted, RPC:CompletedSQL:StmtCompleted, RPC:Completed
Capture same trace when problem occursCapture same trace when problem occurs Create a baseline for specific applicationCreate a baseline for specific application
Pre Filter by Application Name or Host NamePre Filter by Application Name or Host Name
Analyzing SQL Profiler TracesAnalyzing SQL Profiler Traces Group by Event Class to understand event Group by Event Class to understand event
distribution such as:distribution such as: SP:Recompile, Attention, Exec Prepared SQL and Prepare SP:Recompile, Attention, Exec Prepared SQL and Prepare
SQL, Missing Column StatisticsSQL, Missing Column Statistics
Include only events such as:Include only events such as: SQL:StmtCompleted, RPC:CompletedSQL:StmtCompleted, RPC:Completed
Identify and group long running, CPU and I/O Identify and group long running, CPU and I/O intensive queries using group byintensive queries using group by Duration, CPU, Reads, WritesDuration, CPU, Reads, Writes
Re-order the data columns to make analysis Re-order the data columns to make analysis easy, depending on troubleshooting goals:easy, depending on troubleshooting goals: Event Class, Text, Start Time, Duration, SPID, CPU, Reads, Event Class, Text, Start Time, Duration, SPID, CPU, Reads,
Writes, Application Name…Writes, Application Name…
Resource QueuingResource Queuing
Queues are operations that are waitingQueues are operations that are waiting CPU, IO, NetworkCPU, IO, Network
Symptom vs. problemSymptom vs. problem CPU bound - due to sorting because of CPU bound - due to sorting because of
incorrect indexingincorrect indexing IO bound – due to poor IO subsystem IO bound – due to poor IO subsystem
configuration, scans, memory pressure configuration, scans, memory pressure flushing cacheflushing cache
Resource Queuing: PerfmonResource Queuing: Perfmon
Most important queues and metricsMost important queues and metrics I/O:I/O:
Physical Disk: Avg. Disk Queue LengthPhysical Disk: Avg. Disk Queue Length Physical Disk: Disk sec/Read, Disk sec/WritePhysical Disk: Disk sec/Read, Disk sec/Write
CPU (overall):CPU (overall): System: Processor Queue LengthSystem: Processor Queue Length Processor: % Privileged, Idle, ProcessorProcessor: % Privileged, Idle, Processor
SQL runs in User modeSQL runs in User mode
Note: SQL manages it’s own thread Note: SQL manages it’s own thread management (User Mode Scheduler)management (User Mode Scheduler) DBCC SQLPERF (UMSSTATS)DBCC SQLPERF (UMSSTATS)
Resource Queuing: DiskResource Queuing: Disk
Disk subsystem should be based on I/O Disk subsystem should be based on I/O throughput requirements, not size of DBthroughput requirements, not size of DB E.g. 1TB data / 72GB per drive = 14 drives.E.g. 1TB data / 72GB per drive = 14 drives.
Will 14 drives provide sufficient IO throughput?Will 14 drives provide sufficient IO throughput? May need more smaller drivesMay need more smaller drives Random (OLTP) vs. sequential (Reporting) IO/secRandom (OLTP) vs. sequential (Reporting) IO/sec Cache – tuned for % read or writeCache – tuned for % read or write
Consider all workloadsConsider all workloads OLTP (typically random IOs)OLTP (typically random IOs) Batch (could be random or sequential depending Batch (could be random or sequential depending
on the type of work done)on the type of work done)
Disk subsystem considerationsDisk subsystem considerations
Avoid Avoid Auto growth of tempdbAuto growth of tempdb
Pre-allocate to max workloadPre-allocate to max workload
Unnecessary scans, sortingUnnecessary scans, sorting
Object placement Object placement Attempt to achieve even IO distributionAttempt to achieve even IO distribution Difficult to master with complex queries, Difficult to master with complex queries,
multiple indexes, joinsmultiple indexes, joins
Round-robin, proportional fillRound-robin, proportional fill
I/O bound – spreading IO I/O bound – spreading IO across multiple spindlesacross multiple spindles
ExampleExample
1) IO Baseline ::fn_virtualfilestats1) IO Baseline ::fn_virtualfilestats2) Add database file2) Add database file3) BCP or Create Index3) BCP or Create Index4) Repeat IO baseline4) Repeat IO baseline
Example Step 1: baseline IOExample Step 1: baseline IO
-- step1: baseline IO-- step1: baseline IO
select step='step 1 - baseline I/O for file 1 .',*select step='step 1 - baseline I/O for file 1 .',*
into #FileStats1into #FileStats1
from from ::fn_virtualfilestats::fn_virtualfilestats(@dbid,1)(@dbid,1)
-- File 3 doesn’t exist yet-- File 3 doesn’t exist yet
select step='step 1 - baseline I/O for file 3 .',*select step='step 1 - baseline I/O for file 3 .',*
into #FileStats3into #FileStats3
from from ::fn_virtualfilestats::fn_virtualfilestats(@dbid,3)(@dbid,3)
DBCC dropcleanbuffersDBCC dropcleanbuffers -- flush cache -- flush cache
Example Step 2: Add DB fileExample Step 2: Add DB file
-- Step 2: -- Step 2: Add database file 3Add database file 3if not exists (select * from sysfiles where name = if not exists (select * from sysfiles where name =
‘mydb_Data2')‘mydb_Data2')ALTER DATABASE mydbALTER DATABASE mydbADD FILEADD FILE --- [to filegroup default]--- [to filegroup default]( NAME = mydb_Data2,( NAME = mydb_Data2, FILENAME = 'c:\Program Files\Microsoft SQL FILENAME = 'c:\Program Files\Microsoft SQL
Server\MSSQL\Data\mydb_dat2.ndf',Server\MSSQL\Data\mydb_dat2.ndf', SIZE = 10MB,SIZE = 10MB, MAXSIZE = UNLIMITED,MAXSIZE = UNLIMITED, FILEGROWTH = 10% )FILEGROWTH = 10% )
Example step 3: Re-create indexExample step 3: Re-create index
-- -- Step 3: Re-create Index (or BCP)Step 3: Re-create Index (or BCP)
create clustered index X1 on mytab(a) create clustered index X1 on mytab(a)
ON defaultON default
-- -- automatically rebuilds Nonclustered automatically rebuilds Nonclustered indexes, unless drop_existing specifiedindexes, unless drop_existing specified
Example Step 4: New IO baselineExample Step 4: New IO baseline
-- -- Step 4: New IO baselineStep 4: New IO baselinedbcc dropcleanbuffersdbcc dropcleanbuffers -- clears out data cache-- clears out data cache
select count(*) from mytabselect count(*) from mytab
insert into #FileStats1insert into #FileStats1select 'step 4 - table scan after clustered index rebuild',* select 'step 4 - table scan after clustered index rebuild',* from ::fn_virtualfilestats(@dbid,1)from ::fn_virtualfilestats(@dbid,1)
insert into #FileStats3insert into #FileStats3select 'step 4 - table scan after clustered index rebuild',* select 'step 4 - table scan after clustered index rebuild',* from ::fn_virtualfilestats(@dbid,3)from ::fn_virtualfilestats(@dbid,3)
Resource Queuing: CPUResource Queuing: CPU
Win NT ThreadNetwork Handler
UMS UMS WorkWork
QueueQueue
Win NT Win NT Thread 0Thread 0
UMSUMSWorkWork
QueueQueue
UMSUMSWorkWork
QueueQueue
UMSUMSWorkWork
QueueQueue
Network Handler Notified Network Handler Notified When I/O CompletesWhen I/O Completes
UMS UMS Schedules Schedules
FibersFibers
NetworkNetwork
Fibers Write Fibers Write Directly to Directly to
ClientsClients
NetworkNetwork
NT Queues Reads NT Queues Reads Issued by Fibers to Issued by Fibers to
I/O Completion I/O Completion PortPort
CPU nCPU nCPU 1CPU 1 CPU 2CPU 2CPU 0CPU 0
Win NT Win NT Thread 1Thread 1
Win NT Win NT Thread 2Thread 2
Win NT Win NT Thread nThread n
FibersFibersFibersFibers
NT I/O Completion
Port
Resource Queuing: CPUResource Queuing: CPU Common uses of CPU resourcesCommon uses of CPU resources
Context switchingContext switching RecompilationRecompilation SortingSorting Hashing (e.g. Joins)Hashing (e.g. Joins)
Lightweight pooling (fiber-mode)Lightweight pooling (fiber-mode) Can provide 15% boostCan provide 15% boost High CPU (+- >85%)High CPU (+- >85%) High context switching > +-20KHigh context switching > +-20K CPU affinity (limit thread jumping)CPU affinity (limit thread jumping)
SQL thread managementSQL thread managementDbcc sqlperf(umsstats)Dbcc sqlperf(umsstats)
ExampleExample
Scheduler ID .0Scheduler ID .0 num users 13.0num users 13.0 num runnable .0num runnable .0 num workers 12.0num workers 12.0 idle workers 5.0idle workers 5.0 work queued .0work queued .0 cntxt switchescntxt switches 25488.0 25488.0 <-sql<-sql cntxt switches(idle)cntxt switches(idle) 18344.0 18344.0 <-kernel<-kernelScheduler Switches .0Scheduler Switches .0Total Work 14296.0Total Work 14296.0
ItemItem DefinitionDefinitionnum usersnum users Number of SQL threads on the Number of SQL threads on the
schedulerscheduler
num runnablenum runnable Number of SQL threads that are Number of SQL threads that are “runnable” (Scheduler queue length)“runnable” (Scheduler queue length)
num workersnum workers Number of actual workers to process Number of actual workers to process threads (thread pool)threads (thread pool)
idle workersidle workers Number of idle workersNumber of idle workers
cntxt switchescntxt switches Context switches between runnable Context switches between runnable threadsthreads
cntxt switches(idle)cntxt switches(idle) Context switches to the “idle” threadContext switches to the “idle” thread
SQL thread managementSQL thread managementDBCC sqlperf(umsstats)DBCC sqlperf(umsstats)
Example: Tracking UMSSTATSExample: Tracking UMSSTATScreate proc track_umsstats (@num_times int=5,@delaymin int=1)create proc track_umsstats (@num_times int=5,@delaymin int=1)asas-- T. Davidson-- T. Davidson-- @num_times is number of times to capture umsstats, default is 5 times-- @num_times is number of times to capture umsstats, default is 5 times-- -- default delay interval is 1 minutedefault delay interval is 1 minutecreate table #umsstats (Statistic varchar(80),Value numeric(10,1) ,create table #umsstats (Statistic varchar(80),Value numeric(10,1) ,
now datetime default getdate())now datetime default getdate())dbcc sqlperf (umsstats,clear) dbcc sqlperf (umsstats,clear) -- clear out umsstats-- clear out umsstats
declare @i int,@delay varchar(8)declare @i int,@delay varchar(8)select @i = 1select @i = 1select @delay='00:' + right('0'+convert(varchar(2),@delaymin),2) + ':00'select @delay='00:' + right('0'+convert(varchar(2),@delaymin),2) + ':00'while (@i <= @num_times)while (@i <= @num_times)beginbegin insert into #umsstats (Statistic,Value) exec ('dbcc sqlperf(umsstats)')insert into #umsstats (Statistic,Value) exec ('dbcc sqlperf(umsstats)')
select @i = @i + 1select @i = @i + 1waitfor delay @delaywaitfor delay @delay
endendselect * from #umsstats order by Statistic,nowselect * from #umsstats order by Statistic,now
Resource Queuing: NetworkResource Queuing: Network
Exceeding capabilities of NIC’sExceeding capabilities of NIC’s Check perfmon SQLServer:SQL Check perfmon SQLServer:SQL
Statistics:Batch Requests/SecStatistics:Batch Requests/Sec > 3000/sec about max on 100Mb hardware> 3000/sec about max on 100Mb hardware
Exceeding network bandwidthExceeding network bandwidth Check perfmon Check perfmon [[Network Network
Interface:Bytes Total/SecInterface:Bytes Total/Sec] / [] / [Network Network Interface:Current BandwidthInterface:Current Bandwidth] ] > .6 is > .6 is pushing it.pushing it.
Concurrency Issues:Concurrency Issues:Latching and LocksLatching and Locks LocksLocks
Ensure transactional integrity and database Ensure transactional integrity and database consistency consistency
LatchingLatching Light weight short term synchronization objectLight weight short term synchronization object Short-term in duration (Short-term in duration (1/41/4 of overhead of lock) of overhead of lock)
LockingLocking sp_locksp_lock DBCC SQLPERF(waitstats)DBCC SQLPERF(waitstats) syslockinfosyslockinfo sysprocesses (wait states, wait types)sysprocesses (wait states, wait types)
Locking and IndexesLocking and IndexesTables With Clustered Indexes Tables With Clustered Indexes
Always Take KEY LocksAlways Take KEY Locks Never RID LocksNever RID Locks
Sp_indexoption Can Control Sp_indexoption Can Control Locking in IndexesLocking in Indexes
Occasionally Useful for Deadlock Occasionally Useful for Deadlock AvoidanceAvoidance
Concurrency Issues: Concurrency Issues: TransactionsTransactions
Exclusive locks held until commitExclusive locks held until commit Shared locks – depends on transaction Shared locks – depends on transaction
Isolation levelIsolation level Tran Isolation level affects the behavior Tran Isolation level affects the behavior
(duration) of shared locks(duration) of shared locks Read committedRead committed – default, shared released – default, shared released Repeatable ReadRepeatable Read – locks rows until commit – locks rows until commit SerializableSerializable – locks ranges of rows until commit – locks ranges of rows until commit
(no phantoms)(no phantoms)
JoinsJoins
Finding Long TransactionsFinding Long Transactions Use DBCC OPENTRANUse DBCC OPENTRAN Or: query sysprocesses & syslockinfo:Or: query sysprocesses & syslockinfo:
SELECTspid,cmd,status,loginame, open_tran,datediff (s, last_batch, getdate ()) As [WaitTime (s)]
FROM master..sysprocesses pWHERE
open_tran > 0 and spid > 50 anddatediff (s, last_batch, getdate ()) > 30 andexists (select * from master..syslockinfo l
where req_spid = p.spid and rsc_type <> 2)
Querying for waitsQuerying for waits Display All User Threads Waiting on a Display All User Threads Waiting on a
Resource > 5 Seconds:Resource > 5 Seconds:
/* Use this query to look for user threads that /* Use this query to look for user threads that have been waiting on any resource for > 5 have been waiting on any resource for > 5 seconds. It's helpful to locate blocking locks, seconds. It's helpful to locate blocking locks, I/O, etc.I/O, etc.
*/*/SELECT spid, waittime, lastwaittype, waitresourceSELECT spid, waittime, lastwaittype, waitresource
FROM master..sysprocessesFROM master..sysprocesses
WHERE waittime > 5000 -- Report any waits > 5 secsWHERE waittime > 5000 -- Report any waits > 5 secs
AND spid > 50 -- In SQL 2000 all user spids are > 50 AND spid > 50 -- In SQL 2000 all user spids are > 50 -- this should be ~12 in SQL 7.0-- this should be ~12 in SQL 7.0
Techniques to avoid blockingTechniques to avoid blocking Keep Transactions Short and in One BatchKeep Transactions Short and in One Batch No User Interaction During TransactionsNo User Interaction During Transactions Rollback when Canceling; Rollback on Any Error or Rollback when Canceling; Rollback on Any Error or
Timeout Timeout Beware of Implicit TransactionsBeware of Implicit Transactions Process Results QuicklyProcess Results Quickly Reduce Isolation Level to Lowest PossibleReduce Isolation Level to Lowest Possible Stress Test at Maximum Projected User Load Before Stress Test at Maximum Projected User Load Before
DeploymentDeployment Proper Indexing – Index Tuning Wizard or Index Proper Indexing – Index Tuning Wizard or Index
AnalysisAnalysis Other Possibilities to Consider: Other Possibilities to Consider:
Locking hint, Index hint, Join hintLocking hint, Index hint, Join hint
Techniques to minimize deadlocksTechniques to minimize deadlocks
Prevent BlockingPrevent Blocking In-order Resource AccessIn-order Resource AccessBound ConnectionsBound ConnectionsAdd and/or Remove Indexes to Provide Add and/or Remove Indexes to Provide
or Remove Alternate Access Path to the or Remove Alternate Access Path to the Desired ResourceDesired Resource
SET DEADLOCK_PRIORITY LOWSET DEADLOCK_PRIORITY LOWDeadlock Retry Logic in ApplicationDeadlock Retry Logic in Application
Concurrency Issues: Memory Concurrency Issues: Memory and Locksand Locks Memory-intensive queries are queued Memory-intensive queries are queued
if not enough memory availableif not enough memory available SortingSorting Hashing (e.g. joins)Hashing (e.g. joins)
Locks could also be held during the Locks could also be held during the wait for memory, and throughout wait for memory, and throughout sorting and hashingsorting and hashing See locks and transactionsSee locks and transactions
Concurrency issues: Cache Concurrency issues: Cache and I/Oand I/O Better I/O and cache performance via Better I/O and cache performance via
Scatter Gather I/O Scatter Gather I/O Single I/O from different areas of memorySingle I/O from different areas of memory
Asynchronous I/OAsynchronous I/O Use worker threads for I/O in anticipationUse worker threads for I/O in anticipation
Buffer cacheBuffer cache Free buffer listFree buffer list
Clock-out algorithm – reference page counter (stale)Clock-out algorithm – reference page counter (stale) Lazy-writer – throw out page Lazy-writer – throw out page (SQL Counter: Lazy writer p/s)(SQL Counter: Lazy writer p/s)
Buffer pageBuffer page Header reference counter and dirty page indicatorHeader reference counter and dirty page indicator
Concurrency Issues: Cache Concurrency Issues: Cache and I/Oand I/O Freeing and writing buffer pagesFreeing and writing buffer pages
Lazywriter – runs regular intervalsLazywriter – runs regular intervals Maintains size of free buffer listMaintains size of free buffer list
Scans buffer listScans buffer list writes dirty pages (SQL Buffer Mgr:lazy writes/sec)writes dirty pages (SQL Buffer Mgr:lazy writes/sec) Worker threads (UMSSTATS – worker threads)Worker threads (UMSSTATS – worker threads)
Performs asynch I/OPerforms asynch I/O scans buffer listscans buffer list Writes dirty pagesWrites dirty pages
CheckpointCheckpoint Frequency based on recovery interval and number of Frequency based on recovery interval and number of
records in transaction logrecords in transaction log Writes dirty log and data pages (SQL Buffer Mgr: Writes dirty log and data pages (SQL Buffer Mgr:
Checkpoint pages/sec)Checkpoint pages/sec)
Concurrency Issues: Cache Concurrency Issues: Cache and I/Oand I/O
Memory intensive operations may flush Memory intensive operations may flush other pages from SQL Cacheother pages from SQL Cache Successive reads of flushed data are Successive reads of flushed data are
physicalphysical Possible lower hit ratesPossible lower hit rates
Very generally - >=90% is goodVery generally - >=90% is good
Clock algorithm determines pages to be Clock algorithm determines pages to be flushedflushed Decrements reference counterDecrements reference counter
Concurrency Issues: Locks Concurrency Issues: Locks and I/Oand I/O I/O intensive queries can cause I/O intensive queries can cause
transactional locks to be held longer transactional locks to be held longer Some wait states (types) for I/O: Some wait states (types) for I/O:
Async IO completionAsync IO completion Network IO completionNetwork IO completion IO completionIO completion Waiting on writelogWaiting on writelog
Q271509 - INF: How to Monitor SQL Q271509 - INF: How to Monitor SQL Server 2000 BlockingServer 2000 Blocking
Tracking wait statesTracking wait states
ExampleExample
Waittype, waittime, lastwaittypeWaittype, waittime, lastwaittypeDbcc sqlperf(waitstats)Dbcc sqlperf(waitstats)
Example: Tracking WAITSTATSExample: Tracking WAITSTATScreate proc track_waitstats (@num_times int=5,@delaymin int=1)create proc track_waitstats (@num_times int=5,@delaymin int=1)asas-- T. Davidson-- T. Davidsoncreate table #waitstats ([Wait Type] varchar(80), create table #waitstats ([Wait Type] varchar(80),
Requests numeric(10,1), [Wait Time] numeric (10,1),Requests numeric(10,1), [Wait Time] numeric (10,1),[Signal Wait Time] numeric(10,1), now datetime default [Signal Wait Time] numeric(10,1), now datetime default
getdate())getdate())dbcc sqlperf (waitstats,clear) -- clear out waitstatsdbcc sqlperf (waitstats,clear) -- clear out waitstatsdeclare @i int,@delay varchar(8)declare @i int,@delay varchar(8)select @i = 1select @i = 1select @delay='00:' + right('0'+convert(varchar(2),@delaymin),2) + ':00'select @delay='00:' + right('0'+convert(varchar(2),@delaymin),2) + ':00'while (@i <= @num_times)while (@i <= @num_times)beginbegin
insert into #waitstats ([Wait Type], Requests, [Wait Time],[Signal Wait insert into #waitstats ([Wait Type], Requests, [Wait Time],[Signal Wait Time])Time])exec ('dbcc sqlperf(waitstats)')exec ('dbcc sqlperf(waitstats)')select @i = @i + 1select @i = @i + 1waitfor delay @delaywaitfor delay @delay
endendselect * from #waitstats order by [Wait Type],nowselect * from #waitstats order by [Wait Type],now
Query Processing ConceptsQuery Processing Concepts
Compilation vs Optimization Compilation vs Optimization Only DML can be Optimized!Only DML can be Optimized! TSQL Statements are CompiledTSQL Statements are Compiled
Delay Between Compilation and Delay Between Compilation and ExecutionExecution
Plans Needing More Resources May Plans Needing More Resources May Need to WaitNeed to Wait
Query Processing OverviewQuery Processing Overview
Plan in cache?Plan in cache?
Parse/NormalizeParse/Normalize
Compile TSQLCompile TSQL
Optimize SQL statementsOptimize SQL statements
Put plan in cachePut plan in cache
NoNo Retrieve planRetrieve planfrom cachefrom cache
YesYes
Plan need to Plan need to be be
recompiled?recompiled?YesYes
Run plan to completionRun plan to completion
NoNo
Wait for Memory Wait for Memory Grant Scheduler to Grant Scheduler to
ok requestok request
Procedure CacheProcedure Cache
Not a Separate Area of MemoryNot a Separate Area of MemoryCompilation is Often ExpensiveCompilation is Often Expensive
Reusing High-Cost Plans is GoodReusing High-Cost Plans is Good
Some Queries are Cheap to CompileSome Queries are Cheap to Compile May not be worth keeping plan aroundMay not be worth keeping plan around
Procedure Cache Balances Cost to Procedure Cache Balances Cost to Recompile with Frequency of UseRecompile with Frequency of Use
Procedure Cache ManagementProcedure Cache Management
Ad-hoc Ad-hoc & cheap & cheap
to to compilecompile
Allocate memory Allocate memory from buffer cachefrom buffer cache
Don’t CacheDon’t CacheYesYes
Set initial cost to Set initial cost to zerozero
Type of Type of plan?plan?
Ad-hocAd-hoc
Set initial cost to Set initial cost to creation costcreation cost
Put plan in cachePut plan in cache
Retrieve plan Retrieve plan from cachefrom cache
Type Type of of
plan?plan?
Increment Increment cost by 1cost by 1
Set cost to Set cost to creation costcreation cost
Return planReturn plan
Ad-hocAd-hoc
Retrieve PlanRetrieve Plan
Put Plan in CachePut Plan in Cache
Concurrency Issues: Cached Concurrency Issues: Cached ObjectsObjects master..syscacheobjectsmaster..syscacheobjects
Procedure or batch nameProcedure or batch name Set options for plansSet options for plans Ref counts, Use countsRef counts, Use counts Compiled planCompiled plan
Single copy (serial and parallel) – unlike SQL 6.5Single copy (serial and parallel) – unlike SQL 6.5 Re-entrant and re-usableRe-entrant and re-usable
Executable planExecutable plan Data structure for user context, not re-entrantData structure for user context, not re-entrant
Plan re-use of…Plan re-use of… Procs, Triggers, ViewsProcs, Triggers, Views Defaults, Check contraints, rulesDefaults, Check contraints, rules adhoc SQL, sp_executesqladhoc SQL, sp_executesql
Concurrency Issues: Re-Concurrency Issues: Re-compilationcompilation ProfilerProfiler
Lists recomp events and statementsLists recomp events and statements Data column for reason: EventSubClassData column for reason: EventSubClass
Locks on system tables Locks on system tables Re-compiling stored procedure plans serialize Re-compiling stored procedure plans serialize
other users during re-comp.other users during re-comp.
Re-compilation based onRe-compilation based on Rows changed threshold - sysindexes.rowmodctrRows changed threshold - sysindexes.rowmodctr DDL & schema changesDDL & schema changes SomeSome Set Set options options
Recompilation Recompilation
Can Be ExpensiveCan Be Expensive Optimization is really the expensive part!Optimization is really the expensive part!
Plans Can Be Saved for AdHoc QueriesPlans Can Be Saved for AdHoc QueriesQueries Can be AutoParameterizedQueries Can be AutoParameterizedMinimizing Recompilations of Minimizing Recompilations of
ProceduresProcedures
EventSubClass: Reason for EventSubClass: Reason for recompilation recompilation ReasonReason DescriptionDescription
11 Schema, bindings or permissions Schema, bindings or permissions changed between compile or execute.changed between compile or execute.
22 Statistics changed. Statistics changed. 33 Object not found at compile time, Object not found at compile time,
deferred check to run-time. deferred check to run-time. 44 Set option changed in batch.Set option changed in batch.55 Temp table schema, binding or Temp table schema, binding or
permission changed. permission changed. 66 Remote rowset schema, binding or Remote rowset schema, binding or
permission changed. permission changed.
Viewing StatisticsViewing Statistics
DBCC SHOW_STATISTICSDBCC SHOW_STATISTICS DBCC SHOW_STATISTICS DBCC SHOW_STATISTICS ( ( table table , , target target ))
Up to 200 Steps are SampledUp to 200 Steps are SampledALL DENSITY Is Shown for All Left-ALL DENSITY Is Shown for All Left-
based Key Combinations based Key Combinations
Tracking re-compilationTracking re-compilation
demodemo
Profiler: EventSubClassProfiler: EventSubClass
ConclusionsConclusions
SQL Server 2000 is a high performance SQL Server 2000 is a high performance enterprise class database.enterprise class database.
Performance Tuning is essential to Performance Tuning is essential to Reach peak system capabilities.Reach peak system capabilities. Exploit all resourcesExploit all resources
CPUCPU I/OI/O NetworkNetwork Data (concurrency vs. consistency)Data (concurrency vs. consistency)
ReferencesReferences
““Inside SQL Server 2000” by Kalen Inside SQL Server 2000” by Kalen DelaneyDelaney
““SQL Server 2000 Performance Tuning” SQL Server 2000 Performance Tuning” by Whalen, Garcia, DeLuca, Thompsonby Whalen, Garcia, DeLuca, Thompson
SQL Server 2000 Performance Tuning SQL Server 2000 Performance Tuning Technical Reference (MS Press)Technical Reference (MS Press)
““SQL Server 2000 Recompilation” SQL Server 2000 Recompilation” whitepaper by T. Davidsonwhitepaper by T. Davidson
Resources from Microsoft PressResources from Microsoft Press
For more information please visit the TechEd Bookshop.For more information please visit the TechEd Bookshop.www.microsoft.com/mspresswww.microsoft.com/mspress
SQL SERVER 2000
Don’t forget to complete the Don’t forget to complete the on-line Session Feedback form on-line Session Feedback form on the Attendee Web siteon the Attendee Web site
https://web.mseventseurope.com/teched/https://web.mseventseurope.com/teched/