重建索引SQL语句/更新统计信息/收缩数据库 原创
9人赞赏了该文章
1819次浏览
编辑于2021年06月10日 11:25:41
重建索引SQL语句,放到数据库里面执行。执行语句会占用服务器资源影响性能,请业务空闲时操作
--重建索引
declare @SqlStr nvarchar(max)
set @SqlStr=''
select @SqlStr=@SqlStr++'alter index all on '+name+' rebuild;'
from sysobjects where xtype='U'
and(name like'T_%'
)and(name not like'TMP%')
exec (@SqlStr)
--更新统计信息
declare @SqlStr1 nvarchar(max)
set @SqlStr1=''
select @SqlStr1=@SqlStr1++'UPDATE STATISTICS '+name+';'from sysobjects
where xtype='U'and(name like'T_%')and(name not like'TMP%')
exec (@SqlStr1)
--收缩数据库
declare @SqlStr2 nvarchar(max)
set @SqlStr2=''
select @SqlStr2=@SqlStr2++'ALTER TABLE '+name+' rebuild WITH (DATA_COMPRESSION =ROW);'
from sysobjects where xtype='U'and(name like'T_%')and(name not like'TMP%')
exec (@SqlStr2)
推荐阅读