探索SQL Server元数据(三):索引元数据

背景

在首先篇中自我介绍了何等访问元数据,元数据为何在数据库里面,以及怎么样拔取元数据。介绍了如何识破各个数据库对象的在数据库里面的名字。第二篇,我选取了触发器的主旨,因为它是一个能提供很好例子的数据库对象,并且在这么些目标中可见提议难题和化解难点。

本篇我将会介绍元数据中的索引,不仅仅是因为它们自己很关键,更器重的是它们是很好的元数据类型,比如列或者分布计算,那一个不是元数据中的对象。

目录对于其余关全面据库表都是不可或缺的。但是,就好像吐司上的黄油一样,过度施用它们或者会在数据库中暴发难点。有时,可以对表举行过度索引或缺失索引,或者营造重复索引。有时难点是选取一个坏的填写因子,错误地设置ignore_dup_key选项,创设一个永恒不会被选择(但不可以不被保安)的目录,丢失外键上的目录,或者将GUID作为主键的一部分。简单的讲,任何频仍使用的数据库系统中的索引都亟需定期维护和表明,而目录视图是马到成功这几个工作的最直白的方法之一。

都有怎么着索引可以查到?

让大家经过下边的不难语句来看一下都有如何索引在您的数据库上,代码如下:

SELECT  convert(CHAR(50),object_schema_name(t.object_ID)+'.'
    +object_name(t.object_ID)) AS 'The Table', i.name AS index_name
FROM sys.indexes AS i
  INNER JOIN sys.tables t
    ON t.object_id=i.object_id
  WHERE is_hypothetical = 0 AND i.index_id <> 0;

结果如下:

图片 1

何以要去引用sys.tables?那是因为它是有限支撑只取得用户表的最简易方法。大家拔取index_id
的values大于0,因为要是不为表创设集群索引,在sys中依然有一个条文。索引,但它指向的是堆,不意味着索引。每个表在sys中都有一行。索引值为0或1的目录。倘若该表有一个聚集索引,则有一行数据且index_id值为1;即使该表是一个堆(那只是意味该表没有聚集索引的另一种方式),则会有一行的index_id值为0。其它,无论该表是还是不是有聚集索引,每个非聚集索引都有一行,其index_id值大于1。大家过滤了的目录,那些索引是由数据库引擎优化顾问(DTA)创设的,目标唯有是测试一个或者的目录是或不是行得通。避防它们积累起来,最好把它们去掉。

假使您过一个八个指定的表,上面的这些查询是进一步合理的,要求在上边的事例中增添对象的指定:

AND t.object_id = OBJECT_ID('Production.BillOfMaterials');

 

种种表中有多少个目录,并体现他们的名字

眼前的表并不专门有用,因为无法一眼看出各类表有多少索引,以及它们是怎么样。上边这一个讲话可以兑现:

SELECT  convert(CHAR(20),object_schema_name(t.object_ID)+'.'
    +object_name(t.object_ID)) AS 'The_Table',
sum(CASE WHEN i.object_ID IS NULL THEN 0 ELSE 1 END) AS The_Count,
coalesce(stuff(( 
     SELECT ', '+i2.name
       FROM sys.indexes i2
       WHERE t.object_ID = i2.object_ID
       ORDER BY i2.name
     FOR XML PATH(''), TYPE).value(N'(./text())[1]',N'varchar(8000)'),1,2,''),'') AS Index_List
  FROM sys.tables AS t
  LEFT OUTER JOIN sys.indexes i
    ON t.object_id=i.object_id
      AND is_hypothetical = 0 AND i.index_id > 0 
GROUP BY t.Object_ID;

 

自己在老的测试数据库上进行那个测试,对象名称相比短。

The_Table            The_Count   Index_List
-------------------- ----------- --------------------------------------------------
dbo.publishers       1           UPKCL_pubind
dbo.titles           2           titleind, UPKCL_titleidind
dbo.titleauthor      3           auidind, titleidind, UPKCL_taind
dbo.stores           1           UPK_storeid
dbo.sales            2           titleidind, UPKCL_sales
dbo.roysched         1           titleidind
dbo.discounts        0           
dbo.jobs             1           PK__jobs__6E32B6A51A14E395
dbo.pub_info         1           UPKCL_pubinfo
dbo.employee         2           employee_ind, PK_emp_id
dbo.authors          2           aunmind, UPKCL_auidind

(11 row(s) affected)

查找没有聚集索引的表

有关索引,您可以找到很多诙谐的东西。例如,那里有一种高效查找表的点子,无需使用聚集索引(堆)

-- 展示所有没有聚集索引的表名称
SELECT  object_schema_name(sys.tables.object_id)+'.'
    +object_name(sys.tables.object_id) AS 'Heaps'
      FROM sys.indexes /* see whether the table is a heap */
      INNER JOIN sys.tables ON sys.tables.object_ID=sys.indexes.object_ID
      WHERE sys.indexes.type = 0;

每个索引中有多少行在表里面?

经过连日sys.partitions视图,大家得以测算出索引中大概有稍许行。我修改了有些代码,关联了sys.extended_properties,那样可以把备注的消息带出来。

--列出每个索引/堆的行数
SELECT 
  OBJECT_SCHEMA_NAME(t.object_id)+'.'+OBJECT_NAME(t.object_id) as 'Table',
  coalesce(i.NAME,'(IAM for heap)') as 'Index',
  Coalesce(
   (SELECT SUM(s.rows) FROM sys.partitions s WHERE s.object_id = i.object_id
        AND s.index_id = i.index_ID    
    ), 0) 'Rows',coalesce(ep.Value,'') as comments
 FROM sys.tables t
   INNER JOIN sys.indexes i ON i.object_id = t.object_id
   LEFT OUTER JOIN sys.Extended_Properties ep
   ON i.Object_Id = ep.Major_Id AND i.Index_Id = Minor_Id AND Class = 7;

 

图片 2

然后,你可以修改这几个代码,让其只是彰显每个在索引表中的表有多少行。

SELECT
  OBJECT_SCHEMA_NAME(t.object_id)+'.'+OBJECT_NAME(t.object_id) AS 'Table',  
  sum(rows) AS row_count
FROM sys.partitions p INNER JOIN sys.tables t 
   ON p.object_ID=t.object_ID 
WHERE index_id < 2 GROUP BY t.object_ID,Index_ID;

表中都有好多索引吗?

只要您对一些表具有大量索引感到疑虑,那么可以选拔下边查询,该查询告诉您具有当先4个目录和索引计数当先列计数一半的表。它是一种任意拔取具有大批量目录的表的措施。

--超过4个索引的表 
--索引个数超过列数一半
SELECT object_schema_name(TheIndexes.Object_ID) + '.'+ object_name(TheIndexes.Object_ID) AS TableName,
       Columns, Indexes 
 FROM 
   (SELECT count(*) AS indexes, t.object_ID
      FROM sys.indexes i
      INNER JOIN sys.tables t
        ON i.object_ID=t.object_ID 
    GROUP BY t.object_ID) TheIndexes
 INNER JOIN
  (SELECT count(*) AS columns, t.object_ID
     FROM sys.columns c
       INNER JOIN sys.tables t
     ON c.object_ID=t.object_ID 
   GROUP BY t.object_ID)TheColumns
 ON TheIndexes.object_ID=TheColumns.object_ID
 WHERE indexes>columns/2 AND indexes>4;

 

询问更新过的索引缺没有应用过有如何?

连天有要求找出自上次启动服务器来说未曾行使的目录,越发是一旦服务器平昔在做种种各个的干活时。

--Indexes updated but not read.
SELECT
    object_schema_name(i.Object_ID) + '.'+ object_name(i.Object_ID) as Thetable,
    i.name    AS 'Index'
  FROM sys.indexes i
    left outer join sys.dm_db_index_usage_stats s 
      ON s.object_id = i.object_id
       AND s.index_id = i.index_id
       AND s.database_id = DB_ID()
  WHERE OBJECTPROPERTY(i.object_id, 'IsUserTable') = 1
    AND i.index_id > 0  --Exclude heaps.
    AND i.is_primary_key = 0 --and Exclude primary keys.
    AND i.is_unique = 0    --and Exclude unique constraints.
    AND coalesce(s.user_lookups + s.user_scans + s.user_seeks,0) = 0 --No user reads.
    AND coalesce(s.user_updates,0) > 0; --Index is being updated.

 

留意:我已经在代码里选择了动态管理视图sys.dm_db_index_usage_stats,那里起到了手机使用新闻的法力,之后大家会更详尽的采用换那么些目标来注解其成效。

那一个索引占用了多少空间?

若是打算知道索引占了有点空间,有诸多‘胖’索引,就是富含了诸多列,有可能索引中部分列不会师世在其余查询中,那就是荒废了空间。

SELECT 
  object_schema_name(i.Object_ID) + '.'+ object_name(i.Object_ID) AS Thetable,
  coalesce(i.name,'heap IAM')    AS 'Index',
  convert(DECIMAL(9,2),(sum(a.total_pages) * 8.00) / 1024.00)    AS 'Index_MB'
FROM sys.indexes i
INNER JOIN sys.partitions p 
  ON i.object_id = p.object_id
    AND i.index_id = p.index_id
INNER JOIN sys.allocation_units a 
  ON p.partition_id = a.container_id
  WHERE objectproperty(i.object_id, 'IsUserTable') = 1
GROUP BY i.object_id, i.index_id, i.name;

 

图片 3

算算表总的目录空间

让大家看看每个表的总索引空间,以及表中的行数。

SELECT 
  object_schema_name(i.Object_ID) + '.'+ object_name(i.Object_ID) AS Thetable,
  convert(DECIMAL(9,2),(sum(a.total_pages) * 8.00) / 1024.00)    AS 'Index_MB',
  max(row_count) AS 'Rows',
  count(*) AS Index_count
FROM sys.indexes i
INNER JOIN
  (SELECT object_ID,Index_ID, sum(rows) AS Row_count 
     FROM sys.partitions GROUP BY object_ID,Index_ID)f
  ON f.object_ID=i.object_ID AND f.index_ID=i.index_ID
INNER JOIN sys.partitions p 
  ON i.object_id = p.object_id
    AND i.index_id = p.index_id
INNER JOIN sys.allocation_units a 
  ON p.partition_id = a.container_id
  WHERE objectproperty(i.object_id, 'IsUserTable') = 1
GROUP BY i.object_id;

图片 4

怎么着查询表使用索引的各个方法?

意识有关索引的少数质量,平日最好利用性质函数作为急迅格局。

-- 查询没有主键的表
SELECT  object_schema_name(object_id)+'.'+object_name(object_id) as No_Primary_key
  FROM sys.tables/* see whether the table has a primary key */
  WHERE objectproperty(OBJECT_ID,'TableHasPrimaryKey') = 0;


-- 查询没有索引的表
SELECT  object_schema_name(object_id)+'.'+object_name(object_id) as No_Indexes  
  FROM sys.tables /* see whether the table has any index */
  WHERE objectproperty(OBJECT_ID,'TableHasIndex') = 0;


-- )查询没有候选键的表
SELECT  object_schema_name(object_id)+'.'+object_name(object_id) as No_Candidate_Key
  FROM sys.tables/* if no unique constraint then it isn't relational */
  WHERE objectproperty(OBJECT_ID,'TableHasUniqueCnst') = 0
    AND   objectproperty(OBJECT_ID,'TableHasPrimaryKey') = 0;


--查询带有禁用索引的表
SELECT  distinct
  object_schema_name(object_id)+'.'+object_name(object_id) as Has_Disabled_indexes
  FROM sys.indexes /* don't leave these lying around */
  WHERE is_disabled=1;

那么些是指标,那一个不是?

您也许注意到了一些出人意料的事情。即便表的一部分特性(如主键)本身就是目的,但列、总括或索引并非对象。让我们弄领会那或多或少,因为它不是截然直观的展示在sys.objects,您能够找到关于所有国有数据库组件的骨干标准音信,如表、视图、同义词、外键、检查约束、键约束、默许约束、服务队列、触发器和进程。我列出的所有那一个零部件都有其余性能,这个属性必须通过持续相关大旨属性的视图可知,但也包蕴与对象相关的数据列。最好利用那个非凡的视图,因为它们有你须要的具备音讯,系统只过滤您感兴趣的目的类型,比如表。各个对象(如约束和触发器)在sys.objects中都有parent_ID,非零的靶子表,展现它们是子对象。

上面的询问向您出示了一种查看那个子对象并将其与父母关系的简短方法。

--查询索引父对象(表名)和索引名称
SELECT parent.name AS Parents_name, 
       child.name AS Childs_Name, 
       replace(lower(parent.type_desc),'_',' ') AS Parents_type, 
       replace(lower(child.type_desc),'_',' ') AS Childs_type
FROM sys.objects child
  INNER JOIN sys.objects parent
    ON parent.object_ID=child.parent_object_id
WHERE child.parent_object_id<>0
ORDER BY parents_name;

 

图片 5.

您会发现索引不是目标。在第四个查询中,重返的object_ID是定义索引的表的ID。

这边的难点是关系是扑朔迷离的。约束可以蕴含多少个列,也能够由索引强制。索引可以分包多少个列,可是各类很关键。计算数据还足以涵盖几个列,也足以与索引相关联。那意sys.indexes,
sys.stats and
sys.columns不从sys.objects继承。参数和档次也是这么。

如何查询每一个表的每一个索引的每一个列?

最简便的询问艺术如下:

SELECT object_schema_name(t.object_ID)+'.'+t.name AS The_Table, --the name of the table
       i.name AS The_Index,  -- its index
       index_column_id,
       col_name(Ic.Object_Id, Ic.Column_Id) AS The_Column --the column
FROM sys.tables t
INNER JOIN sys.indexes i
    ON t.object_ID=i.object_ID
INNER JOIN sys.Index_columns  ic
    ON i.Object_ID=ic.Object_ID
    AND i.index_ID=ic.index_ID
ORDER BY t.name,i.index_id, index_column_id;

 

 

图片 6

理所当然也可以指定特定表,例如:

  WHERE i.object_id = OBJECT_ID('Production.BillOfMaterials');

目录中有哪些列,顺序又是何等 ?

也足以集中下边语句,每个索引汇总成一行,浮现所有索引,具体代码如下:

SELECT object_schema_name(t.object_ID)+'.'+t.name AS The_Table, --the name of the table
   coalesce(stuff (--get a list of indexes
     (SELECT ', '+i.name
     +' ( '
         +stuff (--get a list of columns
         (SELECT ', ' + col_name(Ic.Object_Id, Ic.Column_Id) 
         FROM  sys.Index_columns  ic
         WHERE ic.Object_ID=i.Object_ID
         AND ic.index_ID=i.index_ID
         ORDER BY index_column_ID ASC
         FOR XML PATH(''), TYPE).value('.', 'varchar(max)'),1,2,'') +' )'
     FROM sys.indexes i 
     WHERE i.object_ID=t.object_ID
     FOR XML PATH(''), TYPE).value('.', 'varchar(max)'),1,2,''),'') AS Indexes
 FROM sys.tables t;

职能如下:

图片 7

怎么查询XML索引?

XML索引被视为索引的扩展。我发觉查看其细节的最好措施是为它们创设一个CREATE语句。

SELECT 'CREATE' + case when secondary_type is null then ' PRIMARY' else '' end
 + ' XML INDEX '+coalesce(xi.name,'')+ '  
    ON ' --what table and column is this XML index on?
 + object_schema_name(ic.Object_ID)+'.'+object_name(ic.Object_ID)
 +' ('+col_name(Ic.Object_Id, Ic.Column_Id)+' )  
    '+ coalesce('USING XML INDEX [' + Using.Name + '] FOR ' + Secondary_Type_DeSc
     COLLATE database_default,'')    
    +'  
'+      replace('WITH ( ' + 
   stuff(
  CASE WHEN xi.Is_Padded <> 0 THEN ', PAD_INDEX  = ON ' ELSE '' END 
  + CASE 
     WHEN xi.Fill_Factor NOT IN (0, 100) 
        THEN ', FILLFACTOR  =' + convert(VARCHAR(3), xi.Fill_Factor) + ''
        ELSE '' END 
  + CASE WHEN xi.Ignore_dUp_Key <> 0 THEN ', IGNORE_DUP_KEY = ON' ELSE '' END 
  + CASE WHEN xi.Allow_Row_Locks = 0 THEN ', ALLOW_ROW_LOCKS  = OFF' ELSE '' END 
  + CASE WHEN xi.Allow_Page_Locks = 0 THEN ', ALLOW_PAGE_LOCKS  = OFF' ELSE ' ' END
   , 1, 1, '')
 + ')', 'WITH ( )', '') --create the list of xml index options
+  coalesce('/* '+convert(varchar(8000),Value)+ '*/','')--and any comment
    AS BuildScript
FROM sys.xml_Indexes xi
      inner join sys.index_columns ic 
   ON ic.Index_Id = xi.Index_Id
   AND ic.Object_Id = xi.Object_Id   
  LEFT OUTER JOIN sys.Indexes [USING]
   ON [USING].Index_Id = xi.UsIng_xml_Index_Id
   AND [USING].Object_Id = xi.Object_Id
  LEFT OUTER JOIN sys.Extended_Properties ep
   ON ic.Object_Id = ep.Major_Id AND ic.Index_Id = Minor_Id AND Class = 7
WHERE object_schema_name(ic.Object_ID) <>'sys' AND ic.index_id>0;

下面的询问结果将显得所有骨干的XML索引细节作为打造脚本。

图片 8

元数据中还有其它门类的目录吗?

还有三种比较很是的目录,一是空间引得,其音讯在sys.spatial_index_tessellations

sys.spatial_indexes表中。另一个是全文索引,其音信在fulltext_index_fragments,fulltext_index_catalog_usages, fulltext_index_columns
fulltext_indexes表中保存。**

追究索引统计音信

明日,让大家探究一下分布总括数据或“stats”。每个索引都有一个叠加的计算对象,以便查询优化器可以提供一个适当的询问布置。为此,它须求推断数据的“基数”,以确定为其余索引值再次回到多少行,并行使这几个“stats”对象告诉它多少是何等分布的。

可以查询总计音信目标是什么样与表展开关联的,语句如下:

SELECT object_schema_name(t.Object_ID) + '.'+ t.name AS The_table, 
    stats.name AS Stats_Name, sys.columns.name AS Column_Name
  FROM sys.stats
 INNER JOIN sys.stats_columns
    ON stats.object_id = stats_columns.object_id
   AND stats.stats_id = stats_columns.stats_id
 INNER JOIN sys.columns
    ON stats_columns.object_id = columns.object_id
   AND stats_columns.column_id = columns.column_id
 INNER JOIN sys.tables t
    ON stats.object_id = t.object_id;

 

当它们与索引相关联时,总结数据继承索引的称号,并采用与索引相同的列。

图片 9

自我批评重复的计算信息

通过相比与各种计算新闻相关联的列号列表,您可以很快查看同一列或一组列是或不是有四个计算音讯。

SELECT object_schema_name(Object_ID)+'.'+object_name(Object_ID) as tableName,
       count(*) as Similar, ColumnList as TheColumn, 
       max(name)+', '+min(name) as duplicates
FROM 
   (SELECT Object_ID, name,   
     stuff (--get a list of columns
         (SELECT ', ' + col_name(sc.Object_Id, sc.Column_Id)
         FROM  sys.stats_columns  sc
         WHERE sc.Object_ID=s.Object_ID
         AND sc.stats_ID=s.stats_ID
         ORDER BY stats_column_ID ASC
         FOR XML PATH(''), TYPE).value('.', 'varchar(max)'),1,2,'') AS ColumnList
   FROM sys.stats s)f
GROUP BY Object_ID,ColumnList 
HAVING count(*) >1;

结果如下:

图片 10

显示了含蓄重复的计算对象,在本例中是sales.customer表在AccountNumber列上有七个类似的计算对象。

总结

 在数据库中有不胜枚举有价值的音信都在目录上。一旦表的数据变大,很不难让表出现有的题目,比如无意中没有聚集索引或主键,或者有重新的目录或不要求的计算新闻等。大家通过操纵如何查询那些索引的动态视图后可以快速查询定位使用表的音信,方便我们预防和化解那类难题,那一个基础艺术已经在DBA和数据库开发的行事中变得进一步首要了,

相关文章