SQL Server Performance Monitoring & Optimization
SQL Server Performance Monitoring & Optimization
SQL Server Performance Monitoring & Optimization
Anil Desai
Independent consultant (Austin, TX)
Author of several SQL Server books
Instructor, Implementing and Managing SQL
Server 2005 (Keystone Learning)
Info: http://AnilDesai.net or Anil@AnilDesai.net
I.
II.
III.
IV.
V.
Best Practices:
Optimize for
real-world
workloads
Monitor/review
performance
regularly
Focus on
specific issues
Server Dashboard
Memory Consumption
Activity All Block Transactions
Activity Top Sessions
Performance Batch Execution Statistics
Performance Top Queries by Average CPU
Object Execution Statistics
Examples:
Disk Usage
All Transactions
All Blocking Transactions
Index Usage Statistics
Top Transactions by Age
Schema Changes History
Purpose:
Monitoring and troubleshooting
View server state and performance details
Returns relational result sets
Scopes:
Server level
Database level
Examples:
Database Engine
Sys.DM_DB_File_Space_Usage
Indexes
Sys.DM_DB_Index_Operational_Stats
Sys.DM_DB_Index_Physical_Stats
I/O Related
Sys.DM_IO_Pending_IO_Requests
Sys.DM_IO_Virtual_File_Stats
Data Collected:
System Information (MSINFO)
Windows Event Logs
SQL Server configuration
Purpose / Features:
Examples:
Generate a list of the 100 slowest queries
Monitor all failed logins (Security)
Trace Definitions
Events
Columns
Filters
Groupings:
Event Categories
Event Classes
Events
Examples:
TSQL
Stored Procedures
Performance
Errors and Warnings
Security auditing
StartTime / EndTime
TextData
Duration
Resource Usage (CPU, Reads, Writes)
Information: User, Database, App. Names
Interactive
Good for live monitoring of small sets of data
Trace table
Will automatically create the table
Can set maximum number of rows
Transact-SQL Events
ShowPlan Events
Deadlock Events
Purpose / Goal:
Correlate server performance with database
performance
Process:
Define and start a counter log
Define and start a SQL Profiler trace
Import Performance Data in SQL Profiler
Objects
Counters
Instances
Modes:
System Monitor
Performance Logs and Alerts
Counter Logs
Trace Logs
Alerts
Files
Transact-SQL Files
XML Files
Should represent commonly-used queries
Process:
Processes
Interactive users
SQL Server Management Studio
SQL Profiler
Database Engine Tuning Advisor
Replication
Service Broker
Sys.DM_Exec_Sessions
Sys.DM_Exec_Requests
Sys.SysProcesses
sp_who / sp_who2
Process Information
Current Process ID: @@SPID
Session Options: DBCC USEROPTIONS
Killing Processes
KILL ProcessID [WITH STATUSONLY]
Lock Modes:
Shared, Exclusive, etc.
Lock escalation
Blocking
When transaction(s) must wait for a lock on a
resource
LOCK_TIMEOUT setting (default = wait forever)
Locking Models:
Pessimistic
Optimistic
READ UNCOMMITTED
READ COMMITTED (default)
REPEATABLE READ
SERIALIZABLE
Row-Versioning:
ALLOW_SNAPSHOT_ISOLATION
READ_COMMITTED_SNAPSHOT
Activity Monitor
SQL Profiler
System Monitor:
SQL Server Locks Object
System Views
Sys.DM_Tran_Locks
Sys.DM_Exec_Requests
Deadlocks:
Deadlock victim
Transaction is rolled-back
Process receives a 1205 error
Example:
Deadlock priorities:
SET DEADLOCK_PRIORITY (LOW, NORMAL, HIGH,
integer)
Deadlock resolution:
Lower priority is killed first
If equal priorities, least expensive transaction
becomes the victim
Application or user should attempt to re-run the
transaction