DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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.

Grant Fritchey user avatar by
Grant Fritchey
·
Oct. 17, 18 · Tutorial
Like (2)
Save
Tweet
Share
4.73K Views

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.

plan_affecting_convert

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.

Conclusion

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.

Event

Published at DZone with permission of Grant Fritchey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Policy-as-Code? An Introduction to Open Policy Agent
  • Key Considerations When Implementing Virtual Kubernetes Clusters
  • The Importance of Delegation in Management Teams
  • Enabling DB Migrations Using Kubernetes Init

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: