如何查询SQL Server 中 Index 的创建时间

今天有人问如何查询index的创建时间,使用下面的脚本可以查询出索引列已经创建时间,但是这个脚本只是针对,Pimary Key,Unique Index,其他 nonclustered index 是查询不到创建时间的,根本没有保存:

select
    i.name as IndexName,
    o.name as TableName,
    ic.key_ordinal as ColumnOrder,
    ic.is_included_column as IsIncluded,
    co.[name] as ColumnName,
    o.create_date
from sys.indexes i
join sys.objects o on i.object_id = o.object_id
join sys.index_columns ic on ic.object_id = i.object_id
    and ic.index_id = i.index_id
join sys.columns co on co.object_id = i.object_id
    and co.column_id = ic.column_id
where i.name like '%IX_tablename_column%'   -- input index name
--and i.[type] = 2
--and i.is_unique = 0
--and i.is_primary_key = 0
--and o.[type] = 'U'
--and ic.is_included_column = 0
order by o.[name], i.[name], ic.is_included_column, ic.key_ordinal;

 

如果需要查询索引最后一次更新的时间,使用下面的脚本:

SELECT object_schema_name(stats.object_id) AS Object_Schema_Name,
    object_name(stats.object_id) AS Object_Name,
    indexes.name AS Index_Name,
    STATS_DATE(stats.object_id, stats.stats_id) AS Stats_Last_Update  --这是从统计信息里确定更新的时间
FROM sys.stats
JOIN sys.indexes
    ON stats.object_id = indexes.object_id
    AND stats.name = indexes.name
where indexes.name like '%IX_tablename_column%'

posted @ 2019-05-14 13:33  雅槐  阅读(1514)  评论(1编辑  收藏  举报