Persisting statistics sampling rate

Published Mar 23 2019 04:46 PM 2,833 Views
Microsoft
First published on MSDN on Aug 11, 2017
When SQL Server creates or updates statistics and a sampling rate is not manually specified, SQL Server calculates a default sampling rate. Depending on the real distribution of data in the underlying table, the default sampling rate may not accurately represent the data distribution and then cause degradation of query plan efficiency.

To improve this scenario, a database administrator can choose to manually update statistics with a specific sampling rate that can better represent the distribution of data. However, a subsequent automatic update statistics operation will reset back to the default sampling rate, possibly reintroducing degradation of query plan efficiency.

With the most recent SQL Server 2016 SP1 CU4 and SQL Server 2017 CU1 , we released an enhancement for the CREATE and UPDATE STATISTICS command – the ability to persist sampling rates between updates with a PERSIST_SAMPLE_PERCENT keyword.

Let’s use an example. I’m using the following query:
SELECT [SalesOrderID],[ShipDate],[DueDate],[OrderDate]
FROM [Sales].[SalesOrderHeaderBulk]
WHERE OrderDate BETWEEN '8/30/2012 00:00:00.000' AND '9/30/2012 00:00:00.000'
GO
Looking at the actual plan, we see a skew between estimated and actual rows in the scan over the orders table:



Let’s check stats on this table then. I’m searching for stats on table orders with column col2 (part of the join argument):
SELECT ss.stats_id, ss.name, filter_definition, last_updated, rows,
rows_sampled, steps, unfiltered_rows, modification_counter, persisted_sample_percent,
(rows_sampled * 100)/rows AS sample_percent
FROM sys.stats ss
INNER JOIN sys.stats_columns sc
ON ss.stats_id = sc.stats_id AND ss.object_id = sc.object_id
INNER JOIN sys.all_columns ac
ON ac.column_id = sc.column_id AND ac.object_id = sc.object_id
CROSS APPLY sys.dm_db_stats_properties(ss.object_id, ss.stats_id) shr
WHERE ss.[object_id] = OBJECT_ID('[Sales].[SalesOrderHeaderBulk]')
AND ac.name = 'OrderDate';
We see a statistic for the index used above, with a sampling rate of 6 percent. Let’s say I really need to improve estimations, and that having a higher sampling rate could just do that.



So I’ll just update this statistic, and while I could try to find a better (and higher) sampling rate, for this demo I’ll just update with full scan:
UPDATE STATISTICS [Sales].[SalesOrderHeaderBulk] [IX_OrderDate] WITH FULLSCAN


Let’s see the estimations now:



Great, so what happens when auto update statistics is triggered? Or I have some stats maintenance job that does not explicitly sets the sampling rate?
UPDATE STATISTICS [Sales].[SalesOrderHeaderBulk] [IX_OrderDate]


It’s back at default sampling (in this case 6 percent).

I really want to always update this statistic with 100 percent sampling rate, so I can now use the following PERSIST_SAMPLE_PERCENT keyword:
UPDATE STATISTICS [Sales].[SalesOrderHeaderBulk] [IX_OrderDate]
WITH FULLSCAN, PERSIST_SAMPLE_PERCENT = ON


Notice the new persisted_sample_percent column is now showing 100 percent (because I used FULLSCAN). Both DBCC SHOW_STATISTICS and sys.dm_db_stats_properties have this new column.

What happens when auto update statistics is triggered or my stats are manually updated again?
UPDATE STATISTICS [Sales].[SalesOrderHeaderBulk] [IX_OrderDate]

My choice for sampling percentage is now persisted. It will remain persisted until I set this option to OFF. When set to OFF (which is the default), the statistics sampling percentage will get reset to default sampling in subsequent updates that do not explicitly specify a sampling percentage.

Note that if a manual update statistics is executed with a different sampling percentage, but not using PERSIST_SAMPLE_PERCENT keyword to set it as the new persisted value, the previously set fixed sampling will still be in effect for subsequent auto update statistics, or any other manual update that does not set a specific sampling rate.
UPDATE STATISTICS [Sales].[SalesOrderHeaderBulk] [IX_OrderDate] WITH SAMPLE 90 PERCENT


Pedro Lopes ( @sqlpto ) – Senior Program Manager
%3CLINGO-SUB%20id%3D%22lingo-sub-385575%22%20slang%3D%22en-US%22%3EPersisting%20statistics%20sampling%20rate%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-385575%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Aug%2011%2C%202017%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20When%20SQL%20Server%20creates%20or%20updates%20statistics%20and%20a%20sampling%20rate%20is%20not%20manually%20specified%2C%20SQL%20Server%20calculates%20a%20default%20sampling%20rate.%20Depending%20on%20the%20real%20distribution%20of%20data%20in%20the%20underlying%20table%2C%20the%20default%20sampling%20rate%20may%20not%20accurately%20represent%20the%20data%20distribution%20and%20then%20cause%20degradation%20of%20query%20plan%20efficiency.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20To%20improve%20this%20scenario%2C%20a%20database%20administrator%20can%20choose%20to%20manually%20update%20statistics%20with%20a%20specific%20sampling%20rate%20that%20can%20better%20represent%20the%20distribution%20of%20data.%20However%2C%20a%20subsequent%20automatic%20update%20statistics%20operation%20will%20reset%20back%20to%20the%20default%20sampling%20rate%2C%20possibly%20reintroducing%20degradation%20of%20query%20plan%20efficiency.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20With%20the%20most%20recent%20%3CA%20href%3D%22http%3A%2F%2Fsupport.microsoft.com%2Fen-us%2Fhelp%2F4024305%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20SQL%20Server%202016%20SP1%20CU4%20%3C%2FA%3E%20and%20%3CA%20href%3D%22http%3A%2F%2Fsupport.microsoft.com%2Fhelp%2F4038634%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20SQL%20Server%202017%20CU1%20%3C%2FA%3E%20%2C%20we%20released%20an%20enhancement%20for%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fcreate-statistics-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20CREATE%20%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fstatements%2Fupdate-statistics-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20UPDATE%20STATISTICS%20%3C%2FA%3E%20command%20%E2%80%93%20the%20ability%20to%20persist%20sampling%20rates%20between%20updates%20with%20a%20%3CSTRONG%3E%20PERSIST_SAMPLE_PERCENT%20%3C%2FSTRONG%3E%20keyword.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Let%E2%80%99s%20use%20an%20example.%20I%E2%80%99m%20using%20the%20following%20query%3A%20%3CBR%20%2F%3E%20SELECT%20%5BSalesOrderID%5D%2C%5BShipDate%5D%2C%5BDueDate%5D%2C%5BOrderDate%5D%20%3CBR%20%2F%3E%20FROM%20%5BSales%5D.%5BSalesOrderHeaderBulk%5D%20%3CBR%20%2F%3E%20WHERE%20OrderDate%20BETWEEN%20'8%2F30%2F2012%2000%3A00%3A00.000'%20AND%20'9%2F30%2F2012%2000%3A00%3A00.000'%20%3CBR%20%2F%3E%20GO%20%3CBR%20%2F%3E%20Looking%20at%20the%20actual%20plan%2C%20we%20see%20a%20skew%20between%20estimated%20and%20actual%20rows%20in%20the%20scan%20over%20the%20orders%20table%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98620i263320C6833B72E9%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Let%E2%80%99s%20check%20stats%20on%20this%20table%20then.%20I%E2%80%99m%20searching%20for%20stats%20on%20table%20orders%20with%20column%20col2%20(part%20of%20the%20join%20argument)%3A%20%3CBR%20%2F%3E%20SELECT%20ss.stats_id%2C%20ss.name%2C%20filter_definition%2C%20last_updated%2C%20rows%2C%20%3CBR%20%2F%3E%20rows_sampled%2C%20steps%2C%20unfiltered_rows%2C%20modification_counter%2C%20persisted_sample_percent%2C%20%3CBR%20%2F%3E%20(rows_sampled%20*%20100)%2Frows%20AS%20sample_percent%20%3CBR%20%2F%3E%20FROM%20sys.stats%20ss%20%3CBR%20%2F%3E%20INNER%20JOIN%20sys.stats_columns%20sc%20%3CBR%20%2F%3E%20ON%20ss.stats_id%20%3D%20sc.stats_id%20AND%20ss.object_id%20%3D%20sc.object_id%20%3CBR%20%2F%3E%20INNER%20JOIN%20sys.all_columns%20ac%20%3CBR%20%2F%3E%20ON%20ac.column_id%20%3D%20sc.column_id%20AND%20ac.object_id%20%3D%20sc.object_id%20%3CBR%20%2F%3E%20CROSS%20APPLY%20sys.dm_db_stats_properties(ss.object_id%2C%20ss.stats_id)%20shr%20%3CBR%20%2F%3E%20WHERE%20ss.%5Bobject_id%5D%20%3D%20OBJECT_ID('%5BSales%5D.%5BSalesOrderHeaderBulk%5D')%20%3CBR%20%2F%3E%20AND%20ac.name%20%3D%20'OrderDate'%3B%20%3CBR%20%2F%3E%20We%20see%20a%20statistic%20for%20the%20index%20used%20above%2C%20with%20a%20sampling%20rate%20of%206%20percent.%20Let%E2%80%99s%20say%20I%20really%20need%20to%20improve%20estimations%2C%20and%20that%20having%20a%20higher%20sampling%20rate%20could%20just%20do%20that.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98621i8A108799CE8A4182%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20So%20I%E2%80%99ll%20just%20update%20this%20statistic%2C%20and%20while%20I%20could%20try%20to%20find%20a%20better%20(and%20higher)%20sampling%20rate%2C%20for%20this%20demo%20I%E2%80%99ll%20just%20update%20with%20full%20scan%3A%20%3CBR%20%2F%3E%20UPDATE%20STATISTICS%20%5BSales%5D.%5BSalesOrderHeaderBulk%5D%20%5BIX_OrderDate%5D%20WITH%20FULLSCAN%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98622i23C61364E400F3D2%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Let%E2%80%99s%20see%20the%20estimations%20now%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98623i58491311D98D4DCA%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Great%2C%20so%20what%20happens%20when%20auto%20update%20statistics%20is%20triggered%3F%20Or%20I%20have%20some%20stats%20maintenance%20job%20that%20does%20not%20explicitly%20sets%20the%20sampling%20rate%3F%20%3CBR%20%2F%3E%20UPDATE%20STATISTICS%20%5BSales%5D.%5BSalesOrderHeaderBulk%5D%20%5BIX_OrderDate%5D%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98624i44F28677C0A5CC53%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20It%E2%80%99s%20back%20at%20default%20sampling%20(in%20this%20case%206%20percent).%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20I%20really%20want%20to%20always%20update%20this%20statistic%20with%20100%20percent%20sampling%20rate%2C%20so%20I%20can%20now%20use%20the%20following%20%3CSTRONG%3E%20PERSIST_SAMPLE_PERCENT%20%3C%2FSTRONG%3E%20keyword%3A%20%3CBR%20%2F%3E%20UPDATE%20STATISTICS%20%5BSales%5D.%5BSalesOrderHeaderBulk%5D%20%5BIX_OrderDate%5D%20%3CBR%20%2F%3E%20WITH%20FULLSCAN%2C%20PERSIST_SAMPLE_PERCENT%20%3D%20ON%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98625i024BCED4CFE46AB5%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Notice%20the%20new%20%3CSTRONG%3E%20persisted_sample_percent%20%3C%2FSTRONG%3E%20column%20is%20now%20showing%20100%20percent%20(because%20I%20used%20FULLSCAN).%20Both%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Fdatabase-console-commands%2Fdbcc-show-statistics-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20DBCC%20SHOW_STATISTICS%20%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-dynamic-management-views%2Fsys-dm-db-stats-properties-transact-sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20sys.dm_db_stats_properties%20%3C%2FA%3E%20have%20this%20new%20column.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20What%20happens%20when%20auto%20update%20statistics%20is%20triggered%20or%20my%20stats%20are%20manually%20updated%20again%3F%20%3CBR%20%2F%3E%20UPDATE%20STATISTICS%20%5BSales%5D.%5BSalesOrderHeaderBulk%5D%20%5BIX_OrderDate%5D%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98626iEEEC1C853F1F95A6%22%20%2F%3E%20%3CBR%20%2F%3E%20My%20choice%20for%20sampling%20percentage%20is%20now%20persisted.%20It%20will%20remain%20persisted%20until%20I%20set%20this%20option%20to%20OFF.%20When%20set%20to%20OFF%20(which%20is%20the%20default)%2C%20the%20statistics%20sampling%20percentage%20will%20get%20reset%20to%20default%20sampling%20in%20subsequent%20updates%20that%20do%20not%20explicitly%20specify%20a%20sampling%20percentage.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Note%20that%20if%20a%20manual%20update%20statistics%20is%20executed%20with%20a%20different%20sampling%20percentage%2C%20but%20not%20using%20PERSIST_SAMPLE_PERCENT%20keyword%20to%20set%20it%20as%20the%20new%20persisted%20value%2C%20the%20previously%20set%20fixed%20sampling%20will%20still%20be%20in%20effect%20for%20subsequent%20auto%20update%20statistics%2C%20or%20any%20other%20manual%20update%20that%20does%20not%20set%20a%20specific%20sampling%20rate.%20%3CBR%20%2F%3E%20UPDATE%20STATISTICS%20%5BSales%5D.%5BSalesOrderHeaderBulk%5D%20%5BIX_OrderDate%5D%20WITH%20SAMPLE%2090%20PERCENT%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98627i4E4266AE8D49FB74%22%20%2F%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Pedro%20Lopes%20(%20%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2Fsqlpto%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20%40sqlpto%20%3C%2FA%3E%20)%20%E2%80%93%20Senior%20Program%20Manager%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-385575%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Aug%2011%2C%202017%20When%20SQL%20Server%20creates%20or%20updates%20statistics%20and%20a%20sampling%20rate%20is%20not%20manually%20specified%2C%20SQL%20Server%20calculates%20a%20default%20sampling%20rate.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-385575%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerTiger%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 23 2019 04:46 PM
Updated by: