存在其他人加载的表.我需要查询表,但缺少索引会使查询计划变得糟糕.我想要做的是检测是否有特定列的索引,以便我可以创建它(如果它不存在),如果它已经存在则不创建它.
谢谢.
邪恶
你可以查询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