Additional Use for sp_statement_completed

SQL Server
Another discussion in Gothenburg (such a great group of people) around Extended Events lead to a (admittedly, tiny) idea for an additional use for sp_statement_completed. The basics for sp_statement_completed are pretty straight forward. If you want to capture a procedure's behavior, you use rpc_completed. If you want to know about the individual statements within the procedure, you use sp_statement_completed. Now, I don't recommend turning this on across the board or without some good filtering in place because, it's likely to generate quite a bit of data. However, it can be useful, including in the following manner. What Path Did I Take? I've got a silly, example, procedure here: CREATE PROC dbo.PathTest ( @Path1 INT, @Path2 INT, @Path3 INT ) AS IF @Path1 = 1 PRINT 'Path 1'; ELSE IF @Path2…
Read More

Can You See Who Forced a Plan

SQL Server
I had an excellent group of people in Gothenburg Sweden when I taught there and I was asked: Can You See Who Forced a Plan? I didn't know the answer for certain, so I said what I always say: I don't know, but I'll see if I can find out. Query Store System Views One of the first places I'd look to see who forced a plan is the system views in Query Store. No, I don't think it'll be there, but it's worth a look. The obvious place it could be is sys.query_store_plan. After all, that's where a plan will be marked as is_forced. But you look through that and there's nothing about who forced a plan. And looking through the other views, there's nothing showing that. So, this…
Read More

Missing Columns in Extended Events Live Data Explorer

SQL Server
Let me be extremely clear up front, this is not my original work. I saw this post on DBA.StackExchange.com and I wanted to share and promote it. Nice work FevziKartal. The rest of this post is just me replicating work already done by others. I just want to see it in action. Columns in Live Data Explorer Back when I wrote the 2017 version of my query performance tuning book (no link, a) get the 2022 version for reasons I'm about to explain, b) this post is just about testing FevziKartal's work, not self-promotion), I was on board with #TeamXE. Jonathan Kehayias had taken me under his wing and explained the virtues of Extended Events and I was sold. I was also grossly ignorant. I thought that the way you…
Read More

What Are the Most Common Blockers to Adopting SQL Server Extended Events?

SQL Server
Yeah, stupid long title. It's a question I put to several different AI engines. I'm curious what the aggregated knowledge of the internet has to say on the topic of blockers to Extended Events adoption. I'll leave it to you to do the search on your favorite engine and get the wordy, long-winded, answers. However, I want to bring up some of the points that were raised in order to talk about them. First, we'll talk about some of the odd answers. Then, we'll talk about the consensus. Also, one engine gave me a blatantly poor example Session, so I want to point that out. Note: I'm not dinging on AIs. I'm growing to like the little monsters. We're going to address valid points that they bring up. However, I…
Read More

Observing Extended Events

SQL Server, You Can't Do That In Profiler
Sure, you can right click on a running session for Extended Events and open the Live Data window, but are there other ways of observing what Extended Events is capturing? Yes. Extended Events Output Let me just get out of the way immediately, there's probably a way to do this programmatically with DBATools. I need to dig in there, and I'll post what I find. But what can we do right now? SQL Server Management Studio (SSMS). Here are all the sessions currently on my test/demo machine: Some of these are built in, system_health for example. Most of the rest are mine. So, how can we see what they do? Well, notice that every one has a little plus sign next to it: If we click on the plus sign,…
Read More

Only Capture Extended Events For a Given Time

SQL Server
It's a great question. Let's say you want to capture stored procedure completions. But, you only want to capture them between 3AM and 4AM. Can you do it? Output of rpc_completed Let's create a really simple event: CREATE EVENT SESSION [RPCTimeBoxed] ON SERVER ADD EVENT sqlserver.rpc_completed(SET collect_statement=(1) WHERE ([sqlserver].[database_name]=N'AdventureWorks')); If we start this event, run some code, the output within the Data Explorer window looks like this: Just a couple of points here. Notice the fields in the event. None of them are dates or times. However, up above, we get the timestamp column. Done, right? Let's use that. But first, what does AI, through CoPilot tell me? CoPilot To The Rescue? I asked CoPilot. It took a couple of refinements to get it on board with the idea that…
Read More

Data Technology Learning Resources

AWS, Azure, Database Fundamentals, PostgreSQL, SQL Server
Hey all! Quick one here. Tracy Boggiano, awesome person that she is, has put together an interesting collection of data over on GitHub. It's titled DBA Resources, but it goes beyond DBAs. It's much more about the different data platforms on display. And, it's in Github. Create a pull request and you can add to it. Get your own blog on there, whatever. Anyhoo, worth a look. I'm going to do some contributions. Oh, AND, it's multi-platform, cause, isn't everything these days.
Read More

Exploring Window Functions Execution Plans

SQL Server
There are quite a few different ways that you're likely to see window functions evidence themselves within your execution plan. Let's take a look at one example. Window Functions For our example, I've got a pretty simple query: SELECT soh.CustomerID, soh.SubTotal, ROW_NUMBER() OVER (PARTITION BY soh.CustomerID ORDER BY soh.OrderDate ASC) AS RowNum, Soh.OrderDate FROM Sales.SalesOrderHeader AS soh WHERE soh.OrderDate BETWEEN '1/1/2013' AND '7/1/2013' ORDER BY RowNum DESC, soh.OrderDate; Nothing to it really. What kind of execution plan does this generate? Here's the plan with runtime metrics (aka, an actual plan): I showed the missing index suggestion (and let's remember, they're just suggestions) just for completion and to show I'm not hiding anything. Potentially, adding an index could speed up the query. However, that doesn't affect what we're going to talk…
Read More

Query Store Reports Time Intervals

SQL Server
A great question came up over at DBA.StackExchange regarding the query store reports time intervals: How can SQL Server's Query Store Reports show data for minute-length intervals, when "Statistics Collection Interval" is set to 1 hour? I was intrigued because it's not something I had thought about at all. How Does the Report Decided on Time? The first thing you need to know is that all performance information inside Query Store is aggregated. By default, the aggregation interval is 60 minutes. You can adjust that up or down (although, I wouldn't recommend making it too granular, you'll see a massive increase in storage size). It's aggregated because trying to capture every execution of every query, as anyone who has done it using Extended Events knows, is expensive and has a…
Read More

T-SQL Tuesday #166: Wrap-up

SQL Server
Once more, my apologies for being late on getting the T-SQL Tuesday announcement out. I have no excuse. However, our extended event on Extended Events (yes, I'm the third person to make this joke, yes, I'm blatantly stealing) still has several entries, so let's talk about them. Let's get mine out of the way. I was simply curious what the search engines revealed when I asked a pretty common question: how do you identify slow queries? What I found was, the answers on most search engines to this question are old, very old. Not to say wrong, but since many of them were created before a working version of Extended Events (let alone Query Store) was released, how could they tell you. On to actually good posts. One of my…
Read More