1. Tubelator AI
  2. >
  3. Videos
  4. >
  5. Science & Technology
  6. >
  7. SQL Server Execution Plans: Statistics - Part 2

SQL Server Execution Plans: Statistics - Part 2

Available In Following Subtitles
English
Variant 1
Posted on:
Video by: Bert Wagner
Part 2 of the series on Execution Plans dives into the data that SQL Server uses to generate its query plans: statistics. Subscribe and turn on notifications to never miss a weekly video: https://www.youtube.com/c/DataWithBert?sub_confirmation=1 Check out the full post for example queries and more details: https://bertwagner.com/2019/07/30/execution-plans-statistics/ Follow me on Twitter: https://twitter.com/bertwagner
tubelator logo

Instantly generate YouTube summary, transcript and subtitles!

chrome-icon Install Tubelator On Chrome

Video Summary & Chapters

No chapters for this video generated yet.

Video Transcript

0:00
What's up everyone and thanks for joining me again this week.
0:03
Today we're continuing our series on execution plans and we're going to learn about the metadata
0:08
that's inside SQL Server that the query optimizer uses to generate query plans.
0:13
If you missed last week's episode, be sure to check that out first so you have an understanding
0:17
of what an execution plan is and how you can view one because today we're diving deeper
0:21
into the details.
0:22
So as I mentioned in last week's video, SQL Server uses data about your indexes and your
0:28
columns of data in your database in order to help estimate
0:32
the different costs associated with retrieving data
0:35
in different ways.
0:36
And the primary data that SQL Server uses
0:38
to estimate these costs is what's known as.
0:40
as statistics.
0:41
So statistics are high level information
0:43
about what the data looks like in your indexes and columns.
0:48
SQL Server pre-computes these statistics
0:49
so that at runtime, instead of having to calculate
0:53
how many rows or estimated data size of rows
0:55
for a billion row table,
0:57
which could take a little bit of time,
0:59
it already knows that information up front
1:02
and it can use it in order to generate a query plan
1:04
that will return your data efficiently.
1:06
So by default, SQL Server automatically creates
1:09
and update statistics on your indexes
1:12
whenever a significant amount of data changes.
1:15
You can confirm that this is happening on your database
1:18
by going to the database properties
1:20
and looking at the auto create
1:21
and auto update statistics properties.
1:24
So if those auto statistics options are turned on,
1:27
anytime a significant amount of data
1:28
is either inserted or updated in a table,
1:31
SQL Server will automatically update the statistics on it.
1:35
And the thresholds for automatically updating
1:36
those statistics vary based on which version of SQL Server you're on.
1:41
So in order to see these statistics that I keep talking about, all we need to do is type
1:45
in dbcc show statistics and then the name of our table and the name of the index that
1:49
we want to see statistics for.
1:51
If we go ahead and run that, we'll get the following output.
1:56
And I don't want to turn this video into a deep dive on SQL Server statistics, but the
2:00
things to be aware of are things like rows, right, which indicate the number of rows in
2:03
a table, the numbers of rows sampled.
2:06
So how many rows were sampled to calculate these statistics or maybe it was the whole index of rows that was
2:12
used. Things like all density which measure how many distinct values are in your data and of course the histogram which shows the
2:19
frequencies for up to 200
2:21
ranges of values in your index. And while I'll cover statistics in more detail in another video,
2:26
I want to at least make you aware that this is the information that SQL
2:30
server uses to generate your query plans.
2:32
And so if it's inaccurate or out of date, right,
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome