Quantcast
Viewing all articles
Browse latest Browse all 4

Index Statistics and AUTO_UPDATE_STATISTICS

I recently worked with a client that uses a SQL Server 2008 data warehouse and was having performance issues while loading data.  There were a number of issues to address, but one of the issues surrounded table statistics – more specifically, how and when they were to be maintained.

AUTO_UPDATE_STATISTICS

The first issue was the question about the load performance impact of auto stats.  My client’s concern was that if this option was enabled that SQL Server would waste resources computing statistics over and over again while data was being loaded into the tables.  The good news is that SQL Server does not compute statistics as data is loading into a table.  The auto stats does not compute statistics until they are needed.

Take for example the following:

use master
GO
 
–Disable row counting
SET NOCOUNT ON
GO
 
–Drop the DemoDatabase if it exists
IF EXISTS (SELECT * FROM sys.databases WHERE name = ‘DemoDatabase’)
BEGIN
    ALTER DATABASE DemoDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    DROP DATABASE DemoDatabase
END
GO
 
–Create the DemoDatabase
CREATE DATABASE DemoDatabase
GO
 
–Make sure auto-stats is enabled
ALTER DATABASE DemoDatabase SET AUTO_UPDATE_STATISTICS ON
GO
 
–Change database context
use DemoDatabase
GO
 
–Create a test table
CREATE TABLE dbo.TestTable
(
    ClusterColumn INT NOT NULL IDENTITY(1, 1),
    NonClusterColumn INT NOT NULL
)
GO
 
–Create a clustered index on the test table
CREATE UNIQUE CLUSTERED INDEX PK_TestTable ON dbo.TestTable
(
    ClusterColumn
)
GO
 
–Create a non-clustered index on the test table
CREATE NONCLUSTERED INDEX IX_NonClusterColumn ON dbo.TestTable
(
    NonClusterColumn
)
GO
 
/*Check the last update date for the statistics on each table
NOTE: This will be NULL as the statistics will not *exist* yet*/
SELECT
    STATS_DATE(OBJECT_ID(‘dbo.TestTable’), INDEXPROPERTY(OBJECT_ID(‘dbo.TestTable’), ‘PK_TestTable’, ‘IndexID’))        AS ClusterLastUpdate,
    STATS_DATE(OBJECT_ID(‘dbo.TestTable’), INDEXPROPERTY(OBJECT_ID(‘dbo.TestTable’), ‘IX_NonClusterColumn’, ‘IndexID’))    AS NonClusterLastUpdate
GO
 
–Load some data into the table
DECLARE @I INT = 0
WHILE @I < 1000
BEGIN
    INSERT INTO dbo.TestTable (NonClusterColumn) VALUES(@I)
    SET @I += 1
END
GO
 
/*Check the last update date for the statistics on each table
NOTE: This will be still be NULL as the statistics have not been
required yet and as such have not been created by auto stats*/
SELECT
    STATS_DATE(OBJECT_ID(‘dbo.TestTable’), INDEXPROPERTY(OBJECT_ID(‘dbo.TestTable’), ‘PK_TestTable’, ‘IndexID’))        AS ClusterLastUpdate,
    STATS_DATE(OBJECT_ID(‘dbo.TestTable’), INDEXPROPERTY(OBJECT_ID(‘dbo.TestTable’), ‘IX_NonClusterColumn’, ‘IndexID’))    AS NonClusterLastUpdate
GO
 
–Execute a query that needs statistics
SELECT
    COUNT(*)
FROM
    dbo.TestTable
WHERE
    NonClusterColumn BETWEEN 100 AND 200
GO
 
/*Check the last update date for the statistics on each table
NOTE: statistics exist now for the non-clustered index because
they were needed to execute the previous select query but still
don’t exist for the clustered index as it was not needed to execute this query*/
SELECT
    STATS_DATE(OBJECT_ID(‘dbo.TestTable’), INDEXPROPERTY(OBJECT_ID(‘dbo.TestTable’), ‘PK_TestTable’, ‘IndexID’))        AS ClusterLastUpdate,
    STATS_DATE(OBJECT_ID(‘dbo.TestTable’), INDEXPROPERTY(OBJECT_ID(‘dbo.TestTable’), ‘IX_NonClusterColumn’, ‘IndexID’))    AS NonClusterLastUpdate
GO

 

Rebuilding Indexes

Another point that was brought up during these discussions was whether it is necessary to update statistics for an index after the index is rebuilt.  The answer is no.  Rebuilding an index will automatically update your statistics even if AUTO_UPDATE_STATISTICS is set to off.  This does not hold true however for reorganizing (defragmenting) an index.  Only index rebuilds.


Filed under: Programming SQL Server, SQL Server 2008, SQL Server Deployment, SQL Server Programming, T-SQL, Uncategorized Tagged: AUTO_UPDATE_STATISTICS, Index Performance, Indexes, Optimization, Query Performance, Statistics Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 4

Trending Articles