Skip to content

Conversation

@ReeceGoding
Copy link
Contributor

@ReeceGoding ReeceGoding commented Aug 3, 2025

Closes #3679 .

image

This was disturbingly easy. You will want to check over it very carefully. The only place where I really had to think was the dynamic SQL. I don't have super old versions of SQL Server hanging around, so I have not been able to test any branches other than the one that my 2022 Docker container hits.

The URL that I have given is the best that I could find. I cannot think of anyone who has spoken with the voice of experience about PERSIST_SAMPLE_PERCENT.

I might make an issue for us also not having incremental statistics, but the merge conflict will be a pain unless this is merged first.

Copy link
Member

@BrentOzar BrentOzar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to handle Azure SQL DB and a default sampling rate as shown in the comments for the pull request.

I'm not saying you have to code it @ReeceGoding - I can code this later - but just making a note and moving on during a live stream.

@ReeceGoding
Copy link
Contributor Author

Will do. Thanks @BrentOzar!

I enjoyed the stream. Clicking on the screenshot would have saved you some time!

@ReeceGoding
Copy link
Contributor Author

ReeceGoding commented Aug 27, 2025

Wow. Did not expect to find this edge case. This is valid syntax

CREATE STATISTICS OH_WOW
ON [StackOverflow2010].[dbo].[Posts]
(Score)
WITH SAMPLE 123 ROWS,
PERSIST_SAMPLE_PERCENT = ON;

So we get this...
image

even though you are not allowed to use a decimal for anything like WITH SAMPLE 12.3456 PERCENT. For example, this is not valid syntax

CREATE STATISTICS OH_NO
ON [StackOverflow2010].[dbo].[Posts]
(Score)
WITH SAMPLE 12.34 PERCENT,
PERSIST_SAMPLE_PERCENT = ON;

Maybe I'll have to make @UsualStatisticsSamplingPercent a float.

@ReeceGoding ReeceGoding force-pushed the add-persisted-stats-warning branch from 4fbbc79 to 47a2852 Compare August 27, 2025 22:09
…ch. Also replaced version check with a column-existence check. Both of these were as Brent suggested.

This introduces the complexity of float comparison, since we now care
about the float persisted_sample_percent column and have to compare
with it.

Also added a new check to distinguish the persisted sample rate being
surprising and it being unsurprising.
@ReeceGoding ReeceGoding force-pushed the add-persisted-stats-warning branch from 47a2852 to c689a58 Compare August 27, 2025 22:12
@ReeceGoding
Copy link
Contributor Author

I believe that what I've just pushed does all that @BrentOzar asks. See the commit message for details.

Here's a test script for Brent.

/* On a fresh StackOverflow 2010 copy, there is nothing new here. */
USE [StackOverflow2010];
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 0;
EXEC sp_BlitzIndex @SkipStatistics = 1, @Mode = 0;
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 4;
EXEC sp_BlitzIndex @SkipStatistics = 1, @Mode = 4;
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 4, @UsualStatisticsSamplingPercent  = 20;
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 4, @UsualStatisticsSamplingPercent  = 26.5555555;
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 4, @UsualStatisticsSamplingPercent  = NULL;

/* Now let's make some stats */
CREATE STATISTICS TotallyDefault
ON [StackOverflow2010].[dbo].[Posts]
(Score);

CREATE STATISTICS HundredPercentNotPersisted
ON [StackOverflow2010].[dbo].[Posts]
(Body)
WITH SAMPLE 100 PERCENT;

CREATE STATISTICS FullScanNotPersisted
ON [StackOverflow2010].[dbo].[Posts]
(AnswerCount)
WITH FULLSCAN;

CREATE STATISTICS TwentyFourPercentNotPersisted
ON [StackOverflow2010].[dbo].[Posts]
(OwnerUserId)
WITH SAMPLE 24 PERCENT;

CREATE STATISTICS TwentySixPointFivePercentInRowsNotPersisted
ON [StackOverflow2010].[dbo].[Posts]
(PostTypeId)
WITH SAMPLE 990308 ROWS;

CREATE STATISTICS FullScanPersisted
ON [StackOverflow2010].[dbo].[Posts]
(AcceptedAnswerId)
WITH FULLSCAN, PERSIST_SAMPLE_PERCENT = ON;

CREATE STATISTICS HundredPercentPersisted
ON [StackOverflow2010].[dbo].[Posts]
(ClosedDate)
WITH SAMPLE 100 PERCENT, PERSIST_SAMPLE_PERCENT = ON;

CREATE STATISTICS TwentyFourPercentPersisted
ON [StackOverflow2010].[dbo].[Posts]
(CommentCount)
WITH SAMPLE 24 PERCENT, PERSIST_SAMPLE_PERCENT = ON;

CREATE STATISTICS TwentyFivePointSixPercentInRowsPersisted
ON [StackOverflow2010].[dbo].[Posts]
(Title)
WITH SAMPLE 990308 ROWS, PERSIST_SAMPLE_PERCENT = ON;

/*
    Now try the old calls again
*/
/*
4 rows.
1 Brent's.
2 of mine report that [StackOverflow2010].[dbo].[Posts].[System Or User Statistic].[TwentyFourPercentPersisted].[OwnerUserId]
and [StackOverflow2010].[dbo].[Posts].[System Or User Statistic].[TwentyFivePointSixPercentInRowsPersisted].[Title]
have unexpected sample rates.
This is good, since we set them to stupid values.
The other 1 of mine reports that 2 statistics have 100% persisted but do not say which, but we did one with 100% and another with fullscan so all is as expected.
*/
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 0;
/* As above. */
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 4;
/* Reports nothing from the new stuff. */
EXEC sp_BlitzIndex @SkipStatistics = 1, @Mode = 0;
/* Reports nothing from the new stuff. */
EXEC sp_BlitzIndex @SkipStatistics = 1, @Mode = 4;
/*
5 rows.
1 Brent's.
2 of mine have been seen before.
The 2 others are from [StackOverflow2010].[dbo].[Posts].[System Or User Statistic].[FullScanPersisted].[AcceptedAnswerId]
and [StackOverflow2010].[dbo].[Posts].[System Or User Statistic].[HundredPercentPersisted].[ClosedDate]
*/
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 4, @UsualStatisticsSamplingPercent  = 20;
/*
5 rows.
1 Brent's.
3 of mine have been seen before.
1 is new, reporting that one statistic having a persisted sample rate matching the 26.5556% I asked for has been found.
Those 990308 rows that I asked for must have been a good idea on my machine.
*/
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 4, @UsualStatisticsSamplingPercent  = 26.5555555;
/*
5 rows.
1 Brent's.
All seen before.
*/
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 4, @UsualStatisticsSamplingPercent  = NULL;

/* All three of these are identical to the previous three. */
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 0, @UsualStatisticsSamplingPercent  = 20;
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 0, @UsualStatisticsSamplingPercent  = 26.5555555;
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 0, @UsualStatisticsSamplingPercent  = NULL;

/*
    None of these error. Handling this, if at all, is a decision I'll leave to Brent.
    The 0 case is interesting because sys.dm_db_stats_properties can report persisted_sample_percent as zero
    and we could also entirely legitimately persist a sample percentage so close to zero that I cannot distinguish it from comparisons to zero-the-float being imprecise.
*/
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 0, @UsualStatisticsSamplingPercent  = 600;
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 0, @UsualStatisticsSamplingPercent  = 0;
EXEC sp_BlitzIndex @SkipStatistics = 0, @Mode = 0, @UsualStatisticsSamplingPercent  = -200;
image

Copy link
Member

@BrentOzar BrentOzar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Thanks for turning that around quickly while I'm still aware of what the issues were, hahaha. I'm going to merge this as-is because if there's anything wrong, it's just going to be small stuff and I can tweak stuff in the dev branch, but this looks like it's fine as-is. Thanks Reece!

@BrentOzar BrentOzar merged commit f8300d4 into BrentOzarULTD:dev Aug 28, 2025
0 of 4 checks passed
BrentOzar added a commit that referenced this pull request Aug 28, 2025
Continued work on #3680. Moves 200-priority down to Mode 4, throws error if they pass in an invalid stats sampling rate.
BrentOzar added a commit that referenced this pull request Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sp_BlitzIndex: Warn of persisted statistics if we are checking statistics

2 participants