Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate Table and UPDATE Statistics

Do we need to Update table statistics after calling Truncate table or it gets updated automatically?

Q: Do we need to call "UPDATE STATISTICS" after truncating a table?

like image 619
noob.spt Avatar asked Nov 13 '09 16:11

noob.spt


People also ask

Does truncate remove statistics?

Stats are not automatically updated until the stats are needed again. aka, the TRUNCATE does not do it. So "No".

Do we need to gather stats after truncate table?

I learned that truncate needs stats gathering on the table as its successor process so the database gets the actual statistics, otherwise previous stats are not cleared by the truncate statement.

What effect does the truncate statement have on a table statistics?

In SQL, the TRUNCATE TABLE statement is a Data Definition Language (DDL) operation that marks the extents of a table for deallocation (empty for reuse). The result of this operation quickly removes all data from a table, typically bypassing a number of integrity enforcing mechanisms.

How do you update table statistics?

In Object Explorer, click the plus sign to expand the database in which you want to update the statistic. Click the plus sign to expand the Tables folder. Click the plus sign to expand the table in which you want to update the statistic. Click the plus sign to expand the Statistics folder.


1 Answers

Stats are not automatically updated until the stats are needed again. aka, the TRUNCATE does not do it. So "No".

The original answer was "Yes" because it's not automatic as part of TRUNCATE. It depends how you read the question :-)

Remember, statistics are updated automatically when needed by a query (eg number of row change). From "Index Statistics" in BOL

A statistics update is initiated whenever the statistics used in a query execution plan fail a test for current statistics

One way to verify using STATS_DATE...

SELECT
   name AS index_name, 
   STATS_DATE(object_id, index_id)
FROM
   sys.indexes 
WHERE
   object_id = OBJECT_ID('MyTruncatedTable')


Edit: I wanted to make I sure :-)

You'll see stats are only updated by the SELECT statements, not the INSERT, DELETE or TRUNCATE

IF OBJECT_ID('dbo.foo') IS NOT NULL DROP TABLE dbo.foo
CREATE TABLE dbo.foo (
    bar int NOT NULL IDENTITY (1, 1) PRIMARY KEY,
    thing int NOT NULL
)
CREATE INDEX IX_thing ON dbo.foo (thing)

INSERT dbo.foo (thing) SELECT c1.object_id FROM sys.columns c1, sys.columns c2
SELECT
   name AS index_name, 
   STATS_DATE(object_id, index_id) AS AfterLoad
FROM sys.indexes WHERE object_id = OBJECT_ID('dbo.foo')

SELECT DISTINCT thing FROM dbo.foo ORDER BY thing DESC
SELECT
   name AS index_name, 
   STATS_DATE(object_id, index_id) AS AfterFirstQuery
FROM sys.indexes WHERE object_id = OBJECT_ID('dbo.foo')

DELETE TOP (50000) dbo.foo
SELECT
   name AS index_name, 
   STATS_DATE(object_id, index_id) AS AfterDelete
FROM sys.indexes WHERE object_id = OBJECT_ID('dbo.foo')

SELECT DISTINCT thing FROM dbo.foo ORDER BY thing DESC
SELECT
   name AS index_name, 
   STATS_DATE(object_id, index_id) AS After2ndQuery
FROM sys.indexes WHERE object_id = OBJECT_ID('dbo.foo')

TRUNCATE TABLE dbo.foo
SELECT
   name AS index_name, 
   STATS_DATE(object_id, index_id) AS AfterTruncate
FROM sys.indexes WHERE object_id = OBJECT_ID('dbo.foo')

SELECT DISTINCT thing FROM dbo.foo ORDER BY thing DESC
SELECT
   name AS index_name, 
   STATS_DATE(object_id, index_id) AS After3rdQuery
FROM sys.indexes WHERE object_id = OBJECT_ID('dbo.foo')
like image 78
gbn Avatar answered Oct 01 '22 08:10

gbn