the syntax for creating statistics is:
-- Syntax for SQL Server and Azure SQL Database
UPDATE STATISTICS table_or_indexed_view_name
[
{
{ index_or_statistics__name }
| ( { index_or_statistics_name } [ ,...n ] )
}
]
[ WITH
[
FULLSCAN
[ [ , ] PERSIST_SAMPLE_PERCENT = { ON | OFF } ]
| SAMPLE number { PERCENT | ROWS }
[ [ , ] PERSIST_SAMPLE_PERCENT = { ON | OFF } ]
| RESAMPLE
[ ON PARTITIONS ( { <partition_number> | <range> } [, ...n] ) ]
| <update_stats_stream_option> [ ,...n ]
]
[ [ , ] [ ALL | COLUMNS | INDEX ]
[ [ , ] NORECOMPUTE ]
[ [ , ] INCREMENTAL = { ON | OFF } ]
[ [ , ] MAXDOP = max_degree_of_parallelism ]
] ;
<update_stats_stream_option> ::=
[ STATS_STREAM = stats_stream ]
[ ROWCOUNT = numeric_constant ]
[ PAGECOUNT = numeric_contant ]
and here there is an excellent script by Martin Smith that scripts the stats with STATS_STREAM
when I run that script in one of my test databases I get the following:
DECLARE @Schema sysname,
@Table sysname,
@StatsName sysname,
@IncludeStatsStream BIT,
@StatsStream VARCHAR(MAX),
@TwoPartQuotedName NVARCHAR(500);
select @Schema = 'dbo',
@Table = 'tbl_Application_Medical',
@StatsName = '_WA_Sys_00000002_7039AB57',
@IncludeStatsStream = 1;
select @TwoPartQuotedName = QUOTENAME(@Schema) + '.' + QUOTENAME(@Table);
IF @IncludeStatsStream = 1 AND @@MICROSOFTVERSION/ POWER(2,24) > 9
BEGIN
DECLARE @StatsResults TABLE
(
StatsStream VARBINARY(MAX),
Rows BIGINT,
DataPages BIGINT
);
INSERT INTO @StatsResults
EXEC sys.sp_executesql
N'DBCC SHOW_STATISTICS(@TwoPartQuotedName, @StatsName) WITH STATS_STREAM;',
N'@TwoPartQuotedName NVARCHAR(500), @StatsName sysname',
@TwoPartQuotedName = @TwoPartQuotedName,
@StatsName = @StatsName;
--Would need some other method on 2005 hence just skipping this
SELECT @StatsStream = CONVERT(VARCHAR(MAX), StatsStream, 1)
FROM @StatsResults;
END;
WITH stats AS
(
/*
Support earlier versions without these columns using trick from http://dba.stackexchange.com/a/66755/3690 */
SELECT x.*
FROM (SELECT NULL AS filter_definition, NULL AS is_incremental) AS dummy
CROSS APPLY
(
SELECT object_id, stats_id, name, no_recompute, filter_definition, is_incremental
FROM sys.stats
) AS x
)
SELECT '
CREATE STATISTICS ' + QUOTENAME(name) + '
ON ' + @TwoPartQuotedName + ' (' + SUBSTRING(cols, 2, 10000000) +')
' +
ISNULL(' WHERE ' + filter_definition,'') +
ISNULL(STUFF (
ISNULL(',STATS_STREAM = ' + @StatsStream, '') +
CASE WHEN no_recompute = 1 THEN ',NORECOMPUTE' ELSE '' END +
CASE WHEN is_incremental = 1 THEN ',INCREMENTAL=ON' ELSE '' END
, 1 , 1 , ' WITH ' ) , '') AS [processing-instruction(x)]
FROM stats s
CROSS APPLY (SELECT ',' + QUOTENAME(c.name)
FROM sys.stats_columns sc
JOIN sys.columns c
ON c.object_id = sc.object_id
AND c.column_id = sc.column_id
WHERE sc.object_id = s.object_id
AND sc.stats_id = s.stats_id
ORDER BY sc.stats_column_id
FOR XML PATH(''))CA(cols)
WHERE s.object_id = OBJECT_ID(@TwoPartQuotedName)
AND s.name = @StatsName
FOR XML PATH('');
select * from @StatsResults
a xml file:
<?x CREATE STATISTICS [_WA_Sys_00000002_7039AB57] ON [dbo].[tbl_Application_Medical] ([Smoker]) WITH STATS_STREAM = lot of binary chars that would not fit here?>
when Update statistics for all databases all tables in a server
would I benefit from generating the create statistics script with stats_scream, instead of WITH FULLSCAN
?
what is the difference between these 2?