Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

MS SQL

Posted Feb 15, 2001

SQL Server 7.0 Index Statistics Details

By Alexander Chigrik


Introduction
Distribution Statistics
View Distribution Statistics
Update Distribution Statistics

Introduction

Sometimes it is difficult to determine which indexes to use when processing a query. In this case, the query optimizer uses distribution statistics.

SQL Server 7.0 can update distribution statistics automatically, so in most cases it is not necessary to update distribution statistics manually and you can rely on SQL Server decisions to update distribution statistics.

In this article, I want to tell you about distribution statistics, and about how you can view and update distribution statistics.


Distribution Statistics

In comparison with SQL Server 6.5, there are no distribution pages in SQL Server 7.0: distribution statistics are stored as a long string of bits across multiple pages in the same way image data is stored. The pointer to to this distribution data is stored in the statblob column in the sysindexes table.

The distribution statistics are used to determine which indexes to use when processing a query or it is not possible to use an index and the full table scan will be made.

SQL Server 7.0 can update distribution statistics automatically, so it is not longer necessary to run UPDATE STATISTICS statement to update statistics manually. The SQL Server decides to update distribution statistics automatically by analyzing the value in the rowmodctr column in the sysindexes table.

Now distribution statistics can store the statistics information not only for the first column in index and not only for the indexed columns. You can create the distribution statistics for unindexed columns with the CREATE STATISTICS statement.

You can view or change the automatic UPDATE STATISTICS setting with sp_autostats stored procedure. By default, SQL Server 7.0 will automatically update distribution statistics.


View Distribution Statistics

You can use DBCC SHOW_STATISTICS statement to display the current distribution statistics for the specified target on the specified table.

This is the syntax:

DBCC SHOW_STATISTICS (table, target)

where

table  - is the table with which the index is associated.
target - is the name of the object (index name or collection) for
         which to display statistics information.

This is the example to view the index statistics for the aunmind index from the authors table in the pubs database:

USE pubs
GO
DBCC SHOW_STATISTICS (authors, aunmind)
GO


Update Distribution Statistics

To update distribution statistics, you can use UPDATE STATISTICS statement.

This is the syntax (from SQL Server Books Online):

UPDATE STATISTICS table
    [
        index
        | (statistics_name[,...n])
    ]
    [    WITH
        [
            [FULLSCAN]
            | SAMPLE number {PERCENT | ROWS}]
        ]
        [[,] [ALL | COLUMNS | INDEX]
        [[,] NORECOMPUTE]
    ]

This is the example to update distribution statistics for the aunmind index from the authors table in the pubs database:

USE pubs
GO
UPDATE STATISTICS authors aunmind
GO

For more information, see: UPDATE STATISTICS (T-SQL)

Notes

1. If you set to not automatically recompute distribution statistics, then you should manually run UPDATE STATISTICS statement periodically (when a large amount of data in an indexed column has been added, changed, or deleted).

2. After running TRUNCATE TABLE statement, the distribution statistics will also be deleted, so if you set to not automatically recompute statistics, then after adding new data, you should manually run UPDATE STATISTICS statement to recreate distribution statistics.


» See All Articles by Columnist Alexander Chigrik




MS SQL Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date