如何查找索引的创建日期。我正在使用SQL2008 R2
。
我检查了sys.indexes
,但它没有创建日期,因此我使用sys.objects
加入了查询。问题是索引的对象id和包含该索引的表是相同的。
我正在使用此查询...
select i.name, i.object_id, o.create_date, o.object_id, o.name
from sys.indexes i
join sys.objects o on i.object_id=o.object_id
where i.name = 'Index_Name'
谢谢!
答案 0 :(得分:14)
对于作为约束的索引,请参阅marc_s'answer
对于其他索引,您必须使用STATS_DATE来获取已分配索引的创建时间(每个索引都有统计信息)
像(未经测试)
之类的东西SELECT STATS_DATE(OBJECT_ID('MyTable'),
(SELECT index_id FROM sys.indexes WHERE name = 'Index_Name'))
这依赖于sys.stats链接的sys.indexes
编辑:就任何人都无法找到而言,无法找到答案。遗憾。
答案 1 :(得分:9)
简单查询以按降序日期(统计数据)顺序列出索引。 此日期是上次统计信息更新的状态,因此仅对最近创建的索引可靠。
select STATS_DATE(so.object_id, index_id) StatsDate
, si.name IndexName
, schema_name(so.schema_id) + N'.' + so.Name TableName
, so.object_id, si.index_id
from sys.indexes si
inner join sys.tables so on so.object_id = si.object_id
order by 1 desc
答案 2 :(得分:8)
这是一个相当长的死线程,但是SQLPanda的以下查询为我提供了Azure SQL上非聚集索引所需的信息:
SELECT OBJECT_NAME(i.object_id) AS TableName, i.object_id, i.name, i.type_desc,o.create_date, o.modify_date,o.type,i.is_disabled
FROM sys.indexes i
INNER JOIN sys.objects o ON i.object_id = o.object_id
WHERE o.type NOT IN ('S', 'IT')
AND o.is_ms_shipped = 0
AND i.name IS NOT NULL
ORDER BY modify_date DESC
归功于http://www.sqlpanda.com/2013/10/how-to-check-index-creation-date.html
我刚刚添加了修改日期,因为那是我感兴趣的信息。
答案 3 :(得分:6)
试试这个:
SELECT
i.name 'Index Name',
o.create_date
FROM
sys.indexes i
INNER JOIN
sys.objects o ON i.name = o.name
WHERE
o.is_ms_shipped = 0
AND o.type IN ('PK', 'FK', 'UQ')
object_id
是指在...上创建索引的表。
答案 4 :(得分:3)
select
crdate, i.name, object_name(o.id)
from
sysindexes i
join
sysobjects o ON o.id = i.id
where
i.name = 'My_Index_Name'
答案 5 :(得分:2)
创建PK或UK时,SQL Server会自动为该约束创建唯一索引。这些约束的create_date将与相应索引的创建日期相同。
由于sys.indexes视图没有create_date列,因此搜索此类信息绝对没用。此外,此视图中的object_id列永远不会引用相应的约束。它将指向索引所属的表。 以下测试将证明这一点:
CREATE TABLE dbo.TEST_T1
(
COLUMN_1 INT NOT NULL,
COLUMN_2 INT NOT NULL,
CONSTRAINT PK_TEST_T1 PRIMARY KEY (COLUMN_1)
)
GO
WAITFOR DELAY '00:00:01';
ALTER TABLE dbo.TEST_T1
ADD CONSTRAINT UK_TEST_T1 UNIQUE (COLUMN_2)
GO
SELECT O.name, O.object_id, O.create_date, I.object_id, I.name AS index_name
FROM sys.objects AS O
LEFT OUTER JOIN sys.indexes AS I ON O.object_id = i.object_id
WHERE O.name IN ('TEST_T1', 'PK_TEST_T1', 'UK_TEST_T1')
结果是:
name object_id create_date object_id index_name
PK_TEST_T1 272720024 2015-03-17 11:02:47.197 NULL NULL
TEST_T1 256719967 2015-03-17 11:02:47.190 256719967 PK_TEST_T1
TEST_T1 256719967 2015-03-17 11:02:47.190 256719967 UK_TEST_T1
UK_TEST_T1 288720081 2015-03-17 11:02:48.207 NULL NULL
因此,如果您想查看PK或UK索引的create_date,则无需加入sys.indexes。您应该从sys.objects中选择:
SELECT name, object_id, create_date
FROM sys.objects
WHERE name IN ('PK_TEST_T1', 'UK_TEST_T1')
AND type IN ('PK', 'UQ')
结果是:
name object_id create_date
PK_TEST_T1 272720024 2015-03-17 11:02:47.197
UK_TEST_T1 288720081 2015-03-17 11:02:48.207
答案 6 :(得分:0)
USE [YourDB Name]
SET NOCOUNT ON
DECLARE @Table_Name varchar(200)
DECLARE @Index_Name varchar(200)
DECLARE @Index_Type varchar(50)
DECLARE Indx_Cursor CURSOR
STATIC FOR
select s_tab.name as Table_Name,s_indx.name as Index_Name,s_indx.type_desc as Index_Type
from sys.indexes s_indx
inner join sys.tables s_tab on s_tab.object_id=s_indx.object_id
where s_indx.name is not null;
OPEN Indx_Cursor
IF @@CURSOR_ROWS > 0
BEGIN
FETCH NEXT FROM Indx_Cursor INTO @Table_Name,@Index_Name,@Index_Type
WHILE @@Fetch_status = 0
BEGIN
INSERT INTO INDEX_HISTORY(table_name,index_name,Index_Type,Created_date)
SELECT @Table_Name,@Index_Name,@Index_Type,STATS_DATE(OBJECT_ID(@Table_Name),
(SELECT index_id FROM sys.indexes WHERE name = @Index_Name))as Index_create_Date
FETCH NEXT FROM Indx_Cursor INTO @Table_Name,@Index_Name,@Index_Type
END
END
CLOSE Indx_Cursor
DEALLOCATE Indx_Cursor
select distinct * from index_history
但索引的主要问题是,当我们重建或重新组织索引时,索引创建日期会更改为上次重建或重组索引的日期。
答案 7 :(得分:0)
select OBJECT_NAME(object_id) AS INDEX_NAME,[name], *
from sys.indexes
Where OBJECT_NAME(object_id) NOT LIKE 'sys%' -- to exclude sys indexes
order by INDEX_NAME
这个对我有用。如果8年后神奇地起作用,请回复。 :D