SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update Statistics script


Update Statistics script

Author
Message
Guras
Guras
Say Hey Kid
Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)Say Hey Kid (680 reputation)

Group: General Forum Members
Points: 680 Visits: 1204
I am looking for a script where I can update the statistics of the objects in a database if only the last stat updates date was before any specific date. Any help would be greatly appreciated.

Thank you.
higgim
higgim
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1182 Visits: 2626
SET NOCOUNT ON
DECLARE @StatsToUpdate TABLE ( StatName NVARCHAR(MAX) )
DECLARE @StatUpdating NVARCHAR(MAX)

INSERT INTO @StatsToUpdate
( StatName
)
SELECT 'UPDATE STATISTICS dbo.' + OBJECT_NAME + ' ' + index_name
FROM ( SELECT TOP 1
OBJECT_NAME(A.object_id) AS Object_Name ,
A.name AS index_name ,
STATS_DATE(A.OBJECT_ID, index_id) AS StatsUpdated ,
DATEDIFF(d, STATS_DATE(A.OBJECT_ID, index_id),
GETDATE()) DaysOld
FROM sys.indexes A
INNER JOIN sys.tables B ON A.object_id = B.object_id
WHERE A.name IS NOT NULL
AND DATEDIFF(d, STATS_DATE(A.OBJECT_ID, index_id),
GETDATE()) >= 0
) StatsThatAreTooOld

WHILE ( SELECT COUNT(*)
FROM @StatsToUpdate
) > 0
BEGIN
SET @StatUpdating = ( SELECT TOP 1
statname
FROM @StatsToUpdate
)
EXECUTE sp_executesql @StatUpdating
PRINT 'Done ' + @StatUpdating
DELETE FROM @StatsToUpdate
WHERE StatName = @StatUpdating
END



Not the most elegant. Simply looks at the stats and in this case returns a list of stats more than 0 days old and then carrys out an update stats statement

Would need a little tweaking to fully meet your needs probably and there are probably better ways to do this.
Erin Stellato
Erin Stellato
SSC Veteran
SSC Veteran (256 reputation)SSC Veteran (256 reputation)SSC Veteran (256 reputation)SSC Veteran (256 reputation)SSC Veteran (256 reputation)SSC Veteran (256 reputation)SSC Veteran (256 reputation)SSC Veteran (256 reputation)

Group: General Forum Members
Points: 256 Visits: 404
If you're running SQL Server 2008R2 SP2, or SQL Server 2012 SP1, you can use the new statistics DMF sys.dm_db_stats_properties. I have a query for it on my blog: http://www.sqlskills.com/blogs/erin/new-statistics-dmf-in-sql-server-2008r2-sp2/

Otherwise, you'll have to roll through DBCC SHOW_STATISTICS or use STATS_DATE as the previous poster mentioned.

Hope that helps!

Erin
DBA_Dom
DBA_Dom
Right there with Babe
Right there with Babe (732 reputation)Right there with Babe (732 reputation)Right there with Babe (732 reputation)Right there with Babe (732 reputation)Right there with Babe (732 reputation)Right there with Babe (732 reputation)Right there with Babe (732 reputation)Right there with Babe (732 reputation)

Group: General Forum Members
Points: 732 Visits: 81110
This is what I use to do that same thing. It is used in a SSIS package and the results are held in an object variable then I loop through that variable executing an update stats command.


select o.object_id,o.name,s.stats_id,s.name
from sys.objects o
inner join sys.stats s on o.object_id = s.object_id
where o.type = 'U'
and stats_date(o.object_id,s.stats_id) <= getdate()-20
order by o.object_id,s.stats_id


bangsql
bangsql
SSC-Enthusiastic
SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)

Group: General Forum Members
Points: 110 Visits: 663
Execute following ---

use dbname
go
exec sp_updatestats

(this sp will detect and automatically update only require)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search