Step by Step Guide To SQL Server Performance Tuning 1738662788
Step by Step Guide To SQL Server Performance Tuning 1738662788
Goal: Detect where the bottleneck lies (CPU, Memory, I/O, etc.).
Tools:
Performance Monitor (PerfMon): Track % Processor Time, Page Life Expectancy, Disk
Read/Write Latency.
SQL Server Activity Monitor: Check "Recent Expensive Queries" and "Resource Waits".
DMVs:
o sys.dm_os_wait_stats (identify wait types like PAGEIOLATCH, CXPACKET).
o sys.dm_exec_requests (find currently running queries).
Real-Life Example:
High CPU usage → Check sys.dm_exec_query_stats for queries with high total_worker_time.
Slow reports → Look for PAGEIOLATCH waits indicating disk I/O bottlenecks.
1. CPU Bottlenecks
Causes:
2
Mohammed Ahmer Mohiuddin
SQL Server DBA | Azure, AWS & GCP Cloud Engineer | PostgreSQL | PowerShell | Cloud Admin | Linux
Expert | 4 x Microsoft Azure | ITIL®
www.linkedin.com/in/mohammed-ahmer-mohiuddin-a22a0432
2. Memory Pressure
Causes:
o Insufficient memory allocated to SQL Server.
o Memory grants for sorts/hashes consuming RAM.
DMVs:
o sys.dm_os_process_memory: Check physical_memory_in_use.
o sys.dm_exec_query_memory_grants: Find queries reserving memory.
Fix:
o Increase max server memory (leave 4-8GB for OS).
o Optimize queries with ORDER BY/GROUP BY.
Causes:
o Table/index scans due to missing indexes.
o Poorly configured TempDB.
DMVs:
o sys.dm_io_virtual_file_stats: Identify high read/write files.
o sys.dm_db_index_usage_stats: Find indexes with high scans.
Fix:
o Add indexes to reduce scans.
o Separate TempDB files (1 file per CPU core).
3
Mohammed Ahmer Mohiuddin
SQL Server DBA | Azure, AWS & GCP Cloud Engineer | PostgreSQL | PowerShell | Cloud Admin | Linux
Expert | 4 x Microsoft Azure | ITIL®
www.linkedin.com/in/mohammed-ahmer-mohiuddin-a22a0432
4. Expensive Queries
Causes:
o Complex joins, nested loops, or implicit conversions.
o Queries using SELECT * or functions in WHERE clauses.
DMVs:
o sys.dm_exec_query_stats: Sort by total_logical_reads.
o sys.dm_exec_sql_text: Fetch query text.
Fix:
o Rewrite queries to use indexed columns.
o Avoid functions in predicates (e.g., WHERE YEAR(Date) = 2023 → WHERE Date >=
'2023-01-01').
5. Missing Indexes
Causes:
o SQL Server suggests indexes in execution plans but they’re not created.
o Frequent scans on large tables.
DMVs:
o sys.dm_db_missing_index_details: Lists missing indexes.
o sys.dm_db_index_usage_stats: Compare seeks vs scans.
Fix:
o Create filtered/non-clustered indexes on frequently queried columns.
o Example: Add index on OrderDate for WHERE OrderDate BETWEEN ....
6. Blocking/Deadlocks
Causes:
o Long-running transactions locking resources.
o Poor isolation levels (e.g., READ UNCOMMITTED).
DMVs:
o sys.dm_tran_locks: Check lock types (e.g., X for exclusive).
o sys.dm_os_waiting_tasks: Find blocked sessions.
4
Mohammed Ahmer Mohiuddin
SQL Server DBA | Azure, AWS & GCP Cloud Engineer | PostgreSQL | PowerShell | Cloud Admin | Linux
Expert | 4 x Microsoft Azure | ITIL®
www.linkedin.com/in/mohammed-ahmer-mohiuddin-a22a0432
Fix:
o Use NOLOCK (carefully!) for read-heavy systems.
o Optimize transactions to commit faster.
7. Outdated Statistics
Causes:
o Auto-update stats disabled or infrequent.
o Queries using stale stats for execution plans.
DMVs:
o sys.dm_db_stats_properties: Check last_updated date.
o sys.dm_exec_query_stats: Look for last_worker_time spikes.
Fix:
o Update stats manually: UPDATE STATISTICS TableName.
o Enable AUTO_UPDATE_STATISTICS.
8. TempDB Contention
Causes:
o Excessive temp table usage or version stores.
o Insufficient TempDB files.
DMVs:
o sys.dm_db_task_space_usage: Track TempDB allocations.
o sys.dm_os_wait_stats: Look for PAGELATCH waits on TempDB.
Fix:
o Add multiple TempDB files (1 per CPU core).
o Use SNAPSHOT ISOLATION to reduce version store pressure.
Prioritize: Start with the biggest bottleneck (e.g., a query affecting 80% of users).
Example Workflow:
1. Find high CPU query → Check its execution plan → Add missing index → Test → Deploy.
5
Mohammed Ahmer Mohiuddin
SQL Server DBA | Azure, AWS & GCP Cloud Engineer | PostgreSQL | PowerShell | Cloud Admin | Linux
Expert | 4 x Microsoft Azure | ITIL®
www.linkedin.com/in/mohammed-ahmer-mohiuddin-a22a0432
Tools:
Example:
Real-World Scenario
Key Takeaways
By following this guide, you’ll turn SQL Server performance issues from "Why is this so slow?"
to "Wow, that was easy!" 💡