Query to check index on a table

后端 未结 10 1705
南方客
南方客 2020-12-13 06:06

I need a query to see if a table already has any indexes on it.

相关标签:
10条回答
  • 2020-12-13 06:22

    First you check your table id (aka object_id)

    SELECT * FROM sys.objects WHERE type = 'U' ORDER BY name
    

    then you can get the column's names. For example assuming you obtained from previous query the number 4 as object_id

    SELECT c.name
    FROM sys.index_columns ic
    INNER JOIN sys.columns c ON  c.column_id = ic.column_id
    WHERE ic.object_id = 4 
    AND c.object_id = 4
    
    0 讨论(0)
  • 2020-12-13 06:23

    On SQL Server, this will list all the indexes for a specified table:

    select * from sys.indexes
    where object_id = (select object_id from sys.objects where name = 'MYTABLE')
    

    This query will list all tables without an index:

    SELECT name
    FROM sys.tables 
    WHERE OBJECTPROPERTY(object_id,'IsIndexed') = 0
    

    And this is an interesting MSDN FAQ on a related subject:
    Querying the SQL Server System Catalog FAQ

    0 讨论(0)
  • 2020-12-13 06:23

    Created a stored procedure to list indexes for a table in database in SQL Server

    create procedure _ListIndexes(@tableName nvarchar(200))
    as
    begin
    /*
    exec _ListIndexes '<YOUR TABLE NAME>'
    */
    SELECT DB_NAME(DB_ID()) as DBName,SCH.name + '.' + TBL.name AS TableName,IDX.name as IndexName, IDX.type_desc AS IndexType,COL.Name as ColumnName,IC.*
        FROM sys.tables AS TBL 
             INNER JOIN sys.schemas AS SCH ON TBL.schema_id = SCH.schema_id 
             INNER JOIN sys.indexes AS IDX ON TBL.object_id = IDX.object_id 
             INNER JOIN sys.index_columns IC ON  IDX.object_id = IC.object_id and IDX.index_id = IC.index_id 
             INNER JOIN sys.columns COL ON ic.object_id = COL.object_id and IC.column_id = COL.column_id 
            where TBL.name = @tableName
        ORDER BY TableName,IDX.name
    
    end
    
    0 讨论(0)
  • 2020-12-13 06:24

    Here is what I used for TSQL which took care of the problem that my table name could contain the schema name and possibly the database name:

    DECLARE @THETABLE varchar(100);
    SET @THETABLE = 'theschema.thetable';
    select i.*
      from sys.indexes i
     where i.object_id = OBJECT_ID(@THETABLE)
       and i.name is not NULL;
    

    The use case for this is that I wanted the list of indexes for a named table so I could write a procedure that would dynamically compress all indexes on a table.

    0 讨论(0)
  • 2020-12-13 06:32

    If you just need the indexed columns EXEC sp_helpindex 'TABLE_NAME'

    0 讨论(0)
  • 2020-12-13 06:38

    On Oracle:

    • Determine all indexes on table:

      SELECT index_name 
       FROM user_indexes
       WHERE table_name = :table
      
    • Determine columns indexes and columns on index:

      SELECT index_name
           , column_position
           , column_name
        FROM user_ind_columns
       WHERE table_name = :table
       ORDER BY index_name, column_order
      

    References:

    • ALL_IND_COLUMNS
    • ALL_INDEXES
    0 讨论(0)
提交回复
热议问题