Monday, October 23, 2017

Get list of all indexes in current database ----- find Index for all tables in DB

SELECT DB_NAME() AS Database_Name
, sc.name AS Schema_Name
, o.name AS Table_Name
, i.name AS Index_Name
, i.type_desc AS Index_Type
FROM sys.indexes i
INNER JOIN sys.objects o ON i.object_id = o.object_id
INNER JOIN sys.schemas sc ON o.schema_id = sc.schema_id
WHERE i.name IS NOT NULL
AND o.type = 'U'
ORDER BY o.name, i.type


Index naming convention:
PK_ for primary keys
UK_ for unique keys
IX_ for non clustered non unique indexes
UX_ for unique indexes
All of my index name take the form of
<index or key type>_<table name>_<column 1>_<column 2>_<column n>


Check index on specific table
select * from sys.indexes
where object_id = (select object_id from sys.objects where name = 'TableName')




No comments:

Post a Comment