【问题标题】:How do you detect if there is an index for a specific column on a table in Oracle?如何检测 Oracle 表上的特定列是否存在索引?
【发布时间】:2008-11-20 21:14:52
【问题描述】:

存在其他人加载的表。我需要对表进行查询,但是缺少索引使得查询计划很糟糕。我想做的是检测是否有特定列的索引,以便如果它不存在我可以创建它,如果它已经存在则不创建它。

谢谢。

邪恶

【问题讨论】:

    标签: oracle indexing


    【解决方案1】:

    您可以查询DBA_/ALL_/USER_IND_COLUMNS,即

    SQL> SELECT index_name
      2    FROM dba_ind_columns
      3   WHERE table_owner = 'SCOTT'
      4     AND table_name  = 'EMP'
      5     AND column_name = 'EMPNO';
    
    INDEX_NAME
    ------------------------------
    PK_EMP
    

    当然,您可能想稍微扩展一下查询。这将获取 EMPNO 列出现的任何索引。您可能希望将自己限制为该列是索引的前导列的索引 (COLUMN_POSITION = 1)。或者您可能希望将自己限制为仅针对该特定列的索引(以便COLUMN_POSITION 2 中没有列),即

    SQL> ed
    Wrote file afiedt.buf
    
      1  SELECT index_name
      2    FROM dba_ind_columns a
      3   WHERE table_owner = 'SCOTT'
      4     AND table_name  = 'EMP'
      5     AND column_name = 'EMPNO'
      6     AND column_position = 1
      7     AND NOT EXISTS( SELECT 1
      8                       FROM dba_ind_columns b
      9                      WHERE a.index_owner = b.index_owner
     10                        AND a.index_name  = b.index_name
     11*                       AND b.column_position = 2)
    SQL> /
    
    INDEX_NAME
    ------------------------------
    PK_EMP
    

    【讨论】:

      【解决方案2】:

      熟悉查询 SYS 架构:

      Select * from sys.all_ind_columns where table_name=:TabName and table_owner=:TabOwner;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-14
        • 2013-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-19
        • 1970-01-01
        相关资源
        最近更新 更多