CTOCIO IT专家网

天极传媒 比特网 | 天极网 | IT专家网 | IT商网 | 52PK游戏网 | 手机天极 | IT分众 |
IT专家网搜索

数据库 | Oracle | DB2 | SQL Server | MySQL | 商业智能 | BI | DBA | Sybase| SQL Server 2008

您现在的位置: IT专家网 > 数据库子站 > 数据库技巧

SQL Server索引维护指导

作者: 王红波,  出处:IT专家网, 责任编辑: 李书琴, 
2008-04-15 09:48
  本文以笔者在实际工作中对SQL Server数据库中索引维护的思路和方法为导向,为大家介绍SQL Server索引维护相关的知识和方法。

  SQL Server 2005:

/*
描述:只显示逻辑碎片率大于5%的索引信息
限制:针对SqlServer2005以后版本。
功能:对数据库服务器所有非系统数据库进行索引碎片检查
  返回碎片率>5%的索引信息
*/
create proc p_dba_manage_get_index_defrage
as
set nocount on
if not exists(select 1 from sys.tables where name = 'dba_manage_index_defrag')
create table dba_manage_index_defrag
([db_name] varchar(255)
,[table_name] varchar(255)
,[index_name] varchar(255)
,avg_fragmentation_in_percent real
,write_time datetime default getdate()
)

declare @db_name nvarchar(40)
set @db_name = ''
 
declare cur_db_name cursor for
 select
  name
 from
  sys.databases
 where
  database_id > 4 and state = 0

open cur_db_name
fetch cur_db_name into @db_name
while (@@fetch_status=0)
begin
 
 insert into dba_manage_index_defrag
   ([db_name]
   ,table_name
   ,index_name
   ,avg_fragmentation_in_percent)
 SELECT
   db_name(a.database_id) [db_name],
   c.name [table_name],
   b.name [index_name],
   a.avg_fragmentation_in_percent
 FROM
   sys.dm_db_index_physical_stats (DB_ID(@db_name), null,NULL, NULL, 'Limited') 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
 where
  a.index_id>0
  and a.avg_fragmentation_in_percent>5
fetch next from cur_db_name into @db_name
end

CLOSE cur_db_name
DEALLOCATE cur_db_name

GO
select * from dba_manage_index_defrag –查看结果

共6页。 9 1 2 3 4 5 6 :

网友评论

笔名 
请您注意:遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。    IT专家网友拥有管理笔名和留言的一切权利。
  • 周排行榜
  • 月排行榜

邮件订阅


    
天极服务 | 关于我们 | 网站律师 | 加入我们 | 联系我们 | 广告业务 | 友情链接 | 我要挑错
All Rights Reserved, Copyright 2004-2008, Ctocio.com.cn
渝ICP证B2-20030003号 如有意见请与我们联系 powered by 天极内容管理平台CMS4i