/*eg:

--调用该过程实例
--1 创建临时表
IF OBJECT_ID('tempdb..#index_sql_text') IS NOT NULL
DROP TABLE #index_sql_text
CREATE TABLE #index_sql_text(
tablename varchar(700),
index_name VARCHAR(200),
index_description VARCHAR(200),
index_keys VARCHAR(200),
create_sql_test VARCHAR(500),
drop_sql_test VARCHAR(500)
) --2 执行过程向临时表写入数据
EXEC pro_index_sql_text 'ED_RetailPriceIndex','PK_ED_RetailPriceIndex' --3 使用临时表数据
SELECT * FROM #index_sql_text */ IF EXISTS (SELECT TOP 1 1 FROM sys.objects where type = 'P' AND name = 'pro_index_sql_text')
DROP PROCEDURE pro_index_sql_text
GO CREATE PROCEDURE pro_index_sql_text
@objname nvarchar(776), --表名:该变量不能为空,所以不默认指定
@indexname nvarchar(200) = NULL --索引名:当指定索引时返回该索引数据,当没有指定值时默认返回表的所有索引
AS /*
最后修改日期:2016-12-08
说明:给定表名,返回对应表的索引,主键,唯一约束键的删除及新建脚本
参数:@objname-表名,必须指定,不能包含架构名如 dbo.table(有问题),table(正确)
@indexname-索引名:当指定索引时返回该索引数据,当没有指定值时默认返回表的所有索引 --declare @objname varchar(776)
--select @objname = 'TEST_TABLE'
*/ --1 判断过程外面是否已经创建,创建临时表 #index_sql_text 存放最后结果
IF OBJECT_ID('tempdb..#index_sql_text') IS NULL
BEGIN
CREATE TABLE #index_sql_text(
tablename varchar(700),
index_name VARCHAR(200),
index_description VARCHAR(200),
index_keys VARCHAR(200),
create_sql_test VARCHAR(500),
drop_sql_test VARCHAR(500)
)
END --2 声明变量并检查库表关系(借鉴系统过程 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 --@dbname 赋值
select @dbname = parsename(@objname,3)
if @dbname is null
select @dbname = db_name() -- 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)
return
end --3 声明游标(包括该表的索引情况)
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 --限制对象名称
and (i.name = @indexname OR @indexname IS NULL) --限制索引名称 --打开游标
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.
return
end --4 新建存放基础数据临时表 #spindtab
IF OBJECT_ID('tempdb..#spindtab') IS NOT NULL
DROP TABLE #spindtab
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 /*
返回索引信息及索引字段的升降序
INDEXKEY_PROPERTY ( object_ID ,index_ID ,key_ID ,property )
property 要返回其信息的属性的名称。
IsDescending
存储索引列的排序顺序。
1 = 降序 0 = 升序
*/
select @keys = index_col(@objname, @indid, 1), @i = 2
if (indexkey_property(@objid, @indid, 1, 'isdescending') = 1)
--select @keys = @keys + '(-)'
select @keys = @keys + ' desc' select @thiskey = index_col(@objname, @indid, @i)
if ((@thiskey is not null) and (indexkey_property(@objid, @indid, @i, 'isdescending') = 1))
--select @thiskey = @thiskey + '(-)'
select @thiskey = @thiskey + ' desc' 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 + '(-)'
select @thiskey = @thiskey + ' desc'
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 --5 结果集拼接并写入临时表
insert into #index_sql_text
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,
'create_sql_test' = CASE WHEN is_primary_key = 1 THEN 'ALTER TABLE ' + @objname+ ' ADD CONSTRAINT ' + index_name + ' PRIMARY KEY '
+CASE WHEN index_id =1 THEN 'CLUSTERED ' ELSE 'NONCLUSTERED ' END
+'('+index_keys+');'
WHEN is_primary_key = 0 AND is_unique_key = 1 THEN 'ALTER TABLE ' + @objname+ ' ADD CONSTRAINT ' + index_name
+CASE WHEN is_unique =0 THEN '' ELSE ' UNIQUE ' END
+CASE WHEN index_id =1 THEN 'CLUSTERED ' ELSE 'NONCLUSTERED ' END
+'('+index_keys+');'
WHEN is_primary_key = 0 AND is_unique_key = 0 THEN 'CREATE ' +
CASE WHEN is_unique =0 THEN '' ELSE 'UNIQUE ' END +
CASE WHEN index_id =1 THEN 'CLUSTERED ' ELSE 'NONCLUSTERED ' END + 'INDEX '+index_name+
' on ' + @objname+'('+index_keys+');'
END,
'drop_sql_test' = CASE WHEN is_primary_key = 1 THEN 'ALTER TABLE ' + @objname+ ' DROP CONSTRAINT ' + index_name+';'
WHEN is_primary_key = 0 AND is_unique_key = 1 THEN 'ALTER TABLE ' + @objname
+ ' DROP CONSTRAINT ' + index_name+';'
WHEN is_primary_key = 0 AND is_unique_key = 0 THEN 'DROP INDEX ' + @objname
+'.'+ index_name+';'
END
from #spindtab
order by index_name /*
--调试代码是方便查看,用于调度时不需要该语句
SELECT * FROM #index_sql_text
*/

sqlserver用于统计表索引情况的更多相关文章

  1. SQLServer → 09:索引

    一.索引概念 用途 我们对数据查询及处理速度已成为衡量应用系统成败的标准,而采用索引来加快数据处理速度通常是最普遍采用的优化方法. 概念 索引是一个单独的,存储在磁盘上的数据结构,它们包含则对数据表里 ...

  2. SQLSERVER如何查看索引缺失

    SQLSERVER如何查看索引缺失 当大家发现数据库查询性能很慢的时候,大家都会想到加索引来优化数据库查询性能, 但是面对一个复杂的SQL语句,找到一个优化的索引组合对人脑来讲,真的不是一件很简单的事 ...

  3. SQLSERVER的 筛选索引(Fiter Index)

    fiter index(筛选索引)是SQL Server的一项功能,可使此数据库与众不同. 筛选索引的概念 SQL Server中常用的索引是一种物理结构,它包含来自所有行的一组选定列的值 在一张桌子 ...

  4. SQLServer之修改索引

    使用SSMS数据库管理工具修改索引 使用表设计器修改索引 表设计器可以修改任何类型的索引,修改索引的步骤相同,本示例为修改唯一非聚集索引. 1.连接数据库,选择数据库,选择数据表->右键点击表- ...

  5. SQLServer 事物与索引

    SqlServer 事物与索引 分享by:授客 QQ:1033553122 详情点击百度网盘分享链接: SqlServer 事物与索引.ppt

  6. 一个try可以跟进多个catch语句,用于处理不同情况,当一个try只能匹配一个catch

    一个try可以跟进多个catch语句,用于处理不同情况.当一个try只能匹配一个catch. 我们可以写多个catch语句,但是不能将父类型的exception的位置写在子类型的excepiton之前 ...

  7. SqlServer中查看索引的使用情况

    --查看数据库索引的使用情况 select db_name(database_id) as N'TOPK_TO_DEV', --库名 object_name(a.object_id) as N'Top ...

  8. 查看SqlServer的内存使用情况

    上一篇提到动态T-SQL会产生较多的执行计划,这些执行计划会占用多少内存呢?今天从徐海蔚的书中找到了答案.动态视图不仅可以查到执行计划的缓存,数据表的页面缓存也可以查到,将SQL整理一下,做个标记. ...

  9. 【SqlServer】聚集索引与主键、非聚集索引

    目录结构: contents structure [-] 聚集索引和非聚集索引的区别 聚集索引和主键的区别 主键和(非)聚集索引的常规操作 聚集索引.非聚集索引在SqlServer.MySQL.Ora ...

随机推荐

  1. bzoj 1067 特判

    这道题的大题思路就是模拟 假设给定的年份是x,y,首先分为4个大的情况,分别是 x的信息已知,y的信息已知 x的信息已知,y的信息未知 x的信息未知,y的情况已知 x的信息未知,y的情况未知 然后对于 ...

  2. hdu 4506 小明系列故事——师兄帮帮忙

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4506 题目大意:找规律,判断k的t次幂前面的系数. #include <iostream> ...

  3. django执行sql

    http://docs.30c.org/djangobook2/chapter10/ def first_names(self, last_name): cursor = connection.cur ...

  4. linux基础-临时和永久修改ip地址以及通配符相关

    一.临时配置网络(ip,网关,dns) 修改临时ip地址: 1.ifconfig查看当前的网卡和ip地址 2.临时修改IP地址:ifconfig ens32 192.168.16.200/24,ifc ...

  5. Django【进阶】信号

    -信号 Django中提供了“信号调度”,用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者.   问题:如何对所有数据库添加操作进行日志记录? 问题:信 ...

  6. MySQL 的七种 join

    建表 在这里呢我们先来建立两张有外键关联的张表. CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`tbl_dept`( `id` ...

  7. python3的leetcode题,两个数求和等于目标值,返回这两个数的索引组成的列表(三种方法)

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为gai目标值的 两个 整数. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 ...

  8. python基础之程序交互与数据类型

    一.程序交互 1.什么是程序交互? name=input('输入用户名:') #无论用户输入何种内容,input都会存成字符串格式 print(name) 2.为啥要有程序交互? 让计算机能够模拟人, ...

  9. 怎么将string list 转成有特殊字符分开字符串

    https://stackoverflow.com/questions/4021851/join-string-list-elements-with-a-delimiter-in-one-step Y ...

  10. POJ 2503.Babelfish-sscanf()函数+strcmp()函数+二分

    Babelfish   Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 44545   Accepted: 18803 Des ...