Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server query to find clustered indexes

Is it possible to write a query that returns all tables that have clustered indexes that are not based on an identity key?

like image 763
DevilDog Avatar asked Apr 15 '13 16:04

DevilDog


People also ask

How do I find the clustered index in SQL Server?

If you open the Indexes node under the table name, you will see the new index name ix_parts_id with type Clustered . When executing the following statement, SQL Server traverses the index (Clustered Index Seek) to locate the rows, which is faster than scanning the whole table.

How do I get a list of indexes 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.

What clustered index in SQL?

A clustered index is an index which defines the physical order in which table records are stored in a database. Since there can be only one way in which records are physically stored in a database table, there can be only one clustered index per table. By default a clustered index is created on a primary key column.

Where is clustered index stored in SQL Server?

Clustered indexes are stored as trees. With clustered index, the actual data is stored in the leaf nodes. This can speed up getting the data when a lookup is performed on the index.


2 Answers

How about this:

SELECT
    TableName = t.name, 
    ClusteredIndexName = i.name,
    ColumnName = c.Name
FROM
    sys.tables t
INNER JOIN 
    sys.indexes i ON t.object_id = i.object_id
INNER JOIN 
    sys.index_columns ic ON i.index_id = ic.index_id AND i.object_id = ic.object_id
INNER JOIN 
    sys.columns c ON ic.column_id = c.column_id AND ic.object_id = c.object_id
WHERE
    i.index_id = 1  -- clustered index
    AND c.is_identity = 0
    AND EXISTS (SELECT * 
                FROM sys.columns c2 
                WHERE ic.object_id = c2.object_id AND c2.is_identity = 1)

OK, this query will list those primary keys that have a column which is not identity, but where there's also additionally a second column in the primary key constraint that IS an IDENTITY column.

like image 141
marc_s Avatar answered Oct 13 '22 21:10

marc_s


SELECT  s.name AS schema_name, o.name AS object_name, i.name AS index_name
FROM    sys.indexes i
JOIN    sys.objects o ON i.object_id = o.object_id
JOIN    sys.schemas s ON o.schema_id = s.schema_id
WHERE   i.type = 1 -- Clustered index
--AND       o.is_ms_shipped = 0 -- Uncomment if you want to see only user objects
AND     NOT EXISTS (
    SELECT  * 
    FROM    sys.index_columns ic INNER JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
    WHERE   ic.object_id = i.object_id AND ic.index_id = i.index_id
    AND     c.is_identity = 1 -- Is identity column
)
ORDER BY schema_name, object_name, index_name;

Sample output (AdventureWorks2008R2):

schema_name    object_name                 index_name
-------------- --------------------------- --------------------------------------------------------------------
HumanResources Employee                    PK_Employee_BusinessEntityID
HumanResources EmployeeDepartmentHistory   PK_EmployeeDepartmentHistory_BusinessEntityID_StartDate_DepartmentID
HumanResources EmployeePayHistory          PK_EmployeePayHistory_BusinessEntityID_RateChangeDate
Person         BusinessEntityAddress       PK_BusinessEntityAddress_BusinessEntityID_AddressID_AddressTypeID
Person         BusinessEntityContact       PK_BusinessEntityContact_BusinessEntityID_PersonID_ContactTypeID
like image 30
Bogdan Sahlean Avatar answered Oct 13 '22 21:10

Bogdan Sahlean