What is Statistics in SQL Server | SansSQL

Thursday, September 17, 2009

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

Refrences: Books Online

4 comments:

Ash said...

Now thats a good post.. please unshare your previous post on update statistics as its totally wrong..

Any ways good work in maintaining a blog like this.

Sandesh Segu said...

Hey Ash, thanks for pointing... And Thank you for your valuable feedback.

SANTHOSH said...

Hai sandesh , can i know how we can know that the table has statistics.

Sandesh Segu said...

Hi Santhoo,
Please Check this link.
http://sanssql.blogspot.com/2009/09/find-statistics-info-for-all-tables.html

Post a Comment

Ads