Cardinality Estimation and Row Estimates

How SQL Server estimates rows and how estimate errors affect plan choices.

60 associated posts57 primary posts

[Video] SQL Query Optimization: Why Is It So Hard to Get Right?

The first paper on cost-based query optimization was published in 1979 by Patricia Selinger from IBM Research. This paper laid the basic framework for optimizing relational queries that is still in place today. While there have been many technical enhancements since 1979, query optimizers still fail to pick the best plan when faced with a complex SQL query.

Read more about [Video] SQL Query Optimization: Why Is It So Hard to Get Right? Be the first to comment
Performance Tuning

Using Trace Flag 2453 to Improve Table Variable Performance

I recently saw a server with trace flag 2453 configured. I hadn't come across this trace flag before, so I did a little research. Microsoft says it allows "a table variable to trigger recompile when enough number of rows are changed". This can lead to a more efficient execution plan. Trace flag 2453 is available in SP2 or greater for SQL Server 2012, CU3 or greater for SQL Server 2014 and RTM or greater for SQL Server 2016.

Read more about Using Trace Flag 2453 to Improve Table Variable Performance 18 comments — Join the discussion
Performance Tuning

Can Adding an Index Make SQL Server 2016…Worse?

Using the StackOverflow database, let's check out Krock's query. He's a competitive fella, and he's looking to find users who signed up for StackOverflow after he did, but who have a higher reputation than he does. I'm going to simplify the query a little here: [crayon-6a6ece6a2d819778571699/] The Users table has a clustered index on the Id…

Read more about Can Adding an Index Make SQL Server 2016…Worse? 9 comments — Join the discussion

Stats Week: Statistics Terminology Cheatsheet

These things used to confuse me so much
Despite having worked at a Market Research company for a while, I know nothing about statistics, other than that project managers have all sorts of disagreeably subjective phrases for describing them. Vast majority, convincing plurality, dwindling minority, et al. Less talky, more picture.

When I started getting into SQL Server, and learning about statistics, I heard the same phrases over and over again, but wasn't exactly sure what they meant.

Read more about Stats Week: Statistics Terminology Cheatsheet 7 comments — Join the discussion
Performance Tuning

New Cardinality Estimator, New Missing Index Requests

During some testing with SQL Server 2014's new cardinality estimator, I noticed something fun: the new CE can give you different index recommendations than the old one. I'm using the public Stack Overflow database export, and I'm running this Jon Skeet comparison query from Data.StackExchange.com. (Note that it has something a little tricky at the…

Read more about New Cardinality Estimator, New Missing Index Requests 3 comments — Join the discussion

Careful Testing the 2014 Cardinality Estimator with Trace Flags

When you migrate to SQL Server 2014, rather than turning on the new cardinality estimator right away, I recommend sticking with the old cardinality estimator initially by leaving your database in SQL Server 2012 compatibility mode. Let the database settle down for a few weeks, let folks air out their grievances, and give yourself some time to make sure the system is working at least as well as the old system.

Read more about Careful Testing the 2014 Cardinality Estimator with Trace Flags 34 comments — Join the discussion

Curious About the SQL Server 2014 Cardinality Estimator?

If you've been following the blogosphere, or heading to SQL Saturdays, you'll know that many folks are excited about the brand new SQL Server cardinality estimator (CE for short). As Kendra showed in her article The SQL 2014 Cardinality Estimator Eats Bad TSQL for Breakfast, the new CE provides dramatically improved query plans. What if…

Read more about Curious About the SQL Server 2014 Cardinality Estimator? 6 comments — Join the discussion