SQL Advanced Monitoring Using DMV, Extended Events and Service Broker Javier Villegas – DBA | MCP | MCTS
Speaker Bio 2 Javier Villegas Buenos Aires, Argentina. Database Administrator Since 1997 – SQL 6.0 -> SQL 2016 MCP - MCTS DBA at Mediterranean Shipping Company Since 2006 PASS Member Since 2008 Picture
Agenda 3 Analyzing SQL activity Continuous monitoring with Extended Events (XEvents) Proactive monitoring(DMV / Powershell) Auditing using Service Broker
Analyzing SQL Server Activity 4 dbo.SP_DBA_CurrentlyExec Allow us to quickly see all the SQL Activity Each row represents an active sessions We can see below: SPID, CPU usage, Status, Start Time, Elapsed time, Read/Write times, SQL Statement, Database name and it’s transaction log usage, Object Name, Wait Stat, Login, Host and Application name If detects a blocking situation it will show a second result set with all the sessions involved Useful to quickly detect performance problems
5 dbo.SP_DBA_CurrentlyExec Example Blocks 5
6 dbo.SP_DBA_CurrentlyExec dbo.SP_DBA_CurrentlyExec dbo.SP_DBA_CurrentlyExec 6
7 dbo.SP_WhoIsActive 7
8 sp_server_diagnostics Captures diagnostic information and health data from SQL to detect possible system failures. This process is running in background constantly and periodically updates the results for the output 8
9 sp_server_diagnostics Create_Time Timestamp Component_Type Indicates whether the row contains information for the SQL Server instance level component or for an AlwaysOn availability group Component_Name system, resource, query_processing, io_subsystem, events, State 0, 1, 2 or 3 State_Desc “Unknown”, “Clean”, “Warning”, “Error” Data XML 9
10 sp_server_diagnostics System CPU, Dumps, Pages, etc. Resource Memory Query_processing Tasks, Wait Stats, bloques, etc. IO_subsystem I/O info Events Server events Availability Group 10
11 sp_server_diagnostics 11 Examples
12 Analyzing SQL Server Activity Other Tools Performance Monitor Activity Monitor SQL Server Performance Dashboard SQL Profiler * Extended Events 12
13 Continuous monitoring with Extended Events Extended Events General events control system Correlates database and operating system events Follow-up tool(Performance Monitor / SQL Trace) Configurable with T-SQL Useful to troubleshoot without performance impact (minimal extra load) Collects data from: I/O Waits Query Parameters Execution Plans Lock 13
14 Continuous monitoring with Extended Events Extended Events - Components Session Definition of what is going to collected as well as when to start and where to store the collected data Target Memory (Ring Buffer) File Package Logical containers SQLOS, SQLSERVER, SecAudit, SQLCLR, etc. Events Data collection 14
15 Continuous monitoring with Extended Events Extended Events We can collect same events as with SQL Profiler (Even more since MS keep updating them) Lighter than SQL Profiler. Easy to work with (T-SQL, XML) 15
16 DEMO Analyzing SQL Server Activity SP_DBA_CurrentlyExec SP_Server_Diagnostics Extended Events SQL Errors Deadlocks Long running SQL Statements 16
17 Proactive monitoring(DMV / Powershell) Continuously collecting key data and vital signs from the SQL Instance SQL Server and Windows Version and Edition MAXDOP Cluster Database Mirroring/AlwaysOn Network Active Transactions Transactions per second Active connections Performance Monitor counters related to I/O using PowerShell I/O Latency 17
18 Proactive monitoring(DMV / Powershell) Also… SQL Job running every minute that perform below tasks Runs “currently executing”. Storing the results just if detects blocks or if there is an statement running for over X minutes Sends with the blocking reports The table used to keep data from “Currently Executing” is very useful when we have to investigate performance issues from the past Check transaction log usage for all the databases (Sends s with reports) Disks Usage Used vs Free Space (Sends when there is less than 1 GB or 7% from total – This can be configurable) Detects open transactions for more than X minutes Detects SQL Dumps Detects critical errors from SQL Error Log Disks Usage and performance info using PerfMon Disks latencies reports 18
19 Auditing We can create a system using Service Broker to help us to audit objects manipulation within an user database Table with rows identifying every CREATE, DROP or ALTER Each row contains below: Event Timestamp Event type CREATE_PROCEDURE, DROP_TABLE, etc. Login used for the event Schema and object name Object type Session ID for the event HostName (*) XML with the details 19
20 Proactive monitoring(DMV / Powershell) 20
21 Proactive monitoring(DMV / Powershell) 21
22 Proactive monitoring(DMV / Powershell) 22
23 Proactive monitoring(DMV / Powershell) 23
24 Proactive monitoring(DMV / Powershell) 24
25 DEMO Proactive monitoring Audit 25