chapter twenty two

22 Tracing the activity on the instance with dbatools

 

As DBAs, we often need to trace activity on our SQL Server instances. We might do this for on-demand tasks, such as collect requests taking more than x seconds to execute. Or we might have multiple traces collecting data in a proactive way so we can check them when needed. For example, we may need to find a specific event, such as a deadlock event, to be analyzed.

Until SQL Server 2008, the native tool that we had for the job was SQL Server Trace and SQL Server Profiler. Since then we have been able to use Extended Events, and, since SQL Server 2012 SP1, it covers 100% of the SQL Trace events. Extended events are a much lighter way to achieve at least the same as SQL Trace, but they can provide even more detailed results.

Being able to quickly create and/or import an extended event to collect data can be the difference between catching or missing specific events to analyze a problematic situation. The GUI within SSMS, until you get used to it, is not the most user-friendly. Being able to leverage T-SQL scripts and some automation will make our life much easier.

22.1 SQL Trace & SQL Profiler

22.2 Extended Events

22.3 What dbatools has to offer

22.3.1 Which Extended Events do we have currently on the instance?

22.3.2 Converting SQL Server Profiler template to Extended Events

22.3.3 Using Templates

22.3.4 Start & Stop collecting data

22.3.5 Reading the data?

22.3.6 Replicate XE Session to multiple instances

22.3.7 Clean Up

22.4 Hands-on Lab