sqlserver2008 如何定时清理索引碎片

查询索引引起的表垃圾碎片sql脚本:

 SELECT object_name(a.object_id) [TableName] ,a.index_id ,name [IndexName] ,avg_fragmentation_in_percent 
From sys.dm_db_index_physical_stats ( DB_ID() , NULL , NULL, NULL, NULL ) 
As a JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id
Where avg_fragmentation_in_percent > 0
Order By avg_fragmentation_in_percent Desc 

------解决方案--------------------

维护计划,其中有一项是索引重整.

全界面操作.

------解决方案--------------------
declare @Db_name nvarchar(256)
,@SchemaName nvarchar(256)
,@TableName Nvarchar(256)
,@IndexName Nvarchar(512)
,@PctFrag decimal
,@Defrag nvarchar(max)

declare frg_cur cursor for
select d.name,e.name,c.name ,b.name ,a.avg_fragmentation_in_percent  
from sys.dm_db_index_physical_stats(DB_ID(''),NULL,NULL,NULL,'SAMPLED') as a
join sys.indexes as b on a.object_id=b.object_id and a.index_id=b.index_id  
join sys.tables as c on a.object_id=c.object_id  
join sys.databases as d on a.database_id=d.database_id
join sys.schemas as e on c.schema_id=e.schema_id
where a.avg_fragmentation_in_percent >20--索引碎片的大小百分比
and c.type='U' and a.page_count>8

open frg_cur
fetch next from frg_cur into @Db_name,@SchemaName,@TableName,@IndexName,@PctFrag
while @@FETCH_STATUS=0
begin
if @PctFrag between 20.0 and 40.0
begin
set @Defrag=N' ALTER INDEX '+@IndexName+' ON '+@Db_name+'.'+@SchemaName+'.'+ @TableName +' REORGANIZE'--重新组织索引页不删除索引
EXEC SP_EXECUTESQL @Defrag

end
else if @PctFrag>40.0
begin
SET @Defrag=N' ALTER INDEX '+@IndexName+' ON '+@Db_name+'.'+@SchemaName+'.'+ @TableName +' REBUILD ONLINE=ON'--联机重建索引。即不锁定表重新创建索引
EXEC SP_EXECUTESQL @Defrag

end
fetch next from frg_cur into @Db_name,@SchemaName,@TableName,@IndexName,@PctFrag
end
close frg_cur
deallocate frg_cur
---记得把系统数据库过滤掉就可以了

写个作业 调用 
------解决方案--------------------
----将其放到作业中定时执行即可(最好在生产服务器空闲的时候执行)。仅供参考。

create procedure pr_auto_indexdefrag
as
set nocount on

begin

declare @Db_name nvarchar(256)
,@SchemaName nvarchar(256)
,@TableName Nvarchar(256)
,@IndexName Nvarchar(512)
,@PctFrag decimal
,@Defrag nvarchar(max)

if exists(select 1 from sys.objects where object_id =object_id(N'#tmp')) Drop table #tmp;
create table #tmp(dbname nvarchar(256),tablename nvarchar(256),indexname nvarchar(256),schemaname nvarchar(256),avgfragment decimal)

exec sp_MSforeachdb 'insert into #tmp(dbname,tablename,indexname,schemaname,avgfragment)
select ''?'' dbname,c.name,b.name,e.name,a.avg_fragmentation_in_percent  
from ?.sys.dm_db_index_physical_stats(DB_ID(''?''),NULL,NULL,NULL,''SAMPLED'') as a
join ?.sys.indexes as b on a.object_id=b.object_id and a.index_id=b.index_id  
join ?.sys.tables as c on a.object_id=c.object_id  
join sys.databases as d on a.database_id=d.database_id
join ?.sys.schemas as e on c.schema_id=e.schema_id
where a.avg_fragmentation_in_percent >20
and c.type=''U'' and a.page_count>8
and d.name like ''caihong_%'''

declare frg_cur cursor for
select * from #tmp

open frg_cur
fetch next from frg_cur into @Db_name,@TableName,@IndexName,@SchemaName,@PctFrag
while @@FETCH_STATUS=0
begin

if @PctFrag between 20.0 and 40.0
begin
set @Defrag=N' ALTER INDEX '+@IndexName+' ON'+@Db_name+'.'+@SchemaName+'.'+ @TableName +' REORGANIZE'--重新组织索引页不删除索引
EXEC SP_EXECUTESQL @Defrag
end
else if @PctFrag>40.0
begin
SET @Defrag=N' ALTER INDEX '+@IndexName+' ON'+@Db_name+'.'+@SchemaName+'.'+ @TableName +' REBUILD WITH (ONLINE = ON )'--联机重建索引。即不锁定表重新创建索引
EXEC SP_EXECUTESQL @Defrag
end
fetch next from frg_cur into @Db_name,@TableName,@IndexName,@SchemaName,@PctFrag
end
close frg_cur
deallocate frg_curend

end

set nocount off

------解决方案--------------------
mark一下
尽量争取在生产环境空闲的情况 下优化
重整索引很耗资源的 
------解决方案--------------------

探讨
----将其放到作业中定时执行即可(最好在生产服务器空闲的时候执行)。仅供参考。

create procedure pr_auto_indexdefrag
as
set nocount on

begin

declare @Db_name nvarchar(256)
,@SchemaName nvarchar(256)
,@TableName Nvarc……

------解决方案--------------------
作业或者维护计划都可以实现的。

转载:http://www.myexception.cn/sql-server/319573_2.html

sqlserver2008 如何定时清理索引碎片的更多相关文章

  1. SQL Server清理索引碎片

    DECLARE @SchemeName NVARCHAR(MAX)=N''; DECLARE @TableName NVARCHAR(MAX)=N''; DECLARE @IndexName NVAR ...

  2. 数据库索引碎片——数据库sql

    文章:检测和整理索引碎片 文章:[笔记整理]SQL Server 索引碎片 和 重建索引 文章介绍了检查表的索引碎片百分比 文章:[小问题笔记(八)] 常用SQL(读字段名,改字段名,打印影响行数,添 ...

  3. 【译】索引进阶(十二):SQL SERVER中的索引碎片【中篇】

    原文链接:传送门. 为了讨论碎片产生的原因,以及避免和移除索引碎片的技术,我们必须从本进阶系列后续将介绍的两个章节借用一些知识点:创建/更新索引的知识,以及向一个索引表插入数据行的相关知识. 当我们讲 ...

  4. sql server维护解决方案(备份、检查完整性、索引碎片整理)

    请务必看原文 原文:https://ola.hallengren.com/frequently-asked-questions.html 经常问的问题 入门 如何开始使用SQL Server维护解决方 ...

  5. SQL索引碎片的产生,处理过程。

    本文参考 https://www.cnblogs.com/CareySon/archive/2011/12/22/2297568.html https://www.jb51.net/softjc/12 ...

  6. Linux日志定时清理

    linux是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,让系统定时清理一些不需要的文件很有一种爽快的事情.不用你去每天惦记着是否需要清理日志 ...

  7. Oracle索引碎片检查及定期重建常用表的索引

    背景说明: 今天查阅书籍时,偶然间发现“在对某个索引行执行删除操作时,只是为该行增加了一个删除标记,这个索引行并不会释放它的存储空间,Insert产生的新的索引行也不能被插入到该位置.索引列的修改过程 ...

  8. SQL Server2005索引碎片分析和解决方法

    SQL Server2005索引碎片分析和解决方法 本文作者(郑贤娴),请您在阅读本文时尊重作者版权. 摘要: SQL Server,为了反应数据的更新,需要维护表上的索引,因而这些索引会形成碎片.根 ...

  9. linux 定时清理session

    php session 运营想让用户登陆网站就能永久保持登陆会话,感觉这是扯淡,因为视频播放1小时,所以我设置了两小时过期. 但是用户过多,导致session文件大量存储.产生上百万千万.服务器空间很 ...

随机推荐

  1. json jar包支持

    json-lib工具包(json核心包)下载地址: http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/ json ...

  2. PHP linux spl_autoload_register区分大小写

    一个PHP脚本用到spl_autoload_register,在WINDOWS下运行正常,但在LINUX就include不了,后来发现WINDOWS大小写不敏感,而在LINUX下区分大小写,WINDO ...

  3. 配置Windows Update,补丁更新

    配置Windows Update更新下载及安装方式: #NotificationLevel说明: # 0:未配置,不会对当前设置进行更改 # 1:从不检查更新 # 2:检查更新,但是让我选择是否下载和 ...

  4. NEUOJ 1117: Ready to declare(单调队列)

    1117: Ready to declare 时间限制: 1 Sec  内存限制: 128 MB 提交: 358  解决: 41 [提交][状态][pid=1117" style=" ...

  5. ios开发——实用技术OC篇》倒计时实现的两种方法

    倒计时实现的两种方法 timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把timer给invalidate掉就ok了,代码如下: secondsCountDow ...

  6. 如何在Ubuntu 13.04中升级到 GNOME 3.8

    如何在Ubuntu 13.04中升级到 GNOME 3.8 添加 GNOME 3 PPA(Personal Package Archives) 在你进一步浏览之前,确认你正在运行的是Ubuntu 13 ...

  7. Javascript-获取URL请求参数

    function getUrlParam(){    var param = [], hash;    var url = window.location.href;//获取网页的url     va ...

  8. apache配置--虚拟目录

    apache在httpd-vhosts.conf中 配置二级域名或者泛域名: <VirtualHost *:80>    ServerAdmin 846606478@qq.com    D ...

  9. JS_CSS_logon_Mask

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Java中的DeskTop类

        在Jdk1.6以后新增加了一个类--DeskTop,在JDK中它的解释是这样的: The Desktop class allows a Java application to launch a ...