A few days ago, a customer asked us how they could capture statistics changes that result in a new execution plan, receive a notification when queries have anti-patterns, or changes in automatic SQL Server optimization. Next, I would like to show you some examples.
The script is used to create an Extended Event session named "CaptureChangesOnQuery" on a database. This session captures several different events related to automatic tuning, plan regression, settings changes, missing column statistics, query anti-patterns, and SQL statement recompilation. Let's go through each event and its details:
This is the definition of Extended Event:
CREATE EVENT SESSION [CaptureChangesOnQuery] ON DATABASE
ADD EVENT qds.automatic_tuning_config_change(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.compile_plan_guid,sqlserver.database_name,sqlserver.plan_handle,sqlserver.query_hash,sqlserver.query_plan_hash,sqlserver.session_id,sqlserver.sql_text,sqlserver.username)),
ADD EVENT qds.automatic_tuning_error(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.compile_plan_guid,sqlserver.database_name,sqlserver.plan_handle,sqlserver.query_hash,sqlserver.query_plan_hash,sqlserver.session_id,sqlserver.sql_text,sqlserver.username)),
ADD EVENT qds.automatic_tuning_plan_regression_detection_check_completed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.compile_plan_guid,sqlserver.database_name,sqlserver.plan_handle,sqlserver.query_hash,sqlserver.query_plan_hash,sqlserver.session_id,sqlserver.sql_text,sqlserver.username)),
ADD EVENT qds.automatic_tuning_plan_regression_verification_check_completed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.compile_plan_guid,sqlserver.database_name,sqlserver.plan_handle,sqlserver.query_hash,sqlserver.query_plan_hash,sqlserver.session_id,sqlserver.sql_text,sqlserver.username)),
ADD EVENT sqlserver.automatic_tuning_settings_changed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.compile_plan_guid,sqlserver.database_name,sqlserver.plan_handle,sqlserver.query_hash,sqlserver.query_plan_hash,sqlserver.session_id,sqlserver.sql_text,sqlserver.username)),
ADD EVENT sqlserver.missing_column_statistics(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.compile_plan_guid,sqlserver.database_name,sqlserver.plan_handle,sqlserver.query_hash,sqlserver.query_plan_hash,sqlserver.session_id,sqlserver.sql_text,sqlserver.username)),
ADD EVENT sqlserver.query_antipattern(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.compile_plan_guid,sqlserver.database_name,sqlserver.plan_handle,sqlserver.query_hash,sqlserver.query_plan_hash,sqlserver.session_id,sqlserver.sql_text,sqlserver.username)),
ADD EVENT sqlserver.sql_statement_recompile(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.compile_plan_guid,sqlserver.database_name,sqlserver.plan_handle,sqlserver.query_hash,sqlserver.query_plan_hash,sqlserver.session_id,sqlserver.sql_text,sqlserver.username))
ADD TARGET package0.ring_buffer
WITH (STARTUP_STATE=OFF)
qds.automatic_tuning_config_change
:
qds.automatic_tuning_error
:
qds.automatic_tuning_plan_regression_detection_check_completed
:
qds.automatic_tuning_plan_regression_verification_check_completed
:
sqlserver.automatic_tuning_settings_changed
:
sqlserver.missing_column_statistics
:
sqlserver.query_antipattern
:
sqlserver.sql_statement_recompile
:
The target for all these events is the package0.ring_buffer
, which is a target type used to store event data in a ring buffer memory area.
Once the event session is running, the specified events will generate data in the ring buffer, which can be queried or analyzed to gain insights into automatic tuning, plan regressions, settings changes, missing column statistics, query anti-patterns, and SQL statement recompilations occurring in the database.
I would like to share an example about it. For example, following the previous articles:
We are going to define the following Extended Event definition:
This information helps us a lot to review any aspect that may cause performance issues in our queries. Finally, once we finished the process, we need to stop the extended event running the following query: ALTER EVENT SESSION [CaptureChangesOnQuery] ON DATABASE STATE = STOP
Enjoy!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.