Presentation is loading. Please wait.

Presentation is loading. Please wait.

DAT 342 Advanced SQL Server Performance and Tuning Bren Newman Program Manager SQL Server Development Microsoft Corporation.

Similar presentations


Presentation on theme: "DAT 342 Advanced SQL Server Performance and Tuning Bren Newman Program Manager SQL Server Development Microsoft Corporation."— Presentation transcript:

1 DAT 342 Advanced SQL Server Performance and Tuning Bren Newman Program Manager SQL Server Development Microsoft Corporation

2 Agenda Performance Overview Performance Overview Performance Toolset Performance Toolset Resource Queuing Resource Queuing Concurrency issues Concurrency issues Q & A Q & A

3 Performance Overview When under load, every system has a bottleneck that limits performance When under load, every system has a bottleneck that limits performance May depend on workload, time of day May depend on workload, time of day E.g. OLTP and Batch may have different profiles and bottlenecks E.g. OLTP and Batch may have different profiles and bottlenecks Fix one bottleneck, and another surfaces under load Fix one bottleneck, and another surfaces under load CPU, I/O, Locking (concurrency), Network CPU, I/O, Locking (concurrency), Network In a perfect world, CPU is the last bottleneck In a perfect world, CPU is the last bottleneck

4 Performance Toolset Perfmon Perfmon Virtual tables (and Stored Procs) Virtual tables (and Stored Procs) FileStats FileStats DBCC DBCC Profiler Profiler

5 Perf Toolset: PERFMON System objects such as System objects such as System, processor, IO, cache, memory System, processor, IO, cache, memory SQL Server objects SQL Server objects Overall SQL Server performance Overall SQL Server performance Examples: data access, buffer, locks, cache, memory, statistics, etc. Examples: data access, buffer, locks, cache, memory, statistics, etc.

6 Perf Toolset: Virtual tables Syslockinfo Syslockinfo Dbid, object, index, type, status Dbid, object, index, type, status Sysprocesses Sysprocesses Waittype, waittime, lastwaittype Waittype, waittime, lastwaittype Sysperfinfo Sysperfinfo SQL counters exposed to PERFMON SQL counters exposed to PERFMON

7 Perf Toolset: FileStats ::fn_virtualfilestats ::fn_virtualfilestats Physical IO per file Physical IO per file SQL Server IO only SQL Server IO only Compare SQL IO to PERFMON Compare SQL IO to PERFMON PhysicalDisk:Disk Reads/sec PhysicalDisk:Disk Writes/sec PhysicalDisk:Disk Reads/sec PhysicalDisk:Disk Writes/sec PhysicalDisk:Disk Read Bytes/sec PhysicalDisk:Disk Read Bytes/sec PhysicalDisk:Disk Write Bytes/sec PhysicalDisk:Disk Write Bytes/sec select DbId,FileId,TimeStamp,NumberReads,NumberWrites,BytesRead,BytesWritten,IoStallMS from ::fn_VirtualFileStats (DbId,FileId) -- IoStallMS <= 20ms for log

8 Perf Toolset: DBCC DBCC SQLPERF DBCC SQLPERF Logspace Logspace Umsstats – sql thread management Umsstats – sql thread management Waitstats – resources, wait types Waitstats – resources, wait types

9 Other DBCC commands Relevant DBCC Commands Relevant DBCC Commands DBCC MEMORYSTATUS DBCC MEMORYSTATUS DBCC SQLMGRSTATS DBCC SQLMGRSTATS DBCC CACHESTATS DBCC CACHESTATS DBCC SHOW_STATISTICS DBCC SHOW_STATISTICS DBCC INPUTBUFFER DBCC INPUTBUFFER DBCC DROPCLEANBUFFERS DBCC DROPCLEANBUFFERS DBCC FREEPROCCACHE DBCC FREEPROCCACHE

10 DBCC Commands Overview Allows gathering of more in-depth information that may not be available otherwise Allows gathering of more in-depth information that may not be available otherwise Do not use them unless you have a specific area you want to investigate Do not use them unless you have a specific area you want to investigate Do not use them unless you know how to properly interpret the output Do not use them unless you know how to properly interpret the output Do not get distracted with information you do not need Do not get distracted with information you do not need DBCC Command output represents a snapshot in time DBCC Command output represents a snapshot in time May need to capture a series of output to analyze the trend May need to capture a series of output to analyze the trend

11 DBCC MEMORYSTATUS Provides a snapshot of the current memory status of SQL Server Provides a snapshot of the current memory status of SQL Server Describes the distribution of 8k buffers among various components of SQL Server Describes the distribution of 8k buffers among various components of SQL Server Output contains seven sections: Output contains seven sections: Shows distribution of committed memory Shows distribution of committed memory Shows size and makeup of buffer pool Shows size and makeup of buffer pool Describes makeup of the procedure cache Describes makeup of the procedure cache Shows distribution of Dynamic Memory Usage Shows distribution of Dynamic Memory Usage Shows distribution of various global memory usage Shows distribution of various global memory usage Displays query memory grant information Displays query memory grant information Shows concurrent query optimization status Shows concurrent query optimization status

12 DBCC SQLMGRSTATS Useful in checking the effectiveness of caching Ad-hoc and prepared SQL statements Useful in checking the effectiveness of caching Ad-hoc and prepared SQL statements Reports three values: Reports three values: Memory (8k pages) Used Memory (8k pages) Used If the amount of memory used is very large it is an indication that some connection(s) is preparing a lot of SQL statements but not un-preparing it If the amount of memory used is very large it is an indication that some connection(s) is preparing a lot of SQL statements but not un-preparing it Number of SQL Entries Number of SQL Entries Indicates the number of SQL statements cached Indicates the number of SQL statements cached Number of False Hits Number of False Hits SQL Statements are identified via object_id which is a hash value. Therefore, we can have false hits when using hash values to look for specific SQL statement. This value should be low. SQL Statements are identified via object_id which is a hash value. Therefore, we can have false hits when using hash values to look for specific SQL statement. This value should be low.

13 DBCC CACHESTATS Provides more detail on the contents and performance of the cache Provides more detail on the contents and performance of the cache Output is categorized by Object Type and contains the following columns: Output is categorized by Object Type and contains the following columns: Hit Ratio: represent how frequently pages are found in cache Hit Ratio: represent how frequently pages are found in cache Object Count: show the number of objects cached Object Count: show the number of objects cached Avg. Cost: an internal measure based on time taken to compile and memory used by the compiled plan. This controls whether a plan is cached or not. Avg. Cost: an internal measure based on time taken to compile and memory used by the compiled plan. This 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: These columns correspond to cache objects thrown out of the cache by the Lazy Writer. These are supposed to give us an idea of the value of cache objects thrown out. LW Object Count, LW Avg Cost, LW Avg Stay, LW Avg Use: These columns correspond to cache objects thrown out of the cache by the Lazy Writer. These are supposed to give us an idea of the value of cache objects thrown out.

14 DBCC SHOW_STATISTICS Displays the current distribution statistics for the specified target (Index Name or Collection) on the specified table Displays the current distribution statistics for the specified target (Index Name or Collection) on 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 information. Rows Sampled: Number of rows sampled for statistics information. Steps: Number of distribution steps. Steps: Number of distribution steps. Density: Selectivity of the first index column prefix (non- frequent). Density: Selectivity of the first index column prefix (non- frequent). Average key length: Average length of the first index column prefix. Average key length: Average length of the first index column prefix.

15 DBCC INPUTBUFFER (spid) Displays the last statement sent from a User Connection Displays the last statement sent from a User Connection spid represents the system process ID of the user spid represents the system process ID of the user Output contains three columns: Output contains three columns: EventType: RPC, Language or No Event EventType: RPC, Language or No Event Parameters: 0 = text, 1-n = parameters Parameters: 0 = text, 1-n = parameters EventInfo: EventInfo: RPC – contains procedure name RPC – contains procedure name Language or No Event – First 255 characters of the event Language or No Event – First 255 characters of the event

16 DBCC DROPCLEANBUFFERS Flush data cache buffers from the Buffer Pool Flush data cache buffers from the Buffer Pool Not all data cache buffers can be flushed. In order to flush a buffer, the buffer: Not all data cache buffers can be flushed. In order to flush a buffer, the buffer: Can normally be flushed by Lazy Writer Can normally be flushed by Lazy Writer Is not part of procedure cache Is not part of procedure cache Cannot be KEPT, DIRTY, or PINNED Cannot be KEPT, DIRTY, or PINNED Cannot be part of an outstanding I/O request Cannot be part of an outstanding I/O request Is not already latched Is not already latched Once a buffer is flushed, it is returned to the free list and is available for reuse Once a buffer is flushed, it is returned to the free list and is available for reuse

17 DBCC FREEPROCCACHE Free all procedure cache buffers not currently in use by active connections Free all procedure cache buffers not currently in use by active connections Forces all statements and procedures to be recompiled on next execution Forces all statements and procedures to be recompiled on next execution DBCC FLUSHPROCINDB( ) clears plans from one database DBCC FLUSHPROCINDB( ) clears plans from one database Do not use on production systems Do not use on production systems

18 Perf Toolset: Profiler Captures SQL events Captures SQL events E.g. re-compilation, scans, errors & warnings, autostats, execution plans, cache hits & misses, user events (sp_trace_generateevent) E.g. re-compilation, scans, errors & warnings, autostats, execution plans, cache hits & misses, user events (sp_trace_generateevent) Detail on specific query performance Detail on specific query performance Statement level resource consumption Statement level resource consumption CPU, Reads, Writes, Duration CPU, Reads, Writes, Duration

19 Using System Stored Procedures Create A Trace Definition Create A Trace Definition sp_trace_create sp_trace_create Specify black box, shutdown on Error, Rollover option Specify black box, shutdown on Error, Rollover option Add, remove an event or data column to trace Add, remove an event or data column to trace sp_trace_setevent sp_trace_setevent Applies a filter to a trace Applies a filter to a trace sp_trace_setfilter sp_trace_setfilter Modify the current state of the specified trace Modify the current state of the specified trace sp_trace_setstatus sp_trace_setstatus Create a user-defined event Create a user-defined event sp_trace_generateevent sp_trace_generateevent

20 Trace Procedures – Example BIT = output, 0, N'C:\test' SELECT RC = 1 exec 10, exec 10, exec 10, exec 10, exec 10, exec 10, exec 10, exec 10, -- Set the Filter EXEC sp_trace_setfilter 1, 10, 0, 6, N'MS% -- Start Trace (status 1 = start) = 1

21 Tracing for Performance Issues Create a SQL profiler baseline when SQL Server is running fine Create a SQL profiler baseline when SQL Server is running fine Events to Capture: Events to Capture: CursorOpen, CursorExecute and CursorImplicitConversion CursorOpen, CursorExecute and CursorImplicitConversion SP:Recompile SP:Recompile Attention Attention Audit Login and Logout, Sessions Audit Login and Logout, Sessions Exec Prepared SQL and Prepare SQL Exec Prepared SQL and Prepare SQL Errors and Warnings Errors and Warnings SQL:StmtCompleted, RPC:Completed SQL:StmtCompleted, RPC:Completed Capture same trace when problem occurs Capture same trace when problem occurs Create a baseline for specific application Create a baseline for specific application Pre Filter by Application Name or Host Name Pre Filter by Application Name or Host Name

22 Analyzing SQL Profiler Traces Group by Event Class to understand event distribution such as: Group by Event Class to understand event distribution such as: SP:Recompile, Attention, Exec Prepared SQL and Prepare SQL, Missing Column Statistics SP:Recompile, Attention, Exec Prepared SQL and Prepare SQL, Missing Column Statistics Include only events such as: Include only events such as: SQL:StmtCompleted, RPC:Completed SQL:StmtCompleted, RPC:Completed Identify and group long running, CPU and I/O intensive queries using group by Identify and group long running, CPU and I/O intensive queries using group by Duration, CPU, Reads, Writes Duration, CPU, Reads, Writes Re-order the data columns to make analysis easy, depending on troubleshooting goals: Re-order the data columns to make analysis easy, depending on troubleshooting goals: Event Class, Text, Start Time, Duration, SPID, CPU, Reads, Writes, Application Name… Event Class, Text, Start Time, Duration, SPID, CPU, Reads, Writes, Application Name…

23 Resource Queuing Queues are operations that are waiting Queues are operations that are waiting CPU, IO, Network CPU, IO, Network Symptom vs. problem Symptom vs. problem CPU bound - due to sorting because of incorrect indexing CPU bound - due to sorting because of incorrect indexing IO bound – due to poor IO subsystem configuration, scans, memory pressure flushing cache IO bound – due to poor IO subsystem configuration, scans, memory pressure flushing cache

24 Resource Queuing: Perfmon Most important queues and metrics Most important queues and metrics I/O: I/O: Physical Disk: Avg. Disk Queue Length Physical Disk: Avg. Disk Queue Length Physical Disk: Disk sec/Read, Disk sec/Write Physical Disk: Disk sec/Read, Disk sec/Write CPU (overall): CPU (overall): System: Processor Queue Length System: Processor Queue Length Processor: % Privileged, Idle, Processor Processor: % Privileged, Idle, Processor SQL runs in User mode SQL runs in User mode Note: SQL manages its own thread management (User Mode Scheduler) Note: SQL manages its own thread management (User Mode Scheduler) DBCC SQLPERF (UMSSTATS) DBCC SQLPERF (UMSSTATS)

25 Resource Queuing: Disk Disk subsystem should be based on I/O throughput requirements, not size of DB Disk subsystem should be based on I/O throughput 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 drives May need more smaller drives Random (OLTP) vs. sequential (Reporting) IO/sec Random (OLTP) vs. sequential (Reporting) IO/sec Cache – tuned for % read or write Cache – tuned for % read or write Consider all workloads Consider all workloads OLTP (typically random IOs) OLTP (typically random IOs) Batch (could be random or sequential depending on the type of work done) Batch (could be random or sequential depending on the type of work done)

26 Disk subsystem considerations Avoid Avoid Auto growth of tempdb Auto growth of tempdb Pre-allocate to max workload Pre-allocate to max workload Unnecessary scans, sorting Unnecessary scans, sorting Object placement Object placement Attempt to achieve even IO distribution Attempt to achieve even IO distribution Difficult to master with complex queries, multiple indexes, joins Difficult to master with complex queries, multiple indexes, joins Round-robin, proportional fill Round-robin, proportional fill

27 I/O bound – spreading IO across multiple spindles Example Example 1) IO Baseline ::fn_virtualfilestats 2) Add database file 3) BCP or Create Index 4) Repeat IO baseline

28 Example Step 1: baseline IO -- step1: baseline IO select step='step 1 - baseline I/O for file 1.',* into #FileStats1 from -- File 3 doesnt exist yet select step='step 1 - baseline I/O for file 3.',* into #FileStats3 from DBCC dropcleanbuffers -- flush cache

29 Example Step 2: Add DB file -- Step 2: Add database file 3 if not exists (select * from sysfiles where name = mydb_Data2') ALTER DATABASE mydb ADD FILE --- [to filegroup default] ( NAME = mydb_Data2, FILENAME = 'c:\Program Files\Microsoft SQL Server\MSSQL\Data\mydb_dat2.ndf', FILENAME = 'c:\Program Files\Microsoft SQL Server\MSSQL\Data\mydb_dat2.ndf', SIZE = 10MB, SIZE = 10MB, MAXSIZE = UNLIMITED, MAXSIZE = UNLIMITED, FILEGROWTH = 10% ) FILEGROWTH = 10% )

30 Example step 3: Re-create index -- Step 3: Re-create Index (or BCP) create clustered index X1 on mytab(a) ON default -- automatically rebuilds Nonclustered indexes, unless drop_existing specified

31 Example Step 4: New IO baseline -- Step 4: New IO baseline dbcc dropcleanbuffers-- clears out data cache select count(*) from mytab insert into #FileStats1 select 'step 4 - table scan after clustered index rebuild',* from insert into #FileStats3 select 'step 4 - table scan after clustered index rebuild',* from

32 Resource Queuing: CPU Win NT Thread Network Handler UMSWorkQueue Win NT Thread 0 UMSWorkQueueUMSWorkQueueUMSWorkQueue Network Handler Notified When I/O Completes UMS Schedules Fibers Network Fibers Write Directly to Clients Network NT Queues Reads Issued by Fibers to I/O Completion Port CPU n CPU 1 CPU 2 CPU 0 Win NT Thread 1 Win NT Thread 2 Win NT Thread n Fibers NT I/O Completion Port

33 Resource Queuing: CPU Common uses of CPU resources Common uses of CPU resources Context switching Context switching Recompilation Recompilation Sorting Sorting Hashing (e.g. Joins) Hashing (e.g. Joins) Lightweight pooling (fiber-mode) Lightweight pooling (fiber-mode) Can provide 15% boost Can provide 15% boost High CPU (+- >85%) High CPU (+- >85%) High context switching > +-20K High context switching > +-20K CPU affinity (limit thread jumping) CPU affinity (limit thread jumping)

34 SQL thread management Dbcc sqlperf(umsstats) Example Example Scheduler ID.0 num users 13.0 num users 13.0 num runnable.0 num runnable.0 num workers 12.0 num workers 12.0 idle workers 5.0 idle workers 5.0 work queued.0 work queued.0 cntxt switches <-sql cntxt switches <-sql cntxt switches(idle) <-kernel cntxt switches(idle) <-kernel Scheduler Switches.0 Total Work

35 ItemDefinition num users Number of SQL threads on the scheduler num runnable Number of SQL threads that are runnable (Scheduler queue length) num workers Number of actual workers to process threads (thread pool) idle workers Number of idle workers cntxt switches Context switches between runnable threads cntxt switches(idle) Context switches to the idle thread SQL thread management DBCC sqlperf(umsstats)

36 Example: Tracking UMSSTATS create proc track_umsstats int=1) as -- T. Davidson is number of times to capture umsstats, default is 5 times -- default delay interval is 1 minute create table #umsstats (Statistic varchar(80),Value numeric(10,1), now datetime default getdate()) dbcc sqlperf (umsstats,clear) -- clear out umsstats varchar(8) = ':00' while begin insert into #umsstats (Statistic,Value) exec ('dbcc sqlperf(umsstats)') insert into #umsstats (Statistic,Value) exec ('dbcc sqlperf(umsstats)') + 1 waitfor end select * from #umsstats order by Statistic,now

37 Resource Queuing: Network Exceeding capabilities of NICs Exceeding capabilities of NICs Check perfmon SQLServer:SQL Statistics:Batch Requests/Sec Check perfmon SQLServer:SQL Statistics:Batch Requests/Sec > 3000/sec about max on 100Mb hardware > 3000/sec about max on 100Mb hardware Exceeding network bandwidth Exceeding network bandwidth Check perfmon [Network Interface:Bytes Total/Sec] / [Network Interface:Current Bandwidth] >.6 is pushing it. Check perfmon [Network Interface:Bytes Total/Sec] / [Network Interface:Current Bandwidth] >.6 is pushing it.

38 Concurrency Issues: Latching and Locks Locks Locks Ensure transactional integrity and database consistency Ensure transactional integrity and database consistency Latching Latching Light weight short term synchronization object Light weight short term synchronization object Short-term in duration ( 1/4 of overhead of lock) Short-term in duration ( 1/4 of overhead of lock) Locking Locking sp_lock sp_lock DBCC SQLPERF(waitstats) DBCC SQLPERF(waitstats) syslockinfo syslockinfo sysprocesses (wait states, wait types) sysprocesses (wait states, wait types)

39 Locking and Indexes Tables With Clustered Indexes Always Take KEY Locks Tables With Clustered Indexes Always Take KEY Locks Never RID Locks Never RID Locks Sp_indexoption Can Control Locking in Indexes Sp_indexoption Can Control Locking in Indexes Occasionally Useful for Deadlock Avoidance Occasionally Useful for Deadlock Avoidance

40 Concurrency Issues: Transactions Exclusive locks held until commit Exclusive locks held until commit Shared locks – depends on transaction Isolation level Shared locks – depends on transaction Isolation level Tran Isolation level affects the behavior (duration) of shared locks Tran Isolation level affects the behavior (duration) of shared locks Read committed – default, shared released Read committed – default, shared released Repeatable Read – locks rows until commit Repeatable Read – locks rows until commit Serializable – locks ranges of rows until commit (no phantoms) Serializable – locks ranges of rows until commit (no phantoms) Joins Joins

41 Finding Long Transactions Use DBCC OPENTRAN Use DBCC OPENTRAN Or: query sysprocesses & syslockinfo: Or: query sysprocesses & syslockinfo: SELECT spid,cmd,status,loginame, open_tran, datediff (s, last_batch, getdate ()) As [WaitTime (s)] FROM master..sysprocesses p WHERE open_tran > 0 and spid > 50 and datediff (s, last_batch, getdate ()) > 30 and exists (select * from master..syslockinfo l where req_spid = p.spid and rsc_type <> 2)

42 Querying for waits Display All User Threads Waiting on a Resource > 5 Seconds: Display All User Threads Waiting on a Resource > 5 Seconds: /* Use this query to look for user threads that have been waiting on any resource for > 5 seconds. It's helpful to locate blocking locks, I/O, etc. */ SELECT spid, waittime, lastwaittype, waitresource FROM master..sysprocesses WHERE waittime > Report any waits > 5 secs AND spid > In SQL 2000 all user spids are > this should be ~12 in SQL 7.0

43 Techniques to avoid blocking Keep Transactions Short and in One Batch Keep Transactions Short and in One Batch No User Interaction During Transactions No User Interaction During Transactions Rollback when Canceling; Rollback on Any Error or Timeout Rollback when Canceling; Rollback on Any Error or Timeout Beware of Implicit Transactions Beware of Implicit Transactions Process Results Quickly Process Results Quickly Reduce Isolation Level to Lowest Possible Reduce Isolation Level to Lowest Possible Stress Test at Maximum Projected User Load Before Deployment Stress Test at Maximum Projected User Load Before Deployment Proper Indexing – Index Tuning Wizard or Index Analysis Proper Indexing – Index Tuning Wizard or Index Analysis Other Possibilities to Consider: Other Possibilities to Consider: Locking hint, Index hint, Join hint Locking hint, Index hint, Join hint

44 Techniques to minimize deadlocks Prevent Blocking Prevent Blocking In-order Resource Access In-order Resource Access Bound Connections Bound Connections Add and/or Remove Indexes to Provide or Remove Alternate Access Path to the Desired Resource Add and/or Remove Indexes to Provide or Remove Alternate Access Path to the Desired Resource SET DEADLOCK_PRIORITY LOW SET DEADLOCK_PRIORITY LOW Deadlock Retry Logic in Application Deadlock Retry Logic in Application

45 Concurrency Issues: Memory and Locks Memory-intensive queries are queued if not enough memory available Memory-intensive queries are queued if not enough memory available Sorting Sorting Hashing (e.g. joins) Hashing (e.g. joins) Locks could also be held during the wait for memory, and throughout sorting and hashing Locks could also be held during the wait for memory, and throughout sorting and hashing See locks and transactions See locks and transactions

46 Concurrency issues: Cache and 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 memory Single I/O from different areas of memory Asynchronous I/O Asynchronous I/O Use worker threads for I/O in anticipation Use worker threads for I/O in anticipation Buffer cache Buffer cache Free buffer list Free buffer list Clock-out algorithm – reference page counter (stale) Clock-out algorithm – reference page counter (stale) Lazy-writer – throw out page (SQL Counter: Lazy writer p/s) Lazy-writer – throw out page (SQL Counter: Lazy writer p/s) Buffer page Buffer page Header reference counter and dirty page indicator Header reference counter and dirty page indicator

47 Concurrency Issues: Cache and I/O Freeing and writing buffer pages Freeing and writing buffer pages Lazywriter – runs regular intervals Lazywriter – runs regular intervals Maintains size of free buffer list Maintains size of free buffer list Scans buffer list Scans 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/O Performs asynch I/O scans buffer list scans buffer list Writes dirty pages Writes dirty pages Checkpoint Checkpoint Frequency based on recovery interval and number of records in transaction log Frequency based on recovery interval and number of records in transaction log Writes dirty log and data pages (SQL Buffer Mgr: Checkpoint pages/sec) Writes dirty log and data pages (SQL Buffer Mgr: Checkpoint pages/sec)

48 Concurrency Issues: Cache and I/O Memory intensive operations may flush other pages from SQL Cache Memory intensive operations may flush other pages from SQL Cache Successive reads of flushed data are physical Successive reads of flushed data are physical Possible lower hit rates Possible lower hit rates Very generally - >=90% is good Very generally - >=90% is good Clock algorithm determines pages to be flushed Clock algorithm determines pages to be flushed Decrements reference counter Decrements reference counter

49 Concurrency Issues: Locks and I/O I/O intensive queries can cause transactional locks to be held longer I/O intensive queries can cause transactional locks to be held longer Some wait states (types) for I/O: Some wait states (types) for I/O: Async IO completion Async IO completion Network IO completion Network IO completion IO completion IO completion Waiting on writelog Waiting on writelog Q INF: How to Monitor SQL Server 2000 Blocking Q INF: How to Monitor SQL Server 2000 Blocking

50 Tracking wait states Example Example Waittype, waittime, lastwaittype Dbcc sqlperf(waitstats)

51 Example: Tracking WAITSTATS create proc track_waitstats int=1) as -- T. Davidson create table #waitstats ([Wait Type] varchar(80), Requests numeric(10,1), [Wait Time] numeric (10,1), [Signal Wait Time] numeric(10,1), now datetime default getdate()) dbcc sqlperf (waitstats,clear) -- clear out waitstats varchar(8) = ':00' while begin insert into #waitstats ([Wait Type], Requests, [Wait Time],[Signal Wait Time]) exec ('dbcc sqlperf(waitstats)') + 1 waitfor end select * from #waitstats order by [Wait Type],now

52 Query Processing Concepts Compilation vs Optimization Compilation vs Optimization Only DML can be Optimized! Only DML can be Optimized! TSQL Statements are Compiled TSQL Statements are Compiled Delay Between Compilation and Execution Delay Between Compilation and Execution Plans Needing More Resources May Need to Wait Plans Needing More Resources May Need to Wait

53 Query Processing Overview Plan in cache? Parse/Normalize Compile TSQL Optimize SQL statements Put plan in cache No Retrieve plan from cache Yes Plan need to be recompiled? Yes Run plan to completion No Wait for Memory Grant Scheduler to ok request

54 Procedure Cache Not a Separate Area of Memory Not a Separate Area of Memory Compilation is Often Expensive Compilation is Often Expensive Reusing High-Cost Plans is Good Reusing High-Cost Plans is Good Some Queries are Cheap to Compile Some Queries are Cheap to Compile May not be worth keeping plan around May not be worth keeping plan around Procedure Cache Balances Cost to Recompile with Frequency of Use Procedure Cache Balances Cost to Recompile with Frequency of Use

55 Procedure Cache Management Ad-hoc & cheap to compile Allocate memory from buffer cache Dont Cache Yes Set initial cost to zero Type of plan? Ad-hoc Set initial cost to creation cost Put plan in cache Retrieve plan from cache Type of plan? Increment cost by 1 Set cost to creation cost Return plan Ad-hoc Retrieve Plan Put Plan in Cache

56 Concurrency Issues: Cached Objects master..syscacheobjects master..syscacheobjects Procedure or batch name Procedure or batch name Set options for plans Set options for plans Ref counts, Use counts Ref counts, Use counts Compiled plan Compiled plan Single copy (serial and parallel) – unlike SQL 6.5 Single copy (serial and parallel) – unlike SQL 6.5 Re-entrant and re-usable Re-entrant and re-usable Executable plan Executable plan Data structure for user context, not re-entrant Data structure for user context, not re-entrant Plan re-use of… Plan re-use of… Procs, Triggers, Views Procs, Triggers, Views Defaults, Check contraints, rules Defaults, Check contraints, rules adhoc SQL, sp_executesql adhoc SQL, sp_executesql

57 Concurrency Issues: Re- compilation Profiler Profiler Lists recomp events and statements Lists recomp events and statements Data column for reason: EventSubClass Data column for reason: EventSubClass Locks on system tables Locks on system tables Re-compiling stored procedure plans serialize other users during re-comp. Re-compiling stored procedure plans serialize other users during re-comp. Re-compilation based on Re-compilation based on Rows changed threshold - sysindexes.rowmodctr Rows changed threshold - sysindexes.rowmodctr DDL & schema changes DDL & schema changes Some Set options Some Set options

58 Recompilation Can Be Expensive Can Be Expensive Optimization is really the expensive part! Optimization is really the expensive part! Plans Can Be Saved for AdHoc Queries Plans Can Be Saved for AdHoc Queries Queries Can be AutoParameterized Queries Can be AutoParameterized Minimizing Recompilations of Procedures Minimizing Recompilations of Procedures

59 EventSubClass: Reason for recompilation ReasonDescription 1 Schema, bindings or permissions changed between compile or execute. 2 Statistics changed. 3 Object not found at compile time, deferred check to run-time. 4 Set option changed in batch. 5 Temp table schema, binding or permission changed. 6 Remote rowset schema, binding or permission changed.

60 Viewing Statistics DBCC SHOW_STATISTICS DBCC SHOW_STATISTICS DBCC SHOW_STATISTICS ( table, target ) DBCC SHOW_STATISTICS ( table, target ) Up to 200 Steps are Sampled Up to 200 Steps are Sampled ALL DENSITY Is Shown for All Left- based Key Combinations ALL DENSITY Is Shown for All Left- based Key Combinations

61 Tracking re-compilation demo demo Profiler: EventSubClass

62 Conclusions SQL Server 2000 is a high performance enterprise class database. SQL Server 2000 is a high performance enterprise class database. Performance Tuning is essential to Reach peak system capabilities. Performance Tuning is essential to Reach peak system capabilities. Exploit all resources Exploit all resources CPU CPU I/O I/O Network Network Data (concurrency vs. consistency) Data (concurrency vs. consistency)

63 References Inside SQL Server 2000 by Kalen Delaney Inside SQL Server 2000 by Kalen Delaney SQL Server 2000 Performance Tuning by Whalen, Garcia, DeLuca, Thompson SQL Server 2000 Performance Tuning by Whalen, Garcia, DeLuca, Thompson SQL Server 2000 Performance Tuning Technical Reference (MS Press) SQL Server 2000 Performance Tuning Technical Reference (MS Press) SQL Server 2000 Recompilation whitepaper by T. Davidson SQL Server 2000 Recompilation whitepaper by T. Davidson

64 Resources from Microsoft Press For more information please visit the TechEd Bookshop. SQL SERVER 2000

65 Dont forget to complete the on-line Session Feedback form on the Attendee Web site https://web.mseventseurope.com/teched/ https://web.mseventseurope.com/teched/

66


Download ppt "DAT 342 Advanced SQL Server Performance and Tuning Bren Newman Program Manager SQL Server Development Microsoft Corporation."

Similar presentations


Ads by Google