欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解sqlserver查询表索引

程序员文章站 2023-11-18 19:51:10
select   索引名称=a.name  ,表名=c.name  ,索引字段名=d.name  ,索引...

select   索引名称=a.name 

,表名=c.name 

,索引字段名=d.name 

,索引字段位置=d.colid 

from  sysindexes  a 
join  sysindexkeys  b  on  a.id=b.id  and  a.indid=b.indid 
join  sysobjects  c  on  b.id=c.id 
join  syscolumns  d  on  b.id=d.id  and  b.colid=d.colid 
where  a.indid  not in(0,255) 
-- and  c.xtype='u'  and  c.status>0 --查所有用户表 
and  c.name='message' --查指定表 
order by  c.name,a.name,d.name

需创建索引 例如:

根据某列判断是否有重复记录,如果该列为非主键,则创建索引

根据经常查询的列,创建索引

无须创建索引

字段内容大部分一样,例如:男,女

不要给所有的列都创建索引,这样在创建新记录时,增加维护开销时间。

oracle 查询用户表索引

select index_name,index_type,table_name from user_indexes where table_name='表名'

 sqlserver查询一个表上的索引

select 
tableid=o.[object_id],
tablename=o.name,
indexid=isnull(kc.[object_id],idx.index_id),
indexname=idx.name,
indextype=isnull(kc.type_desc,'index'),
index_column_id=idxc.index_column_id,
columnid=c.column_id,
columnname=c.name,
sort=case indexkey_property(idxc.[object_id],idxc.index_id,idxc.index_column_id,'isdescending')
when 1 then 'desc' when 0 then 'asc' else '' end,
primarykey=case when idx.is_primary_key=1 then n'√'else n'' end,
[uqique]=case when idx.is_unique=1 then n'√'else n'' end,
ignore_dup_key=case when idx.ignore_dup_key=1 then n'√'else n'' end,
disabled=case when idx.is_disabled=1 then n'√'else n'' end,
fill_factor=idx.fill_factor,
padded=case when idx.is_padded=1 then n'√'else n'' end
from sys.indexes idx 
inner join sys.index_columns idxc
on idx.[object_id]=idxc.[object_id]
and idx.index_id=idxc.index_id
left join sys.key_constraints kc
on idx.[object_id]=kc.[parent_object_id]
and idx.index_id=kc.unique_index_id
inner join sys.objects o
on o.[object_id]=idx.[object_id]
inner join sys.columns c
on o.[object_id]=c.[object_id]
and o.type='u'
and o.is_ms_shipped=0
and idxc.column_id=c.column_id where o.name='cz201' --cz201是你要查询的表

以上内容就是本文全部所述,希望大家喜欢。