sqlserver2008 如何定时清理索引碎片
sqlserver2008 如何定时清理索引碎片
查询索引引起的表垃圾碎片sql脚本:
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 如何定时清理索引碎片的更多相关文章
- SQL Server清理索引碎片
DECLARE @SchemeName NVARCHAR(MAX)=N''; DECLARE @TableName NVARCHAR(MAX)=N''; DECLARE @IndexName NVAR ...
- 数据库索引碎片——数据库sql
文章:检测和整理索引碎片 文章:[笔记整理]SQL Server 索引碎片 和 重建索引 文章介绍了检查表的索引碎片百分比 文章:[小问题笔记(八)] 常用SQL(读字段名,改字段名,打印影响行数,添 ...
- 【译】索引进阶(十二):SQL SERVER中的索引碎片【中篇】
原文链接:传送门. 为了讨论碎片产生的原因,以及避免和移除索引碎片的技术,我们必须从本进阶系列后续将介绍的两个章节借用一些知识点:创建/更新索引的知识,以及向一个索引表插入数据行的相关知识. 当我们讲 ...
- sql server维护解决方案(备份、检查完整性、索引碎片整理)
请务必看原文 原文:https://ola.hallengren.com/frequently-asked-questions.html 经常问的问题 入门 如何开始使用SQL Server维护解决方 ...
- SQL索引碎片的产生,处理过程。
本文参考 https://www.cnblogs.com/CareySon/archive/2011/12/22/2297568.html https://www.jb51.net/softjc/12 ...
- Linux日志定时清理
linux是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,让系统定时清理一些不需要的文件很有一种爽快的事情.不用你去每天惦记着是否需要清理日志 ...
- Oracle索引碎片检查及定期重建常用表的索引
背景说明: 今天查阅书籍时,偶然间发现“在对某个索引行执行删除操作时,只是为该行增加了一个删除标记,这个索引行并不会释放它的存储空间,Insert产生的新的索引行也不能被插入到该位置.索引列的修改过程 ...
- SQL Server2005索引碎片分析和解决方法
SQL Server2005索引碎片分析和解决方法 本文作者(郑贤娴),请您在阅读本文时尊重作者版权. 摘要: SQL Server,为了反应数据的更新,需要维护表上的索引,因而这些索引会形成碎片.根 ...
- linux 定时清理session
php session 运营想让用户登陆网站就能永久保持登陆会话,感觉这是扯淡,因为视频播放1小时,所以我设置了两小时过期. 但是用户过多,导致session文件大量存储.产生上百万千万.服务器空间很 ...
随机推荐
- Velocity中#set指令
引用可以让模板设计者生成动态内容,而指令允许设计者真正的负责页面的展现和内容. 指令是以#开头,后面紧跟一个关键字,比如if,else,foreach等.而这个关键字,是可以被放在{}中间的.这是规范 ...
- APK的目录结构
APK 包含以下内容: 被编译的代码文件(.dex文件) 文件资源(resources) assets. 证书(certificates) 清单文件(maifestfile) assets 文件 li ...
- HTML5 - HTML5 postMessage API 注意事项
一:发送 window.postMessage("Hello, world", "http://127.0.0.1:8080"); 注意,必须要加上http:/ ...
- Enhancing the Scalability of Memcached
原文地址: https://software.intel.com/en-us/articles/enhancing-the-scalability-of-memcached-0 1 Introduct ...
- java递归简易应用教程
package com.xiaohao.test; import java.util.ArrayList;import java.util.List;import java.util.Locale;i ...
- 【转】Rails 3.1错误-Could not find a JavaScript runtime及execjs和therubyracer介绍
转自:http://rubyer.me/blog/740/ Rails 3.1错误 /gems/execjs-1.1.2/lib/ execjs/runtimes.rb:43:in `autodete ...
- Educational Codeforces Round 2 C. Make Palindrome 贪心
C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- 杭电 3177 Crixalis's Equipment
http://acm.hdu.edu.cn/showproblem.php? pid=3177 Crixalis's Equipment Time Limit: 2000/1000 MS (Java/ ...
- java后端模拟表单提交
代码可实现文本域及非文本域的处理 请求代码: /** * 上传 * * @param urlStr * @param textMap * @param fileMap * @return */ pub ...
- UNIX标准化及实现之限制
前言 UNIX系统实现定义了很多幻数和常量,其中有很多已被硬编码(关于硬编码和软编码:http://www.cnblogs.com/chenkai/archive/2009/04/10/1432903 ...