
Chapters
- 00:00:00 – Introduction: The Blob Problem
- 00:05:05 – Temp Tables and Logging Setup
- 00:05:23 – Values Clause Approaches
- 00:06:03 – UNION ALL and XML
- 00:06:48 – OPENJSON
- 00:07:11 – Testing Every Method With a Cursor
- 00:08:43 – Results: Singleton, Values, and UNION ALL
- 00:09:44 – Results: XML
- 00:10:12 – Results: OPENJSON
- 00:10:24 – Zero KB in the Plan Cache
- 00:11:36 – Wrap-Up
Full transcript
Erik Darling here with Darling Data, and in this video, I don’t have a snazzier name for this, so I’m gonna just show you what I have here, and then I’m gonna describe the problem. I was dealing with a stored procedure recently, and that stored procedure received a blobish input, and it would use dynamic SQL, and it would generate values. But the way it was doing it, it was generating, because when you, when you just do a plain insert with the values clause, you’re limited to 1,000 values, and this blob would often receive many, more than that, 10,000, or 100,000, 200,000, and it turned out that was, it was slow to do that, to, like, iterate over the blob and do, like, an, like, an insert every thousand rows. And so, like, you know, using my, my advanced knowledge, which, which you could, you could also have, if you, if you bought my video course, Learn T-SQL with Erik, you can, you can, you can break the 1,000 values clause if you use, like, a CTE or a derived table, and at first I thought, hey, this might be a winner, but it turned out that was slow too, and so I had to resort to other things, which I’ll talk about in the rest of the video. I don’t know why this part of my head looks so choppy and strange. Anyway.
It’s always a hair problem. Get a new hair and makeup team over here. Anyway, I’m going to talk about what I, what I came up with in the video in a minute, because first I have, I have to try to make some money. That, that, that son of a gun with that Amiga repair channel is keeping me down financially. It’s tired of it. Down in the video description, you can, you, you can hire me. You can buy my training at a, at a discount. There are coupon codes. You can support this channel with as few, as little as $4 a month, if you, if you enjoy this content. And of course, you can also ask me office hours questions. That, that bit is free.
And so is liking, subscribing and telling a friend, which is, uh, the, the, the kindest thing you do is so like whenever you hear all those people bullying you into being kind, uh, that this, this is the, the kind thing you can do. Uh, if you and your team, I need to move my big haircut head out of the way here. Uh, if you and your team need help, uh, doing better things with AI, uh, and databases, uh, I’m, I’m here to help. Uh, uh, I’ve been doing a lot of this, uh, you know, pretty much, you know, probably since, since I started building, uh, stuff, uh, leading up to like my monitoring tool and whatever, uh, I’ve been just really doing a lot of work with, with, um, agents and databases. And, uh, I feel like I’ve got a lot of good, good, good insights and things to say about it at this point.
Um, so, uh, if you are a person out in the world and you want to learn how I do stuff with the robots and databases, I’ve got a presale course linked down in the video description, of course. Or if you work for a company that needs, uh, more, more hands-on customized help, uh, I can also do that with a one-day class that I’ve got for you. That can be online or in person, but if it’s in person, you have to pay for me to get there.
Uh, if you, if you need something free, like free, like flowers in the sidewalk or, uh, the, the ducks in the pond, uh, and you can get my SQL Server and Postgres performance monitoring tool, uh, from code.erikdarling.com. It is all the stuff that you would ever want to see performance-wise, uh, in a nice pretty package of charts and graphs and whatnot. And, um, you know, it, uh, it does everything that I wish paid monitoring tools did, but they don’t because they stink.
Speaking of stinking, I’m going to bring my stink to Seattle, Washington, November 9th through 11th for PASS Summit West. Uh, I have a pre-con there, uh, Advanced T-SQL that doesn’t suck. I might stink, but I can teach you how to write SQL that doesn’t suck.
Uh, and I’ve also got a regular session about isolation levels called Read Committed is Cruel by Default, uh, where you can learn all about why you should just use Read Committed snapshot isolation instead of that dreadful old Read Committed locking isolation level. But, uh, I keep forgetting to make a new image here, so we’re just going to keep rolling with Octobers, and this could be November 2. You never know.
Anyway, um, let’s get through some of this boring stuff. I’ve got some temp tables here, and, uh, I’ve got some, some logging infrastructure here, and, uh, basically the deal is we’re going to load a bunch of data into one of these temp tables. And then we’re going to experiment with various ways of, of doing this.
So, uh, like, one is the values clause, right? Uh, and we’re going to see how that goes. Uh, and then, uh, we also have a one where, uh, we, we break the, go away, alert pop-up, uh, one where we break the values clause into 1,000 per statement.
So this one, uh, does it differently, of course, and then, um, uh, where’s the other one down here? Uh, then we do one big values insert, uh, from a derived table, right? Or, like I said, you could do this with a derived table or a CTE, uh, but this all does, this all does that work.
Dynamic SQL, because, God, I love Dynamic SQL. Um, another way that I experimented with, uh, was using UNION ALL. Boy, did that not go well.
Well, mm, did not enjoy that. Uh, another, another one that I messed with because, you know, not everyone is, uh, in tune with the, the newer things in SQL Server was using XML, uh, with just one big literal kabooby and, uh, shredding that with the, the nodes stuff. Um, you may have seen, uh, uh, Aaron Bertrand had some great posts, uh, back before the string split function was a thing that, uh, they, they talked about using XML to split strings.
Uh, this is kind of the opposite, right? We’re, sort of, maybe, maybe a little bit. Anyway, uh, that’s what this one does, the, the nodes stuff in there.
Uh, and then, uh, the, the one that, uh, uh, uh, I was dreading the most was JSON. Man, every time, every time there’s JSON around a database, I, I feel like I have angered God. I feel like I have, I have done something wrong every time I see JSON.
I’m just being punished. It’s like, eh. Uh, so, uh, that will be the final one. And then, uh, after we, after we experiment with these different methods, uh, we will, we will use a cursor, a great cursor, the finest cursor ever written, probably. Uh, and we will, uh, try, you get all our different methods in a row and we will, we will collect some, some data about each method.
And then, uh, what we’ll do is, uh, log that data to a table and, and see how it all compares. All right. And, uh, we’ve got to get rid of plans in between so we don’t pollute things, uh, cause no one likes a polluted plan cache, right?
And at the very end, I will, I will reveal, reveal to you in, in grand form the findings, the key takeaways, as it were, the load bearing cruxes, all right? Anyway, you’re ready? I’m ready.
So we’re, ah, you son of a gun. Why does that always happen to me? I can never, uh, uh, I’m like the least slick presenter in the world. Something dumb always happens. Uh, so this is running.
This should probably, this should take nine or 10 seconds and then, uh, you know, return some stuff. Of course, it’s taking a bit more than nine or 10 seconds. Cause why would anything ever go right the first time?
Anyway, lucky for us. Let’s, let’s zoom in. Cause I sure do love this new SQL Server Management Studio feature where I can just zoom in on results. So, uh, the singleton inserts obviously did the worst.
Those took almost 14 seconds on this run. Uh, doing, uh, one big values clause, uh, was, was three seconds. So that’s remarkably better than the 13 and a half seconds that singleton inserts started with.
Uh, at least I think so anyway. Uh, UNION ALL did slightly better, right? Um, breaking the values into a thousand per statement.
That, that was okay, uh, for the 20,000 rows. But, you know, imagine, uh, we had 200,000 rows. Uh, that took, well, I mean, it’s behind me. So again, the, the non-slick, uh, presenter is showing through again.
Uh, the number is behind me and I’m never good at remembering which way I have to move for you to see it. There we go. Uh, 23 seconds it looks like.
Where’s my finger? Finger, finger. 23 seconds there. So, um, if we had to do that for a lot more rows, we would have been sitting here a lot longer. Uh, XML?
Hmm, surprising. XML did pretty okay, right? Uh, and, uh, you never know. If Mikael Eriksson might come along and see this video and say, uh, Erik Darlingson, you are, you’re doing the, you did XML wrong. Maybe he has some optimization that would make the XML faster.
But, uh, you know, even with the robots, I couldn’t figure out a way to get the XML to be faster. Uh, and, uh, with open, the OPENJSON though, 94 milliseconds. So, like, even if we had 10 times as many rows, uh, that’s, that’s still, like, under a second.
Now, one thing that you might notice is that the, the plan cache KB for OPENJSON and XML is zero. Uh, these plans were too big. SQL Server was like, uh, nah, I’m not, I’m not, I’m not doing this, right?
It was just too much, uh, because they were just giant, absolutely enormous blobs. Uh, I did run this once with query plans turned on. It was not, not a good time.
Uh, it was a, it was a bad time. The opposite of a good time. Um, but even still, uh, you know, you get some pretty big, I mean, that’s a pretty big plan, right? I mean, honestly, if, if I were doing this in production, which is, is, uh, I mean, not ironically where I had to do it, uh, I would probably want to hide my shame for using OPENJSON too, uh, and having a, a, a zero KB plan in the caches.
Well, that’s a pretty good way to do that, isn’t it? Anyway, uh, I thought this was fun. Um, I’m not sure if you did, but it doesn’t matter, because this is about my enjoyment.
Uh, unless you pay me, then, then it can be about your enjoyment too. Maybe we could enjoy ourselves together. I don’t know.
Spend a day at the beach or something. Anyway, thank you for watching. I hope you enjoyed yourselves. I hope you learned something and I will see you next Tuesday for office hours, uh, which very well may be, uh, from Seattle. It depends on how well I get my act together this weekend.
All right. Goodbye.
Going Further
Someone still has to know enough SQL Server to keep the robots in check, and that’s what my training is for.