SQL Server Troubleshooting and Performance MonitoringAnd throw in some performance tuning for giggles
Intro	Stacy HeinDatabase Guy
Start with the BasicsStart with the tools you knowLogs, Logs, LogsPerformance MonitorSome tools you might not knowTracesProfiler TracesExecution plansToad or other third party applications
Why do performance monitoring?	Goes hand-in-hand with troubleshootingIdentify bottlenecks (CPU, Memory, SQL queries, etc.)Planning (capacity management, end-of-life, etc.)Identify server/instance configuration issuesBase-lining your serversShould start troubleshooting the SQL Server before the client says there is an issue.
The Usual SuspectsMemoryProcessorDiskDatabase instance configurationQueriesMaintenance
Performance CountersRemember that performance counters should not be taken individually.  Get the whole story.Most thresholds are dependent on the system and application.Ignore spikes.  Sustained thresholds are the only ones that count.
Memory	A good place to start generally.64 bit has helped with both memory and processor bottlenecks.More is good – always.
Memory Performance CountersMemory: Available BytesKey counter especially when used with others.Memory: Pages / sec Hard page faults – going to disk to get it.VMM goes to pagefile to retrieve.Above 20 suggests possible memory issue.Memory: Page Faults / Sec Sum of hard an soft page faults
Memory Performance CountersPage File: % Usage Also useful with the other counters.Some rules for the pagefileMove from system diskPut on more than one drive if possibleMake it 2 times the size of the physical RAM in the server
Processor Performance CountersProcessor:% Processor TimeTotal processing time for non-idle thread80-90% means need processorProcessor: %User TimeTotal time used for executing user processes (e.g. SQL)System: Processor QueueOver 2 = badConsider how many cores and divide total by number of cores
Disk Performance CountersPhysical diskPhysicalDisk: Avg. Read Queue Length  Should be less than  2 PhysicalDisk: Avg. Write Queue Length  Should be less than 2 PhysicalDisk: % Disk Time  more than 50% indicates a bottleneck
Disk Performance CountersLogical DiskSQL Server:BufferManager:Page reads/sec SQL Server reading pages from disk. If you are near your hardware maximums for this counter tune your database by adding indexes, redesign queries, etc.SQL Server:BufferManager:Page writes/sec SQL Server writing pages to disk.  If you are near your hardware maximums for this counter tune your database by adding indexes, redesign queries, etc.
Networking Performance CountersNetwork Interface\ Bytes Total/sec Network Interface\ Bytes Sent/sec Network Interface\ Bytes Received/sec Network Interface\ Current Bandwidth5Don’t usually have a networking issue that is not the cable, the NIC, or route/rule issues.
Dynamic Management Views and FunctionsCan get performance counter data from these.  Is a snapshot not continuousCommon ones usedsys.dm_os_performance_counterssys.dm_os_sys_infosys.dm_os_wait_statssys.dm_io_virtual_file_stats - function
Trace FlagsUsed in determining deadlocks (in this case anyway)1204Reports deadlock information formatted by each node involved in the deadlock1222formats deadlock information, first by processes and then by resourcesUse DBCC TRACEON to set
Profiler is your FriendCan execute and capture trace information for all transactions running on SQL server instance.Can isolate transactions you need to capture.
Database Instance ConfigurationCan affect server performanceExamplesMemory settings, processor affinity settings, affinity masks, AWE setting (for 32 bit instances), etc.If you are going to change your settings, understand the instances role
Instance RolesHigh Write InstanceThe database and log disk should be RAID 10 and separated on different physical volumes.The logical and physical model of the database is important.  High write tables should be on their own partitions.  This is also be true for very large, clustered indexes.Tempdb should be on its own RAID 10 partition and should have one data file for each core dedicated to SQL Server.Although disk is usually the primary issue with a high write system, put as much RAM as you can in this system.
Instance RolesHigh Read Instance (Reporting?)Memory, Memory, Memory is the most important component to a read-intensive system.  The more you have the more that can be accessed in memory and not read from disk.Tempdb should be on its own RAID 10 partition and should have one data file for each core dedicated to SQL Server.  If you are doing massive reads, you are probably doing calculations for reporting.  This operations are handled in tempdb now in SQL Server 2005 and forward.Although memory is the key to a system that does massive read operations, if you can afford it, make the database and log file partitions RAID 10 for when you would need to access them.
Instance RolesIf your system is a hybrid the defaults should work for the server.  If not, tweak design and system settings as necessary careful to consider what the instance might be processing (i.e. more reads or more writes).
Other Keys to Troubleshooting	Understand the data and applications on the instanceThe physical and logical design of the database coupled with the system setting will go far in establishing a high performing instance/server. Well designed queries are also
Tuning QueriesExecution PlansProfilerThird party appsToad for SQL Server
Tuning QueriesRules for code creationUse procedures for the repetitive queriesAvoid cursorsReduces the use of temp tables Return only the rows an app needsSet NOCOUNT ONUse the schema when naming the objectDo all DDL at top of proceduresUse ANSI JOIN languageLook for ANSI SET commands in your code
MaintenanceDatabase maintenance is key to mitigating some performance problems.  Backups for obvious reasonsIndex fragmentation can cause issues for the server.Two options DBCC DBREINDEX and DBCC INDEXDEFRAGUPDATE STATISTICS - depends on your application.Be careful there is a performance trade off for recompiling queries. Maintenance is a key component to the preventative side of server support.

Sql server troubleshooting

  • 1.
    SQL Server Troubleshootingand Performance MonitoringAnd throw in some performance tuning for giggles
  • 2.
  • 3.
    Start with theBasicsStart with the tools you knowLogs, Logs, LogsPerformance MonitorSome tools you might not knowTracesProfiler TracesExecution plansToad or other third party applications
  • 4.
    Why do performancemonitoring? Goes hand-in-hand with troubleshootingIdentify bottlenecks (CPU, Memory, SQL queries, etc.)Planning (capacity management, end-of-life, etc.)Identify server/instance configuration issuesBase-lining your serversShould start troubleshooting the SQL Server before the client says there is an issue.
  • 5.
    The Usual SuspectsMemoryProcessorDiskDatabaseinstance configurationQueriesMaintenance
  • 6.
    Performance CountersRemember thatperformance counters should not be taken individually. Get the whole story.Most thresholds are dependent on the system and application.Ignore spikes. Sustained thresholds are the only ones that count.
  • 7.
    Memory A good placeto start generally.64 bit has helped with both memory and processor bottlenecks.More is good – always.
  • 8.
    Memory Performance CountersMemory:Available BytesKey counter especially when used with others.Memory: Pages / sec Hard page faults – going to disk to get it.VMM goes to pagefile to retrieve.Above 20 suggests possible memory issue.Memory: Page Faults / Sec Sum of hard an soft page faults
  • 9.
    Memory Performance CountersPageFile: % Usage Also useful with the other counters.Some rules for the pagefileMove from system diskPut on more than one drive if possibleMake it 2 times the size of the physical RAM in the server
  • 10.
    Processor Performance CountersProcessor:%Processor TimeTotal processing time for non-idle thread80-90% means need processorProcessor: %User TimeTotal time used for executing user processes (e.g. SQL)System: Processor QueueOver 2 = badConsider how many cores and divide total by number of cores
  • 11.
    Disk Performance CountersPhysicaldiskPhysicalDisk: Avg. Read Queue Length  Should be less than  2 PhysicalDisk: Avg. Write Queue Length  Should be less than 2 PhysicalDisk: % Disk Time  more than 50% indicates a bottleneck
  • 12.
    Disk Performance CountersLogicalDiskSQL Server:BufferManager:Page reads/sec SQL Server reading pages from disk. If you are near your hardware maximums for this counter tune your database by adding indexes, redesign queries, etc.SQL Server:BufferManager:Page writes/sec SQL Server writing pages to disk. If you are near your hardware maximums for this counter tune your database by adding indexes, redesign queries, etc.
  • 13.
    Networking Performance CountersNetworkInterface\ Bytes Total/sec Network Interface\ Bytes Sent/sec Network Interface\ Bytes Received/sec Network Interface\ Current Bandwidth5Don’t usually have a networking issue that is not the cable, the NIC, or route/rule issues.
  • 14.
    Dynamic Management Viewsand FunctionsCan get performance counter data from these. Is a snapshot not continuousCommon ones usedsys.dm_os_performance_counterssys.dm_os_sys_infosys.dm_os_wait_statssys.dm_io_virtual_file_stats - function
  • 15.
    Trace FlagsUsed indetermining deadlocks (in this case anyway)1204Reports deadlock information formatted by each node involved in the deadlock1222formats deadlock information, first by processes and then by resourcesUse DBCC TRACEON to set
  • 16.
    Profiler is yourFriendCan execute and capture trace information for all transactions running on SQL server instance.Can isolate transactions you need to capture.
  • 17.
    Database Instance ConfigurationCanaffect server performanceExamplesMemory settings, processor affinity settings, affinity masks, AWE setting (for 32 bit instances), etc.If you are going to change your settings, understand the instances role
  • 18.
    Instance RolesHigh WriteInstanceThe database and log disk should be RAID 10 and separated on different physical volumes.The logical and physical model of the database is important. High write tables should be on their own partitions. This is also be true for very large, clustered indexes.Tempdb should be on its own RAID 10 partition and should have one data file for each core dedicated to SQL Server.Although disk is usually the primary issue with a high write system, put as much RAM as you can in this system.
  • 19.
    Instance RolesHigh ReadInstance (Reporting?)Memory, Memory, Memory is the most important component to a read-intensive system. The more you have the more that can be accessed in memory and not read from disk.Tempdb should be on its own RAID 10 partition and should have one data file for each core dedicated to SQL Server. If you are doing massive reads, you are probably doing calculations for reporting. This operations are handled in tempdb now in SQL Server 2005 and forward.Although memory is the key to a system that does massive read operations, if you can afford it, make the database and log file partitions RAID 10 for when you would need to access them.
  • 20.
    Instance RolesIf yoursystem is a hybrid the defaults should work for the server. If not, tweak design and system settings as necessary careful to consider what the instance might be processing (i.e. more reads or more writes).
  • 21.
    Other Keys toTroubleshooting Understand the data and applications on the instanceThe physical and logical design of the database coupled with the system setting will go far in establishing a high performing instance/server. Well designed queries are also
  • 22.
    Tuning QueriesExecution PlansProfilerThirdparty appsToad for SQL Server
  • 23.
    Tuning QueriesRules forcode creationUse procedures for the repetitive queriesAvoid cursorsReduces the use of temp tables Return only the rows an app needsSet NOCOUNT ONUse the schema when naming the objectDo all DDL at top of proceduresUse ANSI JOIN languageLook for ANSI SET commands in your code
  • 24.
    MaintenanceDatabase maintenance iskey to mitigating some performance problems. Backups for obvious reasonsIndex fragmentation can cause issues for the server.Two options DBCC DBREINDEX and DBCC INDEXDEFRAGUPDATE STATISTICS - depends on your application.Be careful there is a performance trade off for recompiling queries. Maintenance is a key component to the preventative side of server support.