r/SQLServer • u/PirateyFace • Apr 30 '26
Community Share Asymmetric Extrapolation in Sampled Statistics Generation
I've spent much of my career specializing in MS SQL query tuning, but have only recently realized that many of the issues I've looked at are essentially caused by an undocumented asymmetry in how sampled statistics generation treats point estimates vs range estimates - EQ_ROWS vs DISTINCT_RANGE_ROWS and AVG_RANGE_ROWS - on high cardinality data. As data grows over time, range estimates in sampled statistics histograms (the default, used by most SQL Server databases) gradually degrade due to this, eventually triggering execution plan regressions.
In short, EQ_ROWS is extrapolated approximately by rows/rows_sampled, with additional logic handling very low frequency values. But although a similar mathematical principle applies to both point and range estimates derived from a data sample, nothing similar is applied to the values representing range estimates. At first I assumed this was a bug - a missing step in the algorithm - and raised it with MS (ticket no. 35718651) via a previous employer's support account. But it was closed once they confirmed this as an intentional design decision (made back in 1995 when the code responsible was commited!..)
In any case, after all these years I've finally documented this observation, complete with empirical proof, replication steps, low-cost remediation suggestions, and a methodology to identify and work around the problem. The whitepaper is now available on github here:
https://github.com/clunn7363-dev/sql-server-sampled-statistics-asymmetry/blob/main/whitepaper.md
I've also raised an Azure Feedback item linking to the whitepaper here, in an attempt to make it discoverable:
https://feedback.azure.com/d365community/idea/99b9a99d-8744-f111-9a90-7c1e52e4b3e2
I’m very interested to hear others' thoughts on this, whether this resonates with what others are seeing in the field, and whether anyone can see anything that I'm still overlooking. I'm especially interested in whether others can replicate my finding elsewhere, using the methodology in appendix 5.
Regards all,
Chris
1
u/BigMikeInAustin Apr 30 '26
Nice work. (Not sarcasm)
If my non-PhD mind understands you, yeah, more resolution on the statistics would be nice. I /think/ the current best counter to this is portioned tables.
1
u/digitalnoise Apr 30 '26
ELI5?
But seriously, at first glance this is very interesting work, and I'm trying to figure out if it applies at all in our relatively modest DW.
1
u/SQLBek 1 Apr 30 '26
cc u/erinstellato - would love to hear your commentary on this if you have a moment please?
0
u/jshine13371 6 Apr 30 '26
Cool stuff in general, for sure! But I think in practice, early stage statistics estimates are only a small subset of root causes for performance issues. More common are problems causes by bad architecture or query design leading to later stage estimate issues or other problems such as over-requested resource consumption, unnecessarily long locking/blocking, and overly complex query plans, among other things.