Feb 3, 2012

Clear out DTA indexes and statistics


WHILE EXISTS (SELECT  * FROM    sys.indexes WHERE   is_hypothetical = 1)
BEGIN    
DECLARE @sql varchar(max)    
SELECT @sql = 'drop index ' + indexname + ' on [' + tablename + ']'  
FROM (
SELECT TOP 1 indexname = i.name, tablename = o.name    
FROM sys.indexes i    
INNER JOIN sys.objects o
ON i.object_id = o.object_id    
WHERE i.is_hypothetical = 1 and o.type = 'u'
) x    
SELECT @sql    
EXEC (@sql)  
END
GO

WHILE EXISTS (SELECT * FROM sys.stats i WHERE OBJECTPROPERTY(i.[object_id],'IsUserTable') = 1 AND i.[name] LIKE '_dta%' and user_created = 0)
BEGIN    
DECLARE @sql varchar(max)    
SELECT @sql = 'drop statistics [' + object_name(i.[object_id]) + '].['+ i.[name] + ']'  
FROM sys.stats i  
WHERE
OBJECTPROPERTY(i.[object_id],'IsUserTable') = 1 AND
i.[name] LIKE '_dta%'  and user_created = 0    
SELECT @sql    
EXEC (@sql)
END  

No comments:

Post a Comment

Hi,

Thanks for your visit to this blog.
We would be happy with your Queries/Suggestions.