Kris Hokanson That’s bad mmmkay! …Or, how to know when your SQL Server isn’t working as well as it could
Who is Kris Hokanson? Owner and SQL guru at Variable SQL Solutions SQL Server admin since 1998 and SQL 7 Projects range from general database administration and database performance improvement to BI process design using SSIS & SSRS Email: Kris@variablesql.com 2 | Random Acts of Senseless Databasing
Why are we here? Surprising number of people just don’t know Random Acts of Senseless Databasing Why are we here? Surprising number of people just don’t know Set expectations from your server Is something wrong? How is it wrong? Real-world scenarios This presentation was born out of frustration of going into a new environment and nobody else noticing that servers or processes were running poorly. Know how your server should be performing on a regular basis so that when it’s not performing that way, it’s easy to identify. Don’t trust users to tell you when things aren’t running properly. 3 |
Setting Expectations Performance monitoring tools Random Acts of Senseless Databasing Setting Expectations Performance monitoring tools Perfmon, Idera DM, Red-Gat SQL Monitor, Quest Foglight, etc Require proper setup Establish server baseline (know your servers) Helpful when troubleshooting or identifying when something’s wrong Active monitoring of a server is imperative for proactive management and thorough troubleshooting. I prefer free solutions such as Perfmon for starters but as your server count grows it becomes more important to have a dedicated monitoring tool for better alerting and analysis. I hold onto perfmon data as long as possible since it’s useful for identifying trends and histories. Can be easily imported into SQL Server for summarization and the original files compress very well for archival purposes.
Setting Expectations Important Metrics Memory – Available MBytes Physical Disk Avg. Disk sec/Read/Write Disk Reads/Writes/sec Processor – % Processor Time SQLServer: General Statistics – User Connections SQLServer: SQL Statistics – Batch Requests/sec SQLServer: SQL Statistics – Compilations/sec SQL Server:Buffer Manager - Page life expectancy
Setting Expectations Perfmon demo
Setting Expectations Further Reading Brent Ozar - SQL Server Perfmon (Performance Monitor) Best Practices https://www.brentozar.com/archive/2006/12/dba-101-using-perfmon-for-sql-performance-tuning/ Jonathan Kehayias - Essential Perfmon Counters https://www.sqlskills.com/blogs/jonathan/the-accidental-dba-day-21-of-30-essential-perfmon-counters/
Is Something Wrong? Does it take longer than usual? Does a process take hours/minutes instead of minutes/seconds? What do the DMVs say? What does Brent Ozar say? If you regularly connect to a server with SSMS or log in through RDP and it typically takes a couple of seconds to open SSMS but suddenly it’s taking 20, maybe the server is working harder than it needs to. If you’re running a report or process that’s only processing a few hundred thousand rows then there’s typically no reason it should take several minutes. SQL Server can handle millions of rows with relative ease (read a few seconds) if queries are written properly. Brent Ozar Unlimited’s sp_blitz scripts are better than anything I’ve got but they all use the magic DMVs to get their results. Random Acts of Senseless Databasing
Is Something Wrong Important DMVs sys.dm_exec_sessions sys.dm_exec_requests sys.dm_exec_connections sys.dm_exec_sql_text sys.dm_exec_query_stats sys.dm_exec_cached_plans sys.dm_os_wait_stats
Is Something Wrong Further Reading Gregory A. Larsen - Finding the Worst Performing T-SQL Statements on an Instance https://www.databasejournal.com/features/mssql/article.php/3802936/Finding-the-Worst-Performing-T-SQL-Statements-on-an-Instance.htm Paul Randal - Wait statistics, or please tell me where it hurts https://www.sqlskills.com/blogs/paul/wait-statistics-or-please-tell-me-where-it-hurts/
How Is It Wrong? Extended Events Execution Plans Bigger picture Hundreds of different events, only a few are important Execution Plans Detailed explanation of a query Shows where all the work is done Useful to include Set Statistics IO & Time
How Is It Wrong – Extended Events Different events include different fields (actions) Extended Event viewer in SSMS is Read resulting XML files into SQL tables for analysis using t-sql Can easily aggregate reads/writes/durations Some of the more important ones are sp_statement_completed, sql_statement_completed, sql_batch_completed Important fields are client name, client_hostname, database_id, database_name, nt_username. Fields that are automatically included and are most useful for performance analysis include duration, logical_reads, physical_reads, writes
Extended event setup demo How Is It Wrong – Extended Events Extended event setup demo
How Is It Wrong – Execution Plans Identify discrete operations in a query Provide detailed breakdown at each step Graphically represent data flow through the query Rich with clues for improvement
How is it Wrong – Execution Plans
How Is It Wrong Further Reading Jeremiah Peschka - Collecting Detailed Performance Measurements with Extended Events https://www.brentozar.com/archive/2014/04/collecting-detailed-performance-measurements-extended-events/ Tim Ford - SQL Server Query Execution Plans in SQL Server Management Studio https://www.mssqltips.com/sqlservertip/1856/sql-server-query-execution-plans-in-sql-server-management-studio/ Tim Cullen - Getting IO and time statistics for SQL Server queries https://www.mssqltips.com/sqlservertip/1255/getting-io-and-time-statistics-for-sql-server-queries/
Real-World Example Nested Views
Real-World Example Cursors
Questions/comments/snide remarks
Thank you! Kris Hokanson Kris@variablesql.com