Sunday, November 3, 2013

What is Statistics in SQL Server


Statistics in SQL Server refers specifically to information that the server collects about the distribution of data in columns and indexes. These statistics are used by the SQL Server query optimizer to choose the most efficient plan for retrieving or updating data. Good statistics allow the optimizer to accurately assess the cost of different query plans, and choose a high-quality plan.
This feature is Set to ON by default in SQL Server 2000 onwards i.e. in SQL Server 2000, SQL Server 2005 and SQL Server 2008.

The automatic statistics creation function can be disabled at the database level by executing
ALTER DATABASE DBName SET AUTO_CREATE_STATISTICS OFF

Create Statistics:
Syntax:
CREATE STATISTICS statistics_name
ON { table | view } ( column [ ,...n ] )
         [ WITH
                 [  [ FULLSCAN
                       | SAMPLE number { PERCENT | ROWS } ] [ , ] ]
                 [ NORECOMPUTE ]
         ]

Example:
This example creates the City statistics group (collection), which calculates random sampling statistics on
five percent of the AddressLine1 and City columns of the Address table.
Use AdventureWorks
Go
CREATE STATISTICS City
ON Person.Address (AddressLine1, City)
WITH SAMPLE 5 PERCENT
Go

Drop Statistics:
Syntax:
DROP STATISTICS table.statistics_name | view.statistics_name
Example:
Use AdventureWorks
Go
DROP STATISTICS Person.Address.City
 
Update Statistics:
Syntax:
UPDATE STATISTICS table | view
           [
                   index
                   | ( statistics_name [ ,...n ] )
           ]
           [        WITH
                          [
                               [ FULLSCAN ]
                               | SAMPLE number { PERCENT | ROWS } ]
                               |RESAMPLE
                          ]
                          [ [ , ] [ ALL | COLUMNS | INDEX ]
                          [ [ , ] NORECOMPUTE ]
           ]

Example:
Use AdventureWorks
Go
UPDATE STATISTICS Person.Address(City)
       WITH FULLSCAN, NORECOMPUTE
Go
 
sp_updatestats:
Runs UPDATE STATISTICS against all user-defined tables in the current database.
 
Example:
Use AdventureWorks
Go
EXEC sp_updatestats
 
sp_autostats:

Displays or changes the automatic UPDATE STATISTICS setting for a specific index and statistics, or for all indexes and statistics for a given table or indexed view in the current database.
Syntax:

Exec sp_autostats [ @tblname = ] 'table_name'
                [ , [ @flagc = ] 'stats_flag' ]
                [ , [ @indname = ] 'index_name' ]
Example:
This example displays the current statistics status of all indexes on the authors table.
USE AdventureWorks
Go
EXEC sp_autostats Person.Address
This example enables the automatic statistics setting for all indexes of the authors table.
USE AdventureWorks
Go
EXEC sp_autostats Person.Address, 'ON'

DBCC SHOW_Statistics:
Displays the current distribution statistics for the specified target on the specified table.

Syntax:
DBCC SHOW_STATISTICS ( table , target )
 
Example:
USE AdventureWorks
Go
DBCC SHOW_STATISTICS (Person.Address, City)
Go
Update stats with full scan on database
 Is it possible to take update statics with full scan on database level instead of specifying table or index name?

Yes Possible below the scripts.

Method 1:

DECLARE @sql nvarchar(MAX);
SELECT @sql = (SELECT 'UPDATE STATISTICS ' +
                      quotename(s.name) + '.' + quotename(o.name) +
                      ' WITH FULLSCAN; ' AS [text()]
               FROM   sys.objects o
               JOIN   sys.schemas s ON o.schema_id = s.schema_id
               WHERE  o.type = 'U'
               FOR XML PATH(''), TYPE).value('.', 'nvarchar(MAX)');
PRINT @sql
EXEC (@sql)

Method2:

EXEC sp_MSForEachTable 'UPDATE STATISTICS ? WITH FULLSCAN;'

Method3:

DECLARE @tablename varchar(80),@shemaname varchar(80)
DECLARE @SQL AS NVARCHAR(200)
DECLARE TblName_cursor CURSOR FOR
SELECT t.name,s.name FROM sys.tables t join sys.schemas s
on s.schema_id=t.schema_id
OPEN TblName_cursor
FETCH NEXT FROM TblName_cursor
INTO @tablename,@shemaname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = 'UPDATE STATISTICS '+@shemaname+'.[' + @TableName + '] WITH FULLSCAN ' ---+ CONVERT(varchar(3), @sample) + ' PERCENT'
EXEC sp_executesql @statement = @SQL
   FETCH NEXT FROM TblName_cursor
   INTO @tablename,@shemaname
END
CLOSE TblName_cursor
DEALLOCATE TblName_cursor

1 comment:

  1. Most importantly, regardless we need to eat healthy, so arrive at for the new Wholesome Exec's helper of yore that is made with wheat pasta and has 8 grams of entire grain for every serving.

    ReplyDelete