Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index creation date from SQL server

How can I find the create date of an index. I am using SQL2008 R2. I checked sys.indexes but it does not have a create date so I joined the query with sys.objects. The thing is that the object id for an index and the table containing that index is same.

I am using this query...

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' 

Thanks!

like image 780
vinay Avatar asked Sep 28 '11 07:09

vinay


People also ask

How do I find the date of creation of an index?

There is no direct way of finding the creation date of an index. But however if you look in sysobjects there is a creation date for Primary Key or Unique Constraints. Indexes associated with primary Primary Key or Unique Constraints creation date can be known.

How do I find the indexes of a table in SQL Server?

sp_helpindex is a system stored procedure which lists the information of all the indexes on a table or view. This is the easiest method to find the indexes in a table. sp_helpindex returns the name of the index, description of the index and the name of the column on which the index was created.

How can I see all indexes in SQL Server?

You can use the sp_helpindex to view all the indexes of one table. And for all the indexes, you can traverse sys. objects to get all the indexes for each table. Only problem with this is that it only includes the index key columns, not the included columns.


2 Answers

For indexes that are constraints, then see marc_s' answer

For other indexes, you'd have to use STATS_DATE to get the creation time of the assoicated index (every index has statistics on it)

Something like (not tested)

SELECT STATS_DATE(OBJECT_ID('MyTable'),  (SELECT index_id FROM sys.indexes WHERE name = 'Index_Name')) 

This relies on the sys.indexes to sys.stats links

Edit: there is no way to find out as far as anyone can find out. Sorry.

like image 136
gbn Avatar answered Oct 10 '22 20:10

gbn


Simple query to list indexes in descending date (of statistics) order. This date is the sate of last statistics update, so is only reliable for recently created indexes.

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 
like image 41
WaitForPete Avatar answered Oct 10 '22 22:10

WaitForPete