Oracle索引

时间:2018-03-28 08:27:11

标签: oracle database-indexes

我将位图索引添加到现有表

CREATE BITMAP INDEX idx_1 ON table1 (col1) COMPUTE STATISTICS;

但是当我解释查询的计划时,优化器并没有使用新索引,并且表已完全扫描。

之后我创建了一个表的副本并创建了相同的索引,并且优化器使用了索引!

我删除并重新创建了原始表和复制表上的索引,但结果是相同的 - 原始表索引在复制表索引工作时不起作用。

当我尝试提示优化器在原始表上使用索引时,成本比全表扫描方法高3倍。

我正在使用Oracle 11gr2。

    create table AP_IT.T_LEAD_MAIN3
(
  id                NUMBER not null,
  fill_date         DATE not null,
  source_code       NUMBER default 0 not null,
  product_code      NUMBER not null,
  phone             VARCHAR2(64) not null,
  amount            NUMBER,
  amount_scored     NUMBER,
  birthdate         DATE,
  children          NUMBER,
  city_name         VARCHAR2(200),
  city_type         VARCHAR2(4),
  convenient_time   VARCHAR2(400),
  email             VARCHAR2(200),
  firstname         VARCHAR2(200),
  homephone         VARCHAR2(64),
  id_type_form      VARCHAR2(120),
  iin               VARCHAR2(48),
  lastname          VARCHAR2(200),
  marriage          VARCHAR2(4),
  middlename        VARCHAR2(200),
  name              VARCHAR2(600),
  offer_type        VARCHAR2(80),
  order_number      VARCHAR2(120),
  pensioner         VARCHAR2(4),
  period            NUMBER default 0,
  period_scored     NUMBER,
  place_to_get_loan VARCHAR2(400),
  region            VARCHAR2(12),
  rnn               VARCHAR2(48),
  salary            NUMBER,
  score_date        DATE,
  scored            NUMBER default 0,
  sent_email        DATE,
  sent_sms          DATE,
  sex               VARCHAR2(4),
  udost             VARCHAR2(36),
  udost_enddate     DATE,
  work_date         DATE,
  id_old            NUMBER(32),
  transac_type      NUMBER,
  utm_test          NVARCHAR2(100),
  utm_content       NVARCHAR2(100),
  utm_medium        NVARCHAR2(100),
  utm_name          NVARCHAR2(100),
  utm_source        NVARCHAR2(100),
  utm_term          NVARCHAR2(100),
  order_id          NUMBER,
  mm                NUMBER,
  call_start_time   VARCHAR2(10),
  call_end_time     VARCHAR2(10),
  call_date         DATE
)
tablespace RISK_DATA
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 80K
    next 1M
    minextents 1
    maxextents unlimited
  );

-- Create/Recreate primary, unique and foreign key constraints 
alter table AP_IT.T_LEAD_MAIN3
  add constraint PK_LEAD_MAIN3 primary key (ID)
  using index 
  tablespace RISK_DATA
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 80K
    next 1M
    minextents 1
    maxextents unlimited
  );
alter table AP_IT.T_LEAD_MAIN3
  add foreign key (SOURCE_CODE)
  references AP_IT.T_LEAD_SOURCE (SOURCE_CODE);
alter table AP_IT.T_LEAD_MAIN3
  add foreign key (PRODUCT_CODE)
  references AP_IT.T_LEAD_PRODUCT (PRODUCT_CODE);

CREATE BITMAP INDEX idx_tblleadmain_3
ON AP_IT.T_LEAD_MAIN3 (SCORED) COMPUTE STATISTICS;

查询:

select "AMOUNT_SCORED","ORDER_NUMBER","SCORED" 
from ap_it.t_lead_main3 t 
where t.scored = 1

规划原始表

--------------------------------------------------------------
| Id  | Operation         | Name        | Rows  | Cost (%CPU)|
--------------------------------------------------------------
|   0 | SELECT STATEMENT  |             |   573K|  4284   (1)|
|*  1 |  TABLE ACCESS FULL| T_LEAD_MAIN |   573K|  4284   (1)|
--------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("T"."SCORED"=1)

计划复制表

    --------------------------------------------------------------------------------
Plan hash value: 2628593030
-------------------------------------------------------------------------------
| Id  | Operation                    | Name              | Rows  | Cost (%CPU)|
-------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                   |   753K|  1637   (1)|
|   1 |  TABLE ACCESS BY INDEX ROWID | T_LEAD_MAIN3      |   753K|  1637   (1)|
|   2 |   BITMAP CONVERSION TO ROWIDS|                   |       |            |
|*  3 |    BITMAP INDEX SINGLE VALUE | IDX_TBLLEADMAIN_3 |       |            |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   3 - access("T"."SCORED"=1)

1 个答案:

答案 0 :(得分:0)

你已经回答了这个问题:

  

之后我创建了我的表的副本并创建了相同的索引...

创建表的副本(使用CTAS - 创建表..作为select ..)不会生成原始表的相同克隆,而是产生空间分配(例如如果原始表包含删除后的大量可用空间)和顺序或行(例如使用order by创建)可能会改变。

因此,对于原始表格,CBO计算FULL TABLE SCAN的较低费用,但对于新表格(在使用CTAS进行重组之后),优先选择位图indexx访问权限。

只有使用10053 trace跟踪您的查询时才能获得全部真相,但通常您不应期望相同(逻辑)数据具有相同的优化器行为。桌子的实际布局起着重要作用