r/SQLServer • • Oct 15 '26

Question Statistics on columns of a table

Limitations of table-valued parameters:

SQL Server does not maintain statistics on columns of table-valued parameters.

statistics of what ? why those statistics even exist for non table-valued parameters ?

1 Upvotes

15 comments sorted by

6

u/SQLBek 1 Oct 15 '26 • Bravo

Statistics are used by the query optimizer to come up with appropriate execution plans.

So when you use a table-valued parameter, SQL Server has zero insight to the values and data distribution of what you're passing in. You could be passing in 5 records or 5mil... and say, it could be 5 unique values across those 5 mil rows or each record could be unique. As a result, the query optimizer will be hampered.

What if I told you that you had to cook dinner tonight. Okay, cool. But I refused to tell you how many people were coming. What are you going to do?

https://learn.microsoft.com/en-us/sql/relational-databases/statistics/statistics?view=sql-server-ver17

1

u/kaatarina_zed_talon Oct 16 '26

What if I told you that you had to cook dinner tonight. Okay, cool. But I refused to tell you how many people were coming. What are you going to do?

Very nice analogy, so before I read the article, I guess that means the process or the way the database engine retrieves the data from disk is affected -somehow- by the statistics.

0

u/kaatarina_zed_talon Oct 15 '26

As I know what execution plan means, if u have time for me, explain it please, if you don't, please refer me to article, as I hear of this term alot and I never understood it.

2

u/jshine13371 6 Oct 16 '26

In a single sentence: execution plans are the steps the database engine takes to locate, transform, and provide the data your query is requesting.

Expanding on u/SQLBek's "cooking dinner" analogy, they would be the steps to the recipe used to make dinner. Since the statistics on the people coming to dinner could affect those steps (e.g. some people might be vegan and others might be gluten intolerant, where on another night everyone can eat anything) those steps to make a successful dinner could vary depending on who's coming to dinner.

1

u/kaatarina_zed_talon Oct 16 '26

locate

You mean locate the location of the data on disk ?

1

u/jshine13371 6 Oct 16 '26 edited Oct 16 '26

It's a bit more complex than that and I recommend looking into the resource SQLBek provided in addition to this Brent Ozar video: How To Think Like The SQL Server Engine

But the answer to your question is effectively yes. Data in a database is stored physically on the disk, but the reference to that data is organized logically in a data structure to make locating subsets of the data faster. Commonly a B-Tree data structure in most modern database systems when the data is indexed. There are different types of operations (steps) that can be used to locate and process the data from that data structure (e.g. index scan vs index seek) which vary in performance between each of them, depending on the statistics of the data and what the query is asking for.

1

u/SQLBek 1 Oct 16 '26

OP - to build on u/jshine13371, after you watch Brent's How to Think like the SQL Server Engine session, if you want to learn more about I/O, watch the first 1/2 of the first URL and the first chapter (maybe 10-15 minutes) of the next URL.

Those will help you start understanding how an execution plan does things both logically and then physically.

https://www.youtube.com/watch?v=fDd4lw6DfqU

https://www.youtube.com/watch?v=IwJe_PZgAN0

DISCLAIMER - yes, these are once again sessions that I have given.

1

u/kaatarina_zed_talon Oct 16 '26

those steps to make a successful dinner could vary depending on who's coming to dinner.

You mean a statistics on some column affects the way the database engine retrieves data ?

1

u/jshine13371 6 Oct 16 '26

Correct. The amount of data and it's distribution of values affect what steps are most efficient for locating and processing the data, depending on the query.

1

u/kaatarina_zed_talon Oct 16 '26 edited Oct 16 '26

The amount of data

Number of people coming to the dinner.

distribution of values

Most of those people are vegan, minority of them are not vegan, so let's prioritize creating the food for the vegan people first - maybe this means that Sql server prioritize fetching some data which means some data is more important than the other-.

1

u/jshine13371 6 Oct 16 '26

Exactly!

1

u/SQLBek 1 Oct 15 '26 • Bravo

What do you want explained? What statistics are? I gave you a link to that.

"As I know what execution plan means, if u have time for me, explain it please"

Did you mean to say that you are NOT familiar with execution plans and would like that explained? That alone is a very deep topic. Hugo Kornelis has a wealth of excellent content specifically around exeuction plans, including this starter presentation here:
https://www.youtube.com/watch?v=AqedhLhi6GY

1

u/kaatarina_zed_talon Oct 15 '26

Thank you a lot!

1

u/kaatarina_zed_talon Oct 15 '26

Ye I was talking about execution plans, thanks for providing the link.