Query Optimization and Execution Plans

How SQL Server compiles, optimizes, and executes individual queries.

573 associated posts292 primary posts

Production DBA

Office Hours: Short Text Answers Edition

Not all of the questions y'all post at https://pollgab.com/room/brento require long-winded responses.

Brandon: Do you seen a rise in json queries to address impedance mismatch between data/objects? I struggled with EF to produce a query that was neither simple nor complicated against a properly designed db (according to 2 experts). Dropped EF; used json query in stored proc; it was magic.

Read more about Office Hours: Short Text Answers Edition 1 comment — Join the discussion

Should You Use SQL Server 2022’s GREATEST and LEAST?

If you've been following along with this week's posts on DATETRUNC and STRING_SPLIT, you're probably going to think the answer is no, but bear with me. It's Christmas week, right? The news can't all be bad.

GREATEST and LEAST are kinda like MAX and MIN, but instead of taking multiple rows as input, they take multiple columns. For example:
[crayon-6a737da3c4c28029720702/]
Produces 3 and 1. This actually has really useful real-world implications.

Read more about Should You Use SQL Server 2022’s GREATEST and LEAST? 2 comments — Join the discussion
T-SQL & Development

Should You Use SQL Server 2022’s STRING_SPLIT?

SQL Server 2022 improved the STRING_SPLIT function so that it can now return lists that are guaranteed to be in order. However, that's the only thing they improved - there's still a critical performance problem with it. Let's take the Stack Overflow database, Users table, put in an index on Location, and then test a…

Read more about Should You Use SQL Server 2022’s STRING_SPLIT? 2 comments — Join the discussion
T-SQL & Development

Should You Use SQL Server 2022’s DATETRUNC?

SQL Server 2022 introduced a new T-SQL element, DATETRUNC, that truncates parts of dates. For example:
[crayon-6a737da3c62db478044168/]
Truncates everything in that date other than the year, so it returns just 2017-01-01 00:00:

You might ask, "Well, why not just use YEAR()?" That's a good question - there are times when you need a start or end date for a date range, and this could make it easier than trying to construct a full start & end date yourself.

Read more about Should You Use SQL Server 2022’s DATETRUNC? 10 comments — Join the discussion

[Video] Office Hours: Black Friday Edition

My Black Friday sale is in its last days, so most of my time at the moment is spent keeping an eye on the site and answering customer questions. I'm happy to say it's our best year so far, too! Y'all really like the new access-for-life options.

I took a break from the online frenzy to check in on the questions you posted at https://pollgab.com/room/brento and answer the highest-voted ones:

Read more about [Video] Office Hours: Black Friday Edition Be the first to comment

Office Hours: Read This, Watch This Edition

This week, a lot of the questions you posted at https://pollgab.com/room/brento just need pointers to resources at other places. Welcome to the Read This, Watch This series of answers.

Don't Blame Anthony: Our developers prefer writing TSQL queries directly in their .NET code rather than writing sprocs and calling them. What are the pros / cons to this approach?

Read more about Office Hours: Read This, Watch This Edition 1 comment — Join the discussion