当前位置:首页 > 博客人生 > 技术分享

文章摘要:查找数据库中,索引碎片率高于30% 的数据库索引,并生成重构的语句,可把查询结果第一列复制出来,在数据库中执行索引重构,对性能有所提升有帮助

sqlServer自动生成索引_批量重建索引_sqlserver批量新建索引

发布时间:2022-05-17 作者:沈川超 分类: SQLserver

-----查找数据库中,索引碎片率高于30% 的数据库索引,并生成重构的语句,可把查询结果第一列复制出来,在数据库中执行索引重构,对性能有所提升有帮助


SELECT 

'ALTER INDEX ['+ind.name + '] ON [dbo].[' +OBJECT_NAME(ind.OBJECT_ID) + '] REBUILD PARTITION = ALL WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90);',

OBJECT_NAME(ind.OBJECT_ID) AS TableName, 

ind.name AS IndexName, indexstats.index_type_desc AS IndexType, 

indexstats.avg_fragmentation_in_percent

FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) indexstats 

INNER JOIN sys.indexes ind  

ON ind.object_id = indexstats.object_id 

AND ind.index_id = indexstats.index_id 

WHERE indexstats.avg_fragmentation_in_percent > 30

and isnull(ind.name,'') <> ''

and indexstats.index_type_desc = 'NONCLUSTERED INDEX'

ORDER BY indexstats.avg_fragmentation_in_percent DESC 


上一篇: Chrome 谷歌浏览器将整个网页保存为图片


下一篇: C#常规面试题,应届生面试题