Does the auto_stats Extended Event misreport the sample percentage from temporal tables or columnstore?
2
votes
1
answer
141
views
Start up the auto_stats Extended Event, filter out some noise, and prepare yourself to monitor it.
CREATE EVENT SESSION [AutoStatsTest] ON SERVER
ADD EVENT sqlserver.auto_stats(
WHERE ([duration]>1 AND [object_id] > 0) )
ADD TARGET package0.ring_buffer
ALTER EVENT SESSION [AutoStatsTest] ON SERVER
STATE = START
GO
Find a temporal table. I had a copy of the StackOverflow2010 database to hand, so I just made Votes
system versioned.
ALTER TABLE Votes ADD
ValidFrom DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN
CONSTRAINT DF_InsurancePolicy_ValidFrom DEFAULT SYSUTCDATETIME(),
ValidTo DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN
CONSTRAINT DF_InsurancePolicy_ValidTo DEFAULT CONVERT(DATETIME2, '9999-12-31 23:59:59.9999999'),
PERIOD FOR SYSTEM_TIME(ValidFrom, ValidTo);
GO
ALTER TABLE Votes
SET (SYSTEM_VERSIONING = ON);
GO
With your temporal table, do something that will trigger an automatic update of stats. This worked for me
SELECT TOP (1000) [BountyAmount]
FROM [StackOverflow2010].[dbo].[Votes]
WHERE [PostId]
100
Given the duration (800 microseconds on my pathetic machine), I knew that this was nonsense. Upon consulting sys.dm_db_stats_properties
, I found that the actual percentage was much less than 100.
/*
Save yourself the pain of writing
the stats query
and just use sp_BlitzIndex from GitHub
*/
EXEC sp_blitzindex @databasename = 'StackOverflow2010', @tablename = 'Votes'
In summary, **it appears that the auto_stats Extended Event reports an incorrect sample_percentage for temporal tables**. Is this a SQL Server bug or is it my misunderstanding? If it is my misunderstanding, then where can I read further? Individual Extended Events, as far as I know, are very lacking in documentation.
I have only seen this happen with temporal tables. I have found it on both SQL Server 2022 on a real box (the table was multi-terabyte, so I screamed when I saw a 100% sample rate for the primary key's statistic) and on my local 2019 test box.
Adding a columnstore index seems to help reproduce this, but I am not 100% sure.
CREATE NONCLUSTERED COLUMNSTORE INDEX IX_ColStore
on dbo.Votes
(
Id,
PostId
)
I am absolutely sure that there is a real bug here. However, my attempts to reproduce it have given inconsistent results. I suspect that you need a TB-scale table.
**Final update:** I give up on explaining this one. The statistics for the table says that 90 times more rows than appeared in the actual execution plan (caught live as it was running in production, scanning the non-clustered columnstore index) were used to update the statistics. The number of rows in the actual execution plan is 10,000 times less than the table's row count and the auto_stats Extended Event says that 100% of the rows in the table were read. It is impossible for all of these to be true at the same time.
I suspect that it might actually be the deleted bitmap that causes all of this.
Asked by J. Mini
(1225 rep)
Mar 7, 2025, 10:24 PM
Last activity: Jul 17, 2025, 05:21 PM
Last activity: Jul 17, 2025, 05:21 PM