6

I am wondering how SQL server can generate a query plan with "Auto create statistics" set to "off".

I stumbled on the below context at the below link while searching for an answer(Sorry, I searched, but couldn't find anything better).

https://www.simple-talk.com/sql/performance/execution-plan-basics/

Table variables do not ever have statistics generated on them, so they are always assumed by the optimizer to have a single row, regardless of their actual size

Even though the above is applicable only for Table variables, I thought SQL Server will use the same principle to derive a query plan for a table and decided to do a small test.

I cleared the PROC cache and Ran the below query on a table with 1 million records

enter image description here

enter image description here

My test proved me wrong and SQL server reported missing statistics(as expected), But came up with the estimated number of rows as 31622(I expected it to be 1) when there are no statistics. How did the query optimizer come up with this value without statistics?

Joe Obbish
33.1k4 gold badges76 silver badges155 bronze badges
asked Jun 13, 2017 at 7:54

1 Answer 1

10

Table variables is another story, what the server uses when there are no statistics is estimation for unknown input:unknowns

So in your case the estimation will be select power(1000000, 0.75) = 31622 ( = predicate with non unique column prior to 2014)

Related book: T-SQL Querying by Itzik Ben-Gan

answered Jun 13, 2017 at 8:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.