码迷,mamicode.com
首页 > 数据库 > 详细

sqlserver用于统计数据库索引的情况

时间:2016-06-26 16:39:13      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

 


--1.创建临时表#index_clusteres
IF OBJECT_ID(‘tempdb..#index_clusteres‘) IS NOT NULL DROP TABLE #index_clusteres
CREATE TABLE #index_clusteres(tablename varchar(700),index_name VARCHAR(200),index_description VARCHAR(200),index_keys VARCHAR(200))

--2.生产索引数据 Cur_clustered(包含系统过程sp_helpindex)

declare @objname varchar(776)
declare Cur_clustered cursor local for
SELECT OBJECT_NAME(id) FROM dbo.sysindexes
WHERE indid = 1 and OBJECT_NAME(id) IN (‘ANT_ChangeNote‘,‘BD_AccruedInterests‘,‘BD_BasicInfo‘)
order by OBJECT_NAME(id)

OPEN Cur_clustered --打开游标
WHILE (1=1)
begin
FETCH NEXT FROM Cur_clustered INTO @objname; --移动游标指向到第一条数据
if @@fetch_status !=0 break; --用于跳出循环

--以下来自过程: sp_helpindex
declare @objid int, -- the object id of the table
@indid smallint, -- the index id of an index
@groupid int, -- the filegroup id of an index
@indname sysname,
@groupname sysname,
@status int,
@keys nvarchar(2126), --Length (16*max_identifierLength)+(15*2)+(16*3)
@dbname sysname,
@ignore_dup_key bit,
@is_unique bit,
@is_hypothetical bit,
@is_primary_key bit,
@is_unique_key bit,
@auto_created bit,
@no_recompute bit

-- Check to see that the object names are local to the current database.
select @dbname = parsename(@objname,3)
if @dbname is null
select @dbname = db_name()
else if @dbname <> db_name()
begin
raiserror(15250,-1,-1)
end

-- Check to see the the table exists and initialize @objid.
select @objid = object_id(@objname)
if @objid is NULL
begin
raiserror(15009,-1,-1,@objname,@dbname)
end

-- OPEN CURSOR OVER INDEXES (skip stats: bug shiloh_51196)
declare ms_crs_ind cursor local static for
select i.index_id, i.data_space_id, i.name,
i.ignore_dup_key, i.is_unique, i.is_hypothetical, i.is_primary_key, i.is_unique_constraint,
s.auto_created, s.no_recompute
from sys.indexes i
join sys.stats s on i.object_id = s.object_id and i.index_id = s.stats_id
where i.object_id = @objid

open ms_crs_ind

fetch ms_crs_ind into @indid, @groupid, @indname, @ignore_dup_key, @is_unique, @is_hypothetical,
@is_primary_key, @is_unique_key, @auto_created, @no_recompute

-- IF NO INDEX, QUIT
if @@fetch_status < 0
begin
deallocate ms_crs_ind
raiserror(15472,-1,-1,@objname) -- Object does not have any indexes.
end

IF OBJECT_ID(‘tempdb..#spindtab‘) IS NOT NULL DROP TABLE #spindtab
-- create temp table
CREATE TABLE #spindtab
(
index_name sysname collate database_default NOT NULL,
index_id int,
ignore_dup_key bit,
is_unique bit,
is_hypothetical bit,
is_primary_key bit,
is_unique_key bit,
auto_created bit,
no_recompute bit,
groupname sysname collate database_default NULL,
index_keys nvarchar(2126) collate database_default NOT NULL -- see @keys above for length descr
)

-- Now check out each index, figure out its type and keys and
-- save the info in a temporary table that we‘ll print out at the end.
while @@fetch_status >= 0
begin
-- First we‘ll figure out what the keys are.
declare @i int, @thiskey nvarchar(131) -- 128+3

select @keys = index_col(@objname, @indid, 1), @i = 2
if (indexkey_property(@objid, @indid, 1, ‘isdescending‘) = 1)
select @keys = @keys + ‘(-)‘

select @thiskey = index_col(@objname, @indid, @i)
if ((@thiskey is not null) and (indexkey_property(@objid, @indid, @i, ‘isdescending‘) = 1))
select @thiskey = @thiskey + ‘(-)‘

while (@thiskey is not null )
begin
select @keys = @keys + ‘, ‘ + @thiskey, @i = @i + 1
select @thiskey = index_col(@objname, @indid, @i)
if ((@thiskey is not null) and (indexkey_property(@objid, @indid, @i, ‘isdescending‘) = 1))
select @thiskey = @thiskey + ‘(-)‘
end

select @groupname = null
select @groupname = name from sys.data_spaces where data_space_id = @groupid

-- INSERT ROW FOR INDEX
insert into #spindtab values (@indname, @indid, @ignore_dup_key, @is_unique, @is_hypothetical,
@is_primary_key, @is_unique_key, @auto_created, @no_recompute, @groupname, @keys)

-- Next index
fetch ms_crs_ind into @indid, @groupid, @indname, @ignore_dup_key, @is_unique, @is_hypothetical,
@is_primary_key, @is_unique_key, @auto_created, @no_recompute
end
deallocate ms_crs_ind

-- DISPLAY THE RESULTS
insert into #index_clusteres
select
@objname,
‘index_name‘ = index_name,
‘index_description‘ = convert(varchar(210), --bits 16 off, 1, 2, 16777216 on, located on group
case when index_id = 1 then ‘clustered‘ else ‘nonclustered‘ end
+ case when ignore_dup_key <>0 then ‘, ignore duplicate keys‘ else ‘‘ end
+ case when is_unique <>0 then ‘, unique‘ else ‘‘ end
+ case when is_hypothetical <>0 then ‘, hypothetical‘ else ‘‘ end
+ case when is_primary_key <>0 then ‘, primary key‘ else ‘‘ end
+ case when is_unique_key <>0 then ‘, unique key‘ else ‘‘ end
+ case when auto_created <>0 then ‘, auto create‘ else ‘‘ end
+ case when no_recompute <>0 then ‘, stats no recompute‘ else ‘‘ end
+ ‘ located on ‘ + groupname),
‘index_keys‘ = index_keys
from #spindtab
order by index_name

end
CLOSE Cur_clustered --关闭游标
DEALLOCATE Cur_clustered --释放游标

sqlserver用于统计数据库索引的情况

标签:

原文地址:http://www.cnblogs.com/xiaozhi1236/p/5618072.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!