Using Extended Events to Capture Implicit Conversions
Let's take a look at using extended events to capture implicit conversions. Also explore an example of how you would do this.
Join the DZone community and get the full member experience.Join For Free
Using the appropriate data type to avoid conversions or implicit conversions is a fundamental approach to good T-SQL coding practices. Implicit conversions can lead to poor performance. However, other than looking at every execution plan in your system, is there a way to see if you are experiencing implicit conversions? Actually, yeah, it's right there in Extended Events.
Built right into the Extended Events is an event that captures conversions that would affect execution plans, plan_affecting_convert. This event will show both CONVERT and CONVERT_IMPLICIT warnings that you would normally only see within an execution plan. You can capture this event with others. Capturing events together along with causality tracking makes it very easy to track queries that have the issue. Here's one example of how you might capture implicit conversions along with the query:
CREATE EVENT SESSION ImplicitConversion ON SERVER ADD EVENT sqlserver.plan_affecting_convert (WHERE (sqlserver.database_name = N'AdventureWorks2017')), ADD EVENT sqlserver.sql_batch_completed (WHERE (sqlserver.database_name = N'AdventureWorks2017')), ADD EVENT sqlserver.sql_batch_starting (WHERE (sqlserver.database_name = N'AdventureWorks2017')) ADD TARGET package0.event_file (SET filename = N'C:\PerfData\ImplicitConversion.xel') WITH (TRACK_CAUSALITY = ON);
This set of events will capture the start of a batch and the completion of a batch as well as the plan_affecting_convert event. Because I set TRACK_CAUSALITY to ON, I can be sure to associate the implicit conversions with a specific query. You could easily modify this to work with statements or remote procedure calls.
Another way is to capture just the plan_affecting_convert event. Then use the Actions (also called Global Fields) to get the query. This session will do the trick:
CREATE EVENT SESSION ImplicitConversionOnly ON SERVER ADD EVENT sqlserver.plan_affecting_convert (ACTION (sqlserver.sql_text) WHERE (sqlserver.equal_i_sql_unicode_string(sqlserver.database_name, N'AdventureWorks2017'))) ADD TARGET package0.event_file (SET filename = N'C:\PerfData\ImplicitConversionOnly.xel');
Actions can be problematic if they are overused. Also, some actions have more overhead than others. However, in this case, it's probably a smaller load than adding causality tracking.
You may still have queries that have CONVERT or CONVERT_IMPLICIT within them that won't be picked up by this event. That's because not all such conversions lead to problems within the plan. You may also see false positives where the optimizer fires the warning for conversions that don't actually hurt performance.
As you can see, getting a handle on which of your queries may be experiencing issues because of implicit conversions is actually really easy with Extended Events. In fact, you can only capture this information with Extended Events. This ability is just one more reason why I so strongly recommend using Extended Events.
Published at DZone with permission of Grant Fritchey, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.