sql performance tuning 25th jan

92
Microsoft® SQL Server2000 Performance Matthew Stephen SQL Server Evangelist http://blogs.msdn.com/mat_stephen [email protected] Microsoft Corporation

Upload: eduardo-friaza

Post on 10-Mar-2015

104 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: SQL Performance Tuning 25th Jan

Microsoft® SQL Server™ 2000 Performance

Matthew StephenSQL Server Evangelisthttp://blogs.msdn.com/[email protected] Corporation

Page 2: SQL Performance Tuning 25th Jan

What We Will Cover• Locking• Query Processor• Query Tuning• System Configuration• Performance Monitoring

Page 3: SQL Performance Tuning 25th Jan

Session Prerequisites• This session assumes that you

understand the fundamentals of– Windows® 2000 Server– SQL Server 2000– System Monitor

Level 200-300Level 200-300

Page 4: SQL Performance Tuning 25th Jan

Agenda

• Locking

• Query Processor

• Query Tuning

• System Configuration

• Performance Monitoring

Page 5: SQL Performance Tuning 25th Jan

Lock ManagerWhat it does for you

• Acquires and Releases Locks

• Maintains compatibility Between Lock Modes

• Resolves Deadlocks

• Escalates Locks

• Uses 2 Locking Systems– Shared Data Locks– Internal latches for Internal data and index

concurrency

Page 6: SQL Performance Tuning 25th Jan

Lock Isolation Levels

• Supports all 4 ANSI and ISO isolation levels– Serializable– Repeatable Read– Read Committed– Read Uncommitted

Page 7: SQL Performance Tuning 25th Jan

LockingUser data lock types – Shared

• Acquired automatically when data is read

• Applies to Table, Page, Index Key or row.

• Many processes can hold a shared lock on the same data.

• Cannot be locked exclusively while in shared lock mode*

*Unless it is the same process that holds the shared lock

Page 8: SQL Performance Tuning 25th Jan

LockingLock Granularity for user data

Page 9: SQL Performance Tuning 25th Jan

LockingUser data lock types – Exclusive

• Automatically acquired when data is modified

• Only one process can hold at a time on any data.

• Held until the end of a Transaction

• All other lock requests by other processes will be refused.

• Can use Query hints to decide to read locked data.

Page 10: SQL Performance Tuning 25th Jan

LockingUser data lock types – Update

• Hybrid of shared and exclusive

• Acquired when a search is required before and modification

• Allow others to still read while lock applied

• Needs an exclusive lock to modify data

• Data can have many shared but only one update

Page 11: SQL Performance Tuning 25th Jan

LockingUser data lock types – Intent

• Not a real lock mode just a qualifier e.g.Intent Update lock

• Qualifier to modes already discussed

Page 12: SQL Performance Tuning 25th Jan

LockingUser data lock types – Special

• 3 special modes– Schema Stability – used when queries are

modified, prevents scheme modification locks.– Scheme Modification – used when table structures

are being modified– Bulk update – used when the BULK INSERT or

BCP command are used.

Page 13: SQL Performance Tuning 25th Jan

LockingViewing lock information

• Use the sp_lock stored procedure

• Shows current and waiting locks

Spid Dbib Objid IndId Type Resource Mode Status

54 pubs 19723 2 TAB IS GRANT

58 pubs 19755 1 PAG 1:88 IX GRANT

52 Pubs 0 0 DB S GRANT

Page 14: SQL Performance Tuning 25th Jan

LockingViewing lock information - Type

Abbr Resource Internal

Code

Description / Example

DB Database 2

TAB Table 5 Table id 261775902

EXT Extent 8 File/ page number 1:96

PAG Page 6 File/ page number 1:104

KEY Key 7 Hashed value ac0001a10a00

AC Row 9 File/page/slot number 1:151:4

APP Application 10 Hash of the app name MYpr8dea

Page 15: SQL Performance Tuning 25th Jan

LockingViewing lock information - ModeAbbreviation Mode Internal code

S Shared 4

X Exclusive 6

U Update 5

IS Intent shared 7

IU Intent update 8

IX Intent exclusive 9

SIX Shared with intent exclusive 11

Sch-S Schema stability 2

Sch-M Schema modification 3

BU Bulk update 13

Page 16: SQL Performance Tuning 25th Jan

Locking Lock Overhead

• Lock Overhead– Each lock – 32 bytes– Each Process holding lock – 32 bytes– Each Process waiting for lock – 32 bytes

Page 17: SQL Performance Tuning 25th Jan

Agenda

• Locking

• Query Processor

• Query Tuning

• System Configuration

• Performance Monitoring

Page 18: SQL Performance Tuning 25th Jan

Query Processor Query Compilation

Page 19: SQL Performance Tuning 25th Jan

Query Processor Query Optimization

Page 20: SQL Performance Tuning 25th Jan

Query Processor How the Optimizer Works• Query Analysis

• Index Selection

• Join Selection– Nested iteration– Hashing– Merging

Page 21: SQL Performance Tuning 25th Jan

Query Processor Cost and cache plan

Page 22: SQL Performance Tuning 25th Jan

Query ProcessorCompilation and execution flow

Page 23: SQL Performance Tuning 25th Jan

Agenda

• Locking

• Query Processor

• Query Tuning

• System Configuration

• Performance Monitoring

Page 24: SQL Performance Tuning 25th Jan

Query TuningTuning – When to Start

• Start at the Beginning• Consider performance before you even

write your first line of code• Be sure that you've set up a good

database structure• Create what appear to be useful indexes• Make sure all analysis is done with a

representative workload

Page 25: SQL Performance Tuning 25th Jan

Query Tuning Application and Database Design

• Provides Biggest Performance Gains

• Normalize

• Evaluate Your Critical Transactions

• Keep Table Row Lengths and Key Lengths Compact

• Create Useful Indexes

• Benchmark, Prototype and Test

Page 26: SQL Performance Tuning 25th Jan

SQL ServerSQL Server

SQL ProfilerSQL ProfilerIndex Tuning Index Tuning

WizardWizard

Tuni

ng

Tuni

ng

Rec

omm

enda

tions

Rec

omm

enda

tions

Real-Tim

e

Real-Tim

e

Queries

Queries

(Filtered)(Filtered)WorkloadWorkload

Query tuningIndex Creation and Tuning

Page 27: SQL Performance Tuning 25th Jan

Query PerformanceGraphical Execution Plan

Page 28: SQL Performance Tuning 25th Jan

Query Tuning Monitoring Query Performance

• STATISTICS – Input/Output – Logical Reads– Physical Reads– Read Ahead Reads– Scan Count

• STATISTICS - Timings

• SHOWPLAN– Showplan_Text, Showplan_All, Graphical

Showplan

Page 29: SQL Performance Tuning 25th Jan

Query Tuning Query Hints

• Query hints should be used for special cases—not as standard operating procedure

• Hint Types:– Join Hints– Index Hints– Lock Hints– Processing Hints

Page 30: SQL Performance Tuning 25th Jan

Query Tuning Blocking and Deadlocks – How to Resolve Them• Keep transactions as short as possible

• Never add a pause within a transaction for user input

• When you process a result set, process all rows as quickly as possible

• For browsing applications, consider using cursors with optimistic concurrency control

Page 31: SQL Performance Tuning 25th Jan

Query Tuning Deadlocks – How to Resolve Them

• To prevent cycle deadlocks, make all processes access resources in a consistent order.

• Reduce the transaction isolation level if it's suitable for the application.

• To prevent conversion deadlocks, explicitly serialize access to a resource.

Page 32: SQL Performance Tuning 25th Jan

Gather information about the application’s behavior Use SQL Profiler

Analyze the information Query Analyzer Index Tuning Wizard

Apply Changes Index Tuning Wizard Enterprise Manager

Query tuningPerformance Tuning

• A step-by-step approach

Page 33: SQL Performance Tuning 25th Jan

Agenda

• Locking

• Query Processor

• Query Tuning

• System Configuration

• Performance Monitoring

Page 34: SQL Performance Tuning 25th Jan

System Configuration Resource Allocation and System File Location

• Ensure that Maximize Data Throughput for Network Applications is selected

• Do not locate SQL Server files on same drive as PAGEFILE.sys

Page 35: SQL Performance Tuning 25th Jan

System Configuration Configuring SQL Resources

• Sp_configure– EXEC sp_configure

• RECONFIGURE– With override

• Enterprise Manager

Page 36: SQL Performance Tuning 25th Jan

System Configuration Configuring SQL Resources

• Min Server Memory and Max Server Memory

• Set Working Set Size• Minimum Query

Memory

Page 37: SQL Performance Tuning 25th Jan

System Configuration Configuring SQL Resources

• Scheduling– Lightweight Pooling– Affinity mask– Priority boost– Max Worker Threads

• Disk I/O Options

Page 38: SQL Performance Tuning 25th Jan

System Configuration Configuring SQL Resources • Query Processing Options

– Min Memory Per Query– Query Wait– Index Create Memory– Query Governor Cost Limit– Max Degree of Parallelism

Page 39: SQL Performance Tuning 25th Jan

System Configuration Configuring SQL Resources• Database Options

– Read Only– Single User– Autoclose– Autoshrink– Auto Create Statistics– Auto Update Statistics

Page 40: SQL Performance Tuning 25th Jan

System Configuration Configuring SQL Resources

• Buffer Manager– “Pintable” Option– Monitoring Performance

• SQLPERF(WAITSTATS)• SQLPERF(LRUSTATS)

Page 41: SQL Performance Tuning 25th Jan

Agenda

• Locking

• Query Processor

• Query Tuning

• System Configuration

• Performance Monitoring

Page 42: SQL Performance Tuning 25th Jan

Performance Monitoring How to Use SQL Profiler

• Graphical tool to monitor and collect server events

• Step through problem queries to find the cause of the problem

• Identify poorly-performing queries• Capture the series of SQL statements that

lead to a problem• Use the saved traces to replicate problems on

a test server where they can be diagnosed

Page 43: SQL Performance Tuning 25th Jan

Performance MonitoringHow to Use SQL Profiler

• Debug T-SQL or stored procedures• Monitor the performance of SQL Server

to tune workloads• Capture deadlocking scenarios• Playback events captured

Page 44: SQL Performance Tuning 25th Jan

Performance Monitoring SQL Profiler • Event Categories

• Data Columns

• Filters

Page 45: SQL Performance Tuning 25th Jan

Performance MonitoringSystem Stored Procedures

• SQL Trace– sp_trace_create– sp_trace_setevent– sp_trace_setfilter– sp_trace_setstatus– sp_trace_generateevent

• SQLDIAG

Page 46: SQL Performance Tuning 25th Jan

Performance Monitoring Using System Monitor

• Monitors Entire System Performance

• System Counters

• SQL Counters

Page 47: SQL Performance Tuning 25th Jan

Performance Monitoring System Monitor• View data simultaneously from any number of

computers• View and change charts to reflect current

activity, and show counter values that are updated at a user-defined frequency

• Export data from charts, logs, alert logs, and reports to spreadsheet or database applications for further manipulation and printing

• Add system alerts that list an event in the alert log and can notify you by reverting to the Alert view or issuing a network alert

Page 48: SQL Performance Tuning 25th Jan

Performance Monitoring System Monitor• Run a predefined application the first time or

every time a counter value goes over or under a user-defined value

• Create log files that contain data about various objects from different computers

• Append to one file selected sections from other existing log files to form a long-term archive

• View current-activity reports, or create reports from existing log files

• Save individual chart, alert, log, or report settings, or the entire workspace setup for reuse when needed

Page 49: SQL Performance Tuning 25th Jan

Performance Monitoring System Monitor - System Counters

• System:– Context Switches/sec

• Processor: – %Processor Time– %Privileged Time– %User Time– Processor Queue Length

Page 50: SQL Performance Tuning 25th Jan

Performance Monitoring System Monitor - System Counters

• SQL Server: Memory Manager:– Total Server Memory(KB)

• Process: – Working Set Counter For SQL Server

Instance

• SQL Server Buffer Manager:– Buffer Cache Hit Ratio

Page 51: SQL Performance Tuning 25th Jan

Performance Monitoring System Monitor - System Counters

• Memory: – Pages/sec

• SQLServer: Databases– Transactions/sec

• PhysicalDisk:– Disk Transfers/sec

Page 52: SQL Performance Tuning 25th Jan

What can Performance Monitoring do?

• Some typical bottlenecks– What are some of the typical problem areas in

application?• Cache management• Query plan reuse• Recompilation• Transaction management e.g. concurrency• Resource utilization

• Doesn’t replace need for good app design

Page 53: SQL Performance Tuning 25th Jan

Waits & Queues• Complementary information

– Together, explains app performance– Most are familiar with queues ( ½ the story)– Both are essential for problem determination

• Queues– Perfmon counters - measure resource utilization– Queues indicate unfulfilled resource requests

• SQL Waits– Waits from an Application or User connection

perspective

Page 54: SQL Performance Tuning 25th Jan

Waits

• SQL waits– Application sends SQL queries

• SQL Server in turn issues resource requests– Translates to subsystem IO, memory, CPU– E.g. acquire locks, read | write data pages, sorts

• 50+ wait types for SQL2000• Anytime a user connection waits for results, SQL

sets a wait type

Page 55: SQL Performance Tuning 25th Jan

Queues• PERFMON counters provide a view

of system performance from a resource standpoint

• Queues measure resource utilization– Requests that cannot be immediately

provided are queued– Key Perfmon counters

• Physical Disk (IO)• SQL Buffer Cache• CPU

Page 56: SQL Performance Tuning 25th Jan

Wait types 1

• If a thread is not currently executing, a wait type or state is set in sysprocesses

• Sysprocesses contains– Lastwaittype– Waitype– Waittime

• Limitation: Transience of sysprocesses, spid history

Page 57: SQL Performance Tuning 25th Jan

Wait types 2• DBCC sqlperf(waitstats,[clear])

– Cumulative waittypes & waittimes– Limitation: relevance types, what you can control?

Page 58: SQL Performance Tuning 25th Jan

Track_waitstats stored proc• captures SQL waitstats from DBCC

SQLPERF • ranks wait types in descending order based

on percentage of total waits– identifies greatest opportunites for

performance improvement

• Can be graphed to see how waits vary over a workload

• Limitation: long durations can “hide” or smooth out waits

Page 59: SQL Performance Tuning 25th Jan

Track_waitstats code (1 of 2)CREATE proc track_waitstats (@num_samples int=10,@delaynum int=1,@delaytype nvarchar(10)='minutes')as-- T. Davidson-- This stored procedure is provided "AS IS" with no warranties, and confers no rights. -- Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm-- @num_samples is the number of times to capture waitstats, default is 10 times. default delay interval is 1 minute-- delaynum is the delay interval. delaytype specifies whether the delay interval is minutes or seconds-- create waitstats table if it doesn't exist, otherwise truncateset nocount onif not exists (select 1 from sysobjects where name = 'waitstats')

create table waitstats ([wait type] varchar(80), requests numeric(20,1),[wait time] numeric (20,1),[signal wait time] numeric(20,1),now datetime default getdate())

else truncate table waitstatsdbcc sqlperf (waitstats,clear) -- clear out waitstatsdeclare @i int,@delay varchar(8),@dt varchar(3), @now datetime, @totalwait numeric(20,1)

,@endtime datetime,@begintime datetime,@hr int, @min int, @sec int

select @i = 1select @dt = case lower(@delaytype)

when 'minutes' then 'm'when 'minute' then 'm'when 'min' then 'm'when 'mm' then 'm'when 'mi' then 'm'when 'm' then 'm'when 'seconds' then 's'when 'second' then 's'when 'sec' then 's'when 'ss' then 's'when 's' then 's'else @delaytype

endif @dt not in ('s','m')begin

print 'please supply delay type e.g. seconds or minutes'return

end

Page 60: SQL Performance Tuning 25th Jan

Track_waitstats code (2 of 2)

if @dt = 's'begin

select @sec = @delaynum % 60select @min = cast((@delaynum / 60) as int)select @hr = cast((@min / 60) as int)select @min = @min % 60

endif @dt = 'm'begin

select @sec = 0select @min = @delaynum % 60select @hr = cast((@delaynum / 60) as int)

endselect @delay= right('0'+ convert(varchar(2),@hr),2) + ':' +

+ right('0'+convert(varchar(2),@min),2) + ':' + + right('0'+convert(varchar(2),@sec),2)

if @hr > 23 or @min > 59 or @sec > 59begin

select 'hh:mm:ss delay time cannot > 23:59:59'select 'delay interval and type: ' + convert (varchar(10),@delaynum) + ',' + @delaytype + ' converts to ' + @delayreturn

endwhile (@i <= @num_samples)begin insert into waitstats ([wait type], requests, [wait time],[signal wait time])

exec ('dbcc sqlperf(waitstats)')select @i = @i + 1waitfor delay @delay

End--- create waitstats reportexecute get_waitstats

Page 61: SQL Performance Tuning 25th Jan

Get_waitstats codeCREATE proc get_waitstatsas-- This stored procedure is provided "AS IS" with no warranties, and confers no rights. -- Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm---- this proc will create waitstats report listing wait types by percentage-- can be run when track_waitstats is executingset nocount on

declare @now datetime, @totalwait numeric(20,1),@endtime datetime,@begintime datetime,@hr int, @min int, @sec int

select @now=max(now),@begintime=min(now),@endtime=max(now)from waitstats where [wait type] = 'Total'

--- subtract waitfor, sleep, and resource_queue from Totalselect @totalwait = sum([wait time]) + 1 from waitstats where [wait type] not in ('WAITFOR','SLEEP','RESOURCE_QUEUE', 'Total', '***total***') and now = @now

-- insert adjusted totals, rank by percentage descendingdelete waitstats where [wait type] = '***total***' and now = @nowinsert into waitstats select '***total***',0,@totalwait,@totalwait,@now

select [wait type],[wait time],percentage=cast (100*[wait time]/@totalwait as numeric(20,1))from waitstats where [wait type] not in ('WAITFOR','SLEEP','RESOURCE_QUEUE','Total')and now = @noworder by percentage desc

Page 62: SQL Performance Tuning 25th Jan

Track_waitstats Sample output

Page 63: SQL Performance Tuning 25th Jan

Correlating Waits and Queues

• Waits should be correlated with Queues– Look beyond the symptom– Symptom can mask the problem– Identifies the specific resource constraint along

with the associated wait types– Actual problem could be app, SQL, or DB design,

rather than resource limitations

• Helpful to know performance profile when things are good– Now performance is bad - What changed?

Page 64: SQL Performance Tuning 25th Jan

SQL 2000 Waits & Queues * SQL Server Magazine Jan 2004

Page 65: SQL Performance Tuning 25th Jan

Correlating Waits & Queues: IO or Memory Pressure?

Waits Queues ExplanationIO_Completion

Async_IO_Completion

PageIOLatch_x

PageLatch_x

1. SQL Buffer Mgr–Avg Page Life Expectancy (seconds)–Checkpoint pages/sec–Lazywrites/sec

2. Physical Disk–Avg disk sec/read–Avg disk sec/write–Disk queues

Waits indicate IO issue

High Avg disk seconds indicates IO issue

HOWEVER Low average page life indicates memory pressure e.g. cache flushing

Page 66: SQL Performance Tuning 25th Jan

Correlating Waits & Queues – IO or DB Design?

Waits Queues Explanation1. IO_Completion

2. Async_IO_Completion

3. Writelog

1. SQL Buffer Mgr–Avg Page Life Expectancy (seconds)–Checkpoint pages/sec–Lazywrites/sec

2. Physical Disk–Avg disk sec/read–Avg disk sec/write–Disk queues

1. If Profiler shows:

– Scan started

2. If Showplan shows

– Table scans– Clustered

index range scans

– Nonclustered index range scans

– Sorts

Page 67: SQL Performance Tuning 25th Jan

Other sources of information

• Virtual tables• Undocumented

DBCC options

Page 68: SQL Performance Tuning 25th Jan

File Statistics

• Dbcc showfilestats

• Select NumberReads, NumberWrites, BytesRead, BytesWritten, IoStallMS from ::fn_virtualfilestats

Page 69: SQL Performance Tuning 25th Jan

Virtual System Tables• ::Fn_virtualfilestats (dbid, [fileId | -1])

– Provides breakdown of IO by file– Look for IostallMS– compare to Physical Disk reads/writes

• Indentifies SQL component of IO

• Syscacheobjects– Contains compiled objects e.g. query plans

• Sysprocesses– Contains each SQL thread & wait types

• Syslocks

Page 70: SQL Performance Tuning 25th Jan

Stored procedures• Query plans are in syscacheobjects

– Execution plan (N)• User context, variable values, etc.

– Compiled plan (1)• Access strategy - single serial or parallel compiled plan

• Sp_procinfo– Displays stored proc info

• Parameters• Plan re-use (usecounts)• Startup• Owner

Page 71: SQL Performance Tuning 25th Jan

Real memory issues• 32-bit SQL Server

– Virtual memory is limited to 2GB unless using /3GB, then it’s 3GB

• sorting, query plans, user connections, locks, utility buffers compete for virtual memory

• Thrashing (or memory pressure) in real memory means discarding plans & small sorts

– Affects index creation, joins, repeated optimization

– AWE (virtual memory above real)• Data and index pages• AWE map/unmap to access pages in virtual memory

• 64-bit SQL Server– Flat memory model so no limits to proc cache, no AWE

memory mapping

Page 72: SQL Performance Tuning 25th Jan

What is in memory?

• Dbcc memobjlist (lists objects in real memory)

• Dbcc memorystatus

Page 73: SQL Performance Tuning 25th Jan

Syscacheobjects & Re-use

Page 74: SQL Performance Tuning 25th Jan

EventSubClass: Reason for recompilation

1 Schema, bindings or permissions changed between compile or execute.

2 Statistics changed.

3 Object not found at compile time, deferred check at run-time.

4 Set option changed in batch.

5 Temp table schema, binding or permission changed.

6 Remote rowset schema, binding or permission changed.

Page 75: SQL Performance Tuning 25th Jan

Uses of CPU resourcesSorts Distincts, order by,

group byCompare to index & IX overhead

Joins Worktables, temp tables

Query plans indicate joins,types

Lack of plan re-use

SQL Compiles – SQL Recompiles

syscacheobjects usecounts

Look at parameterization

Recompilation SQL Recompiles/sec

Compare to batch requests

Manages IO Profiler Scans, memory pressure, DB design

Index utilization?

Page Life high enough?

UMS – context switching

Dbcc sqlperf(umsstats)

Look for BIG differences. Match big jobs with CPUs

Page 76: SQL Performance Tuning 25th Jan

When does more memory help?

• Additional memory may help if…..– SQL Buffer Mgr:Page Life Expectancy is low

• Average time in seconds a page stays in cache

– SQL Server Memory Mgr: Memory Grants Pending is high

• SQL won’t execute a query if there isn’t enough memory for it

• If after adding, Page Life Expectancy is higher, more memory helps performance

Page 77: SQL Performance Tuning 25th Jan

Concurrency: Helpful Lock scripts

• Sp_blockinfo – lists locking chainDBCC traceon (3605,3604)dbcc lock(StallReportThreshold, 200) –

Dumps all locks greater than 200 ms to SQL errorlog

Page 78: SQL Performance Tuning 25th Jan

Design Issues• There are design considerations resulting from the

Waits and Queues methodology.

Observation Application issue Possible remedies

High IO waits 1. Database design

2. Memory Pressure

1. Bad query plans resulting from improper indexing.

2. Add correct indexes to minimize IO.

3. Add more memory

High CPU Utilization 1. Memory pressure

2. Plan re-use

3. Recompilation

4. Parameterization

1. Check for correct plan re-use, parameterization, re-compilation , see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql2k/html/sql_queryrecompilation.asp

High Blocking / Low concurrency

1. Transaction management

1. Redo transaction management

2. use correct transaction isolation levels

Page 79: SQL Performance Tuning 25th Jan

Summation

• Application performance all about waits and queues– Workloads may vary, shifting waits &

queues

• System performance is all about identifying (& resolving) bottlenecks, maximizing system capabilities– Figure out the problem, if it ain’t

broke….

Page 80: SQL Performance Tuning 25th Jan

DB Files and Placement I• Ideally all database data files (filegroups) and log files

should be on separate devices, including TempDB• The same is true for SAN storage• SAN Sector alignment must be a multiple of 8KB (size of

SQL Sever data page) .– 64Kb boundary for best performance.– Some useful SAN configuration information can be found in

this articlehttp://www.microsoft.com/sql/techinfo/administration/2000/rosetta.asp

• To test storage (SAN or directly attached storage) IO throughput use: SQLIO Disk Subsystem Benchmark Tool (free to SQL users)– http://www.microsoft.com/downloads/details.aspx?FamilyId

=9A8B005B-84E4-4F24-8D65-CB53442D9E19&displaylang=en

Page 81: SQL Performance Tuning 25th Jan

DB Files and Placement II

• For maximum IO throughput there should be as many data files as there are processors

• Start off with a sufficient number of files (minimum 3). Avoid adding files at a later time

• Avoid automatic growth of data files so as not to lose proportional fill• Keep all files at the same size

From Books Online:

As data is written to the filegroup, Microsoft® SQL Server™ writes an amount proportional to the free space in the file to each file within the filegroup, rather than writing all the data to the first file until full and then writing to the next file.

As soon as all the files in a filegroup are full, SQL Server automatically expands one file at a time in a round-robin fashion to accommodate more data (provided that the database is set to grow automatically).

Page 82: SQL Performance Tuning 25th Jan

Performance Monitoring• SQL Server has hundreds of counters that can be monitored and

recorded using Performance (System) Monitor. This requires no additional, specialised or third party software.

• SAP will use some of these counters for its own reports.• SELECT * FROM :: fn_virtualfilestats(-1, -1)

– IoStallMS / NumberReads< 5ms good, <10 ms fair, < 20 ms poor

– More information on this command from:Miscellaneous SQL Server Performance Tuning Tips http://www.sql-server-performance.com/misc_tips.asp

• dbcc sqlperf (waitstats)– WRITELOG (Wait time / requests)

< 5 ms good, <10ms fair– PAGEIOLATCH_SH (Wait time / requests)

< 15 ms good• Windows perfmance counters

– Disk queue length – can be very misleading on SAN equipment where a permanently high figure may be reported without any performance issue.

– Average disk transfer times– For SQL Server counters and other useful Windows counters see

Microsoft SQL Server 2000 RDBMS Performance Tuning Guide for Data Warehousinghttp://www.microsoft.com/technet/prodtechnol/sql/2000/maintain/rdbmspft.mspx

Page 83: SQL Performance Tuning 25th Jan

Performance Monitoring

• DBCC INDEXDEFRAG– An online operation for index

defragmenting. Effects of fragmentation are minimal on a SAN with lots of spindles.

Microsoft SQL Server 2000 RDBMS Performance Tuning Guide for Data Warehousinghttp://www.microsoft.com/technet/prodtechnol/sql/2000/maintain/rdbmspft.mspxHOW TO: Troubleshoot Application Performance with SQL Serverhttp://support.microsoft.com/?id=224587

Page 84: SQL Performance Tuning 25th Jan

Performance Monitoring

Books Online - Distribution Statistics :

All indexes have distribution statistics that describe the selectivity and distribution of the key values in the index.

The distribution statistics are used to estimate how efficient an index would be in retrieving data associated with a key value or range specified in the query.

Distribution statistics may also be maintained for unindexed columns. These can be defined manually using the CREATE STATISTICS statement or created automatically by the query optimizer.

Distribution Statistics

CautionSELECT statements may, from time to time, cause the automatic update of statistics, depending on how much a table has changed since the statistics were last updated.This will have the effect of making the statement take a lot longer to return for no readily apparent reason. Whilst the automatic update of statistics can be disabled this shouldn’t be necessary except in the most extreme situations. Were this deemed necessary statistics would need to be updated as part of a scheduled process.

Page 85: SQL Performance Tuning 25th Jan

Demonstration

Page 86: SQL Performance Tuning 25th Jan

Conclusions• SQL Server 2000 is a high performance

enterprise class database.• Performance Monitoring is a tool that should

be used throughout the application life cycle– Identify bottlenecks in

• application

• database

• resource utilization

– Exploit all resources• CPU

• I/O

• Network

• Data (concurrency vs. consistency)

Page 87: SQL Performance Tuning 25th Jan

References• “Inside SQL Server 2000” by Kalen

Delaney• “SQL Server 2000 Performance Tuning”

by Whalen, Garcia, DeLuca, Thompson• “SQL Server 2000 Recompilation” at

http://msdn.microsoft.com• “SQL Server 2000 Performance Tuning

with Waits & Queues” SQL Magazine (January 2004)

Page 88: SQL Performance Tuning 25th Jan

For More Information• “Inside SQL Server 2000”, Microsoft Press, 2000., or:

http://mspress.microsoft.com/books/4297.htm• “Microsoft® SQL Server™ 2000 Resource Kit”,

http://www.mspress.microsoft.com/PROD/BOOKS/4939.HTM• “Microsoft® SQL Server™ 2000 Reference Library “,

http://mspress.microsoft.com/prod/books/5001.htm• “Microsoft® SQL Server™ 2000 Administrator's Companion”,

http://mspress.microsoft.com/books/4519.htm• “Microsoft® BackOffice® 4.5 Resource Kit ”, Microsoft Press, 1999, or:

http://mspress.microsoft.com/prod/books/2483.htm

• Refer to the TechNet website at www.microsoft.com/technet/• IT Professionals User Groups in your area

– www.microsoft.com/technet/usergroup/default.asp

Page 89: SQL Performance Tuning 25th Jan

• Administering a Microsoft SQL Server 2000 Database– Course # 2072—Five days—Instructor-led

– Available: through MS CTECs in your area

• Querying Microsoft SQL Server 2000 with Transact-SQL– Course # 2071—Two days—Instructor-led or eLearning

– Available: through MS CTECs in your area

TrainingTraining Resources for ITProfessionals

Page 90: SQL Performance Tuning 25th Jan

Where Can I Get TechNet?

• Visit TechNet Online atwww.microsoft.com/technet

• Register for the TechNet Flash www.microsoft.com/technet/register/flash.asp

• Join the TechNet Online forum at www.microsoft.com/technet/discuss

• Become an TechNet Subscriber at technetbuynow.one.microsoft.com

• Attend More TechNet Events

Page 91: SQL Performance Tuning 25th Jan

Become A Microsoft Certified Systems Engineer• What Is MCSE?

– Premier certification for professionals who analyze the business requirements and design and implement the infrastructure for business solutions based on the Microsoft server software.

• How do I become a Windows 2000 MCSE?– Pass 4 Core Exams– Pass 1 Design Exam– Pass 2 Electives from a comprehensive list

• Where Do I Get More Information?– For more information about certification requirements,

exams, and training options, visit www.microsoft.com/mcp

Page 92: SQL Performance Tuning 25th Jan