如何查詢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%'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM