THE SQL Server Blog Spot on the Web

Welcome to SQLblog.com - The SQL Server blog spot on the web Sign in | Join | Help
in Search

Adam Machanic

Adam Machanic, Boston-based independent database consultant, writer, and speaker, shares his experiences with programming, performance tuning, and optimizing SQL Server 2000, 2005, and 2008, in conjunction with related technologies such as .NET.

Introducing: Extended Events Code Generator v1.0

SQL Server 2008 Extended Events. It's a high-performance, feature-rich, and extremely useful tracing system. Too bad it's so difficult to figure out how to use it! Or is it?

Earlier this week Jonathan Kehayias announced his SSMS addin for Extended Events, which makes it super-easy to create and manage events, and even view the raw event data. It's all done via a simple UI, so if you don't have time to learn how to do all of that stuff with the Extended Events DML and DMVs, no worries. 

But what if you have your event all set up thanks to Jonathan's addin, and you find that simply viewing the data isn't enough--now you want to query it, and actually do some analysis. Crazy idea, isn't it? Now you're stuck, unless you want to learn how to do that XQuery stuff. Or are you?

Extended Events Code Generator is my solution to this second problem. Here's how it works: You install the Code Generator, which is simply a T-SQL UDF (with an optional SQLCLR component--more on that later). Then you set up your Event Session(s), with or without the help of Jonathan's addin. And when you're ready to query, you ask the Code Generator to write some code for you. It sets you up with a fully-pivoted view of the data, with all of the columns cast to the appropriate T-SQL data types. Now all you have to do is code. And you'll even get the benefit of full Intellisense support, because you're just dealing with columns in a derived table, not XML.

In addition to making querying a breeze, the generated code will also make a lot of Extended Events queries faster. I sat down with Michael Rys--Mr. SQLXML himself--at TechEd, and he was kind enough to spend a few hours helping me tune the generated code to make it as fast as it can possibly be. And while that's relatively fast (much faster, thanks to Michael's help, than my previous attempts), I wanted it to go even faster, so I created a SQLCLR helper function, included in the attached script, that improves performance by a factor of 3-5 on top of what Michael helped me achieve.

Attached to this post you will find the XE Code Generator, and at the bottom of the script--commented out for those of you who are too paranoid to run SQLCLR code--is the optional helper function. Install the Code Generator in a database of your choice (I recommend tempdb), and if you feel like it and will be using asynchronous file targets, install the SQLCLR function too. The Code Generator will automatically generate code to take advantage of the SQLCLR function if it's installed, or will generate native SQLXML code otherwise. The choice is yours.

Set up an Extended Events session or two, capture some data, and go crazy. I think you'll find that it's not only quite powerful but also a lot of fun to work with. So much fun, in fact, that I'm doing a session on the topic at the PASS conference this November, where I'll share many of the interesting use cases I've found for it to date. And of course all of the examples will use code generated by the Code Generator.

Feel free to reply here with questions, comments, or feature requests. And as always, enjoy!


Published Thursday, July 30, 2009 4:16 PM by Adam Machanic

Attachment(s): xe_code_generator_v1_000.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

SqlServerKudos said:

Kudos for a great Sql Server article - Trackback from SqlServerKudos

July 30, 2009 4:51 PM
 

SQL Server 2008 Extended Events | itsouldiers.com/blog said:

July 31, 2009 5:59 AM
 

Adam Machanic said:

When working with XML in SQL Server, you might want to uniquely identify one node against another. But

August 3, 2009 9:19 AM
 

SQLclr or T-SQL? Performance Options « Developing Matt said:

September 15, 2009 3:41 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Adam Machanic

Adam Machanic is a Boston-based independent database consultant, writer, and speaker. He has been involved in dozens of SQL Server implementations for both high-availability OLTP and large-scale data warehouse applications, and has optimized data access layer performance for several data-intensive applications. Adam has written for numerous web sites and magazines, including SQLblog, Simple Talk, Search SQL Server, SQL Server Professional, CoDe, and VSJ. He has also contributed to several books on SQL Server, including "Expert SQL Server 2005 Development" (Apress, 2007) and "Inside SQL Server 2005: Query Tuning and Optimization" (Microsoft Press, 2007). Adam regularly speaks at user groups, community events, and conferences on a variety of SQL Server and .NET-related topics. He is a Microsoft Most Valuable Professional (MVP) for SQL Server, a Microsoft Certified IT Professional (MCITP), and a member of the INETA North American Speakers Bureau.

This Blog

Syndication

News

Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement