Oracle - 如何获取索引字段等信息

时间:2011-03-23 17:58:15

标签: plsql oracle10g

如何列出Oracle中容易定义的索引列?

如何在select语句中使用这些索引?类似的东西:

SELECT x, y, z FROM a WITH INDEX(x)...

7 个答案:

答案 0 :(得分:14)

  

如何列出Oracle中易于定义的索引列

SELECT *
FROM all_ind_columns
WHERE table_name = 'YOUR_TABLE'

http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_1064.htm#i1577532

  

如何在select语句中使用这些索引

您无需做任何事情。如果索引将加速查询,Oracle将自动使用它。

答案 1 :(得分:4)

此查询还显示基于函数的索引的列表达式(如果有):

SELECT
 i.table_owner,
 i.table_name,
 i.index_name,
 i.uniqueness,
 c.column_name,
 f.column_expression
FROM      all_indexes i
LEFT JOIN all_ind_columns c
 ON   i.index_name      = c.index_name
 AND  i.owner           = c.index_owner
LEFT JOIN all_ind_expressions f
 ON   c.index_owner     = f.index_owner
 AND  c.index_name      = f.index_name
 AND  c.table_owner     = f.table_owner
 AND  c.table_name      = f.table_name
 AND  c.column_position = f.column_position
WHERE i.table_owner LIKE UPPER('%someuserpattern%')
 AND  i.table_name  LIKE UPPER('%sometablepattern%')
ORDER BY i.table_owner, i.table_name, i.index_name, c.column_position

答案 2 :(得分:1)

如果要强制使用特定索引,可以在查询中添加提示:

select /*+ index(tble_name indx_name) */
col1, col2
from tble_name

这将强制使用名为indx_name的索引。

答案 3 :(得分:1)

SELECT table_name AS TABELA,
  index_name      AS INDICE,
  column_position AS POSICAO,
  column_name     AS COLUNA
FROM dba_ind_columns
WHERE (TABLE_OWNER LIKE upper('%&proprietario.%'))
AND (table_name LIKE upper('%&tabela.%'))
ORDER BY TABELA,
  INDICE,
  POSICAO,
  COLUNA;

答案 4 :(得分:1)

作为HorseWithNoName,写道,您可以使用all_ind_columns。

无论如何,我建议使用Oracle SQL开发人员:它是一个免费的工具。您可以在GUI中获取所有索引信息。

您可以放大特定索引:

SELECT * FROM all_ind_columns
WHERE table_name = 'TABLE_NAME'
and index_name = 'INDEX_NAME'
order by Column_position

我不同意"你不必做任何事情。" 我经常把请求写成:

WHERE Trim(LastName) ='SMITH'
WHERE LastName like '%SMITH%'
WHERE trunc(CreationDate) = date'2016-09-23'

即使将LastName和CreationDate列编入索引,Oracle也无法使用它们。

写下

WHERE LastName like 'SMITH%'
WHERE CreationDate between date'2016-09-23' and date'2016-09-24'

答案 5 :(得分:0)

这有助于我显示所有索引,替换为您的详细信息:

SELECT * FROM all_ind_columns
WHERE TABLE_NAME IN 
(SELECT DISTINCT TABLE_NAME from all_indexes where owner = '<INDEX_OWNER>')
order by TABLE_NAME;

答案 6 :(得分:0)

显示表格、索引和逗号分隔的索引列列表的脚本

set pagesize 50000
set linesize 32000
col table_name format a30
col index_name format a30
col primary_key_name format a30
col uniqueness format a10
col columns format a200

select ui.table_name,
       ui.index_name,
       uc.constraint_name as primary_key_name,
       ui.uniqueness,
       listagg(aic.column_name, ', ') within group (order by aic.column_position) as columns
from user_indexes ui
inner join all_ind_columns aic on aic.index_name = ui.index_name
left outer join user_constraints uc on uc.index_name = ui.index_name and uc.constraint_type = 'P'
group by ui.table_name, ui.index_name, ui.uniqueness, uc.constraint_name
order by table_name;
TABLE_NAME                     INDEX_NAME                     PRIMARY_KEY_NAME               UNIQUENESS COLUMNS                                 
------------------------------ ------------------------------ ------------------------------ ---------- ----------------------------------------
ACTIVEMQ_ACKS                  ACTIVEMQ_ACKS_PK               ACTIVEMQ_ACKS_PK               UNIQUE     CONTAINER, CLIENT_ID, SUB_NAME, PRIORITY
ACTIVEMQ_ACKS                  ACTIVEMQ_ACKS_XIDX                                            NONUNIQUE  XID                                     
ACTIVEMQ_CONNECTION            ACTIVEMQ_CONNECTION_NAME_UK                                   UNIQUE     NAME                                    
ACTIVEMQ_CONNECTION            ACTIVEMQ_CONNECTION_PK         ACTIVEMQ_CONNECTION_PK         UNIQUE     ID                                      
ACTIVEMQ_MSGS                  ACTIVEMQ_MSGS_CIDX                                            NONUNIQUE  CONTAINER                               
ACTIVEMQ_MSGS                  ACTIVEMQ_MSGS_EIDX                                            NONUNIQUE  EXPIRATION                              
ACTIVEMQ_MSGS                  ACTIVEMQ_MSGS_MIDX                                            NONUNIQUE  MSGID_PROD, MSGID_SEQ                   
ACTIVEMQ_MSGS                  ACTIVEMQ_MSGS_PIDX                                            NONUNIQUE  PRIORITY                                
ACTIVEMQ_MSGS                  ACTIVEMQ_MSGS_PK               ACTIVEMQ_MSGS_PK               UNIQUE     ID                                      
ACTIVEMQ_MSGS                  ACTIVEMQ_MSGS_XIDX                                            NONUNIQUE  XID                                     
ACT_EVT_LOG                    SYS_C00444651                  SYS_C00444651                  UNIQUE     LOG_NR_                                 
ACT_GE_BYTEARRAY               ACT_IDX_BYTEAR_DEPL                                           NONUNIQUE  DEPLOYMENT_ID_                          
ACT_GE_BYTEARRAY               SYS_C00444634                  SYS_C00444634                  UNIQUE     ID_                                     
ACT_GE_PROPERTY                SYS_C00444632                  SYS_C00444632                  UNIQUE     NAME_                                   
ACT_HI_ACTINST                 ACT_IDX_HI_ACT_INST_END                                       NONUNIQUE  END_TIME_                               
ACT_HI_ACTINST                 ACT_IDX_HI_ACT_INST_EXEC                                      NONUNIQUE  EXECUTION_ID_, ACT_ID_                  
ACT_HI_ACTINST                 ACT_IDX_HI_ACT_INST_PROCINST                                  NONUNIQUE  PROC_INST_ID_, ACT_ID_