1
SQL Server Performance Tuning & Optimization
By Manish Rawat
http://www.linkedin.com/in/manishra
2
Agenda
‣SQL Server Concepts/Structure
‣Performance Measuring & Troubleshooting Tools
‣Locking
‣Performance Problem : CPU
‣Performance Problem : Memory
‣Performance Problem : I/O
‣Performance Problem : Blocking
‣Query Tuning
‣Indexing
3
Performance Focus
‣Response Time
Time interval when a request is submitted and when 1st character of response is received.
‣Throughput
Number of transaction processed in fixed unit of time
‣Scalability
Throughput and response time changes as we add more hardware resources
4
Data - Physical & Logical
‣mdf (data) and ndf (transaction) file
‣Pages (8KB)Fundamental unit of data storage Row size (8096 bytes) : SQL2000Row size can be > 8096 :SQL2008
with varchar(max) data type
The disk space in database is logically divided into pages numbered contiguously from 0 to n..
5
What happens when you CRUD
‣Two places get affectedMemory: Data looked into Memory. If doesn’t exist, Page
brought into Memory(RAM). Operation performed on Page in memory
Transaction Log: Entry in transaction log file (.ndf file)
‣No “instant” changes to mdf file (data file)
Latest Changes are in Memory and Transaction Log file
Backup transaction Log file in case of SQL failure
‣Checkpoint : Lazywriter write to data changes in memory to disk
6
Performance Tools: No Extra cost
‣SQL Server Profiler
‣System Monitor (windows performance monitor)
‣Dynamic Management Views (DMV) : SQL 2005+
‣Database tuning advisor (DTA)
‣Microsoft Free tools (SQLDiag, PSSDiag, SQL Nexus – www.codeplex.com)
‣Performance Studio (SQL 2008)
7
SQL Profiler‣Graphical user interface to SQL Trace for
monitoringEvents to CaptureFilters to capture only certain type of info Information captured can be input to DTA
‣Watch for following ColumnsReads (represent I/O)Duration (time in millisecond)CPU (CPU consumed)
‣Great Integration with Perfmon
8
Transaction Isolation Levels
Level DefinitionRead Un-Committed Don’t need a lock to read a data
Read Committed “Default” Read only committed data otherwise wait. Request ‘S’ lock to read. No guarantee that read is repeatable
Repeatable read Guarantees that data read in a transaction will not change for it duration. Holds the ‘S’ lock until the duration of transaction.
Serializable Prevent phantoms
In any of above transaction, an ‘X’ lock acquired is held for total duration of transaction.
9
Locks
‣Lock types/modesX (exclusive), S (shared) , U(Update), IX (Intent Exclusive), IS…..
imposed on DB (database), RID (row id), PAG(page), TAB(table)
‣Locks Compatibility (matrix)
‣Lock HintsSelect * from employee with (Nolock)
More - Rowlock, HoldLock, Tablock…….
‣Locks HierarchyDatabase (DB) ->Table (TB) -> Page (PG)-> Row (key)
Lock modes Shared(S) Exclusive (X)
Shared (S) – READ LOCKS OK NO
Exclusive(X) NO NO
10
Performance Problem 1 : CPU
‣Detecting1. Perfmon
‣% processer is 80% & above for 15 – 20 minutes.
‣System : Processer Queue length
2. The SQL process has set of workers threads.
‣Running: worker is currently executing on the CPU
‣Runnable: worker is currently executing waiting for its turn on the the CPU
‣Suspended: worker is waiting on a resource (eg a lock)
11
Performance Problem 1 : CPU
‣Isolating & Troubleshooting1. Inefficient Query Plan:
‣ Top 10 queries that are taking most CPU‣Beware of one time query VS. frequently executed ones
2. Excessive Compilation & Recompilation‣ http://support.microsoft.com/kb/308737‣Schema change : Mix of DDL, DML‣Set statements like ANSI_NULL, ARITHBORT etc in SP‣Update Statistics
‣ Look for‣SQL Server: SQL Statistics Batch Request/sec‣SQL Server: SQL Statistics Compilation/sec‣SQL Server: SQL Statistics Recompilation/sec
12
Performance Problem 2: Memory
‣FactsBy Default, SQL will consume all memory it can
‣Detecting‣Memory: Available Bytes is LOW
‣SQL Server Buffer Manager: Buffer Cache Hit Ratio is < 90%
‣SQL Server Buffer Manager: Page Life Expectancy is LOW
‣SQL Server Buffer Manager: Checkpoint pages/sec is increasing
13
Performance Problem 2 : Memory
‣Isolating & TroubleshootingIdentify how much is required
Add more ‣ on 32 bit boxes, SQL Server process can address 2GB only or
3GB if /3GB option exist in boot.ini. Enable AWE option to 1 so SQL can see up to 64 GB. On 64 bit this is it’s not required.
‣More memory available: Use AWE option (0 or 1): sp_configure
14
Performance Problem 3: I/O‣Detecting
1. PhysicalDisk Object : Avg Disk Queue LengthAverage read and write Queued. It should not be consistently >2 /physical disk
2. PhysicalDisk Object : Avg Disk Sec/Read & Sec/WriteAverage time in read & write. It should not be consistently >20 ms
3. PhysicalDisk Object : Disk Reads/sec & Writes/secRate of read & write.. It should be < 85 % of disk capacity
4. Latch waits (DMV )Physical I/O waits when page is addressed to read/write and not in Buffer pool
15
Performance Problem 3 : I/O
‣Isolating & Troubleshooting
Use DMV to find out Queries with most I/O
‣ Is it missing Index? (Most Common Reason)
‣ Is it Poor query ? (Query tuning in later slides)
‣Disk read/write capacity peaking?/ Network peaking?/Incorrect hardware configuration?
16
Performance Problem 4 : Blocking‣Locking (locks) and Blocking (Resource blocks to
ensure data integrity) are expectedYou can only design to MINIMIZE it
‣Blocking Detection & TroubleshootingIdentity what’s getting Blocked and who is blocking it
(sp_who/sp_who2 other tools)Identity Wait Types by DMVMaster..sysprocesses system tablehttp://support.microsoft.com/kb/271509/en-usIs nolock OK?
17
Query Tuning
‣DetectionProfiler
‣ Look for Queries/Stored Proc with High reads, CPU, & Duration. These are candidates of tuning.
‣ Look for Stored proc that’s Recompiling (it’s an event)
DMV’s ‣ Find Queries with missing indexes
‣ Find tables that are defragmented
‣ Find TempDB database bottlenecks
18
Query Tuning cont.‣Troubleshoot : Query Execution Plan
Operator types‣Seek (Best and preferred)
‣Scan (not preferred)
‣Bookmark lookup (better than scan and mostly with non-clustered index)
Join type‣Nested
‣Merge
‣Hash (Avoid)
Graphical Execution Plan Icons :http://msdn2.microsoft.com/en-us/library/ms175913.aspx
http://www.sql-server-performance.com/articles/per/select_indexes_p1.aspx
19
Index Rules‣Clustered Index
Choose wisely. Only one per table possiblePrimary key by default is clustered. Evaluate default behaviour
‣Non-Clustered Index More than one possible.Foreign keys are always good candidate for non-clustered index
(because of joins)
‣Evaluate ‘Included Columns’ in Indexing. Every NonClustered index contains Clustered Keys
‣Choose Index Fill Factor wisely.
‣Find out tables with large rowcount but no indexing. May be it needs index.
Top Related