0

我正在尝试编写一个仅在满足条件时提取第一(随机)行的查询。

-- Create table
create table TRANSACTIONS_SAMPLE
(
  institution_id               NUMBER(5) not null,
  id                           NUMBER(10) not null,
  partitionkey                 NUMBER(10) default 0 not null,
  cardid                       NUMBER(10),
  accountid                    NUMBER(10),
  batchid                      NUMBER(10) not null,
  amt_bill                     NUMBER(16,3),
  load_date                    DATE not null,
  trxn_date                    DATE not null,
  single_msg_flag              NUMBER(5),
  authaccounttype              VARCHAR2(2 BYTE),
  originator                   VARCHAR2(50),
  amount                       NUMBER(16,3) default 0.000 not null,
  embeddedfee                  NUMBER(16,3) default 0.000 not null,,
  valuedate                    DATE,
  startofinterest              DATE,
  minduevaluedate              DATE,
  postdate                     DATE,
  posttimestamp                DATE,
  Status                       CHAR(4 BYTE) default 'NEW' not null,
)
partition by list (PARTITIONKEY)
(
  partition 0002913151 values (1234567)
    tablespace LIVE
    pctfree 10
    initrans 16
    maxtrans 255
    storage
    (
      initial 8M
      next 1M
      minextents 1
      maxextents unlimited
    )
);

-- Create/Recreate indexes 
create index TRANSACTIONS_SAMPLEI01 on TRANSACTIONS_SAMPLE (ACCOUNTID)
  local;
create index TRANSACTIONS_SAMPLEI02 on TRANSACTIONS_SAMPLE (LOAD_DATE)
  local;
create index TRANSACTIONS_SAMPLEI03 on TRANSACTIONS_SAMPLE (BATCHID)
  local;
create index TRANSACTIONS_SAMPLEI04 on TRANSACTIONS_SAMPLE (POSTDATE)
  local;
create index TRANSACTIONS_SAMPLEI05 on TRANSACTIONS_SAMPLE (POSTTIMESTAMP)
  local;
create index TRANSACTIONS_SAMPLEI06 on TRANSACTIONS_SAMPLE (STATUS, PARTITIONKEY)
  local;
create index TRANSACTIONS_SAMPLEI07 on TRANSACTIONS_SAMPLE (CARDID, TRXN_DATE)
  local;
create unique index TRANSACTIONS_SAMPLEUI01 on TRANSACTIONS_SAMPLE (ID, PARTITIONKEY)
  local;
-- Create/Recreate primary, unique and foreign key constraints 
alter table TRANSACTIONS_SAMPLE
  add constraint TRANSACTIONS_SAMPLEPK primary key (ID, PARTITIONKEY);

--QUERY
Select * From (
Select t.AccountId From Transactions_sample t Group by t.Accountid Having Count(t.AccountId) > 10 order by dbms_random.random)
Where Rownum = 1

这个查询的问题是全表扫描。我想在不必完全访问表的情况下获得相同的结果。有任何想法吗?

谢谢

4

1 回答 1

3

如果TRANSACTIONS_SAMPLEI01您为where AccountId is not null. 但前提是您当然不想计算空值。

该列可以为空,但索引不包含空值。要包含空值计数,它必须进行全表扫描,因为它无法从索引中获取该计数。如果你有那个过滤器,优化器知道所有的账户 ID 值都必须在索引中,所以它只需要引用那个,而不是表本身。

explain plan for
Select * From (
Select t.AccountId From Transactions_sample t where AccountId is not null Group by t.Accountid Having Count(t.AccountId) > 10 order by dbms_random.random)
Where Rownum = 1;

select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT                                                                                                                                                                                      
---------------------------------------------------------------------------------------------------------------------
Plan hash value: 381125580                                                                                                                                                                              

---------------------------------------------------------------------------------------------------------------------                                                                                   
| Id  | Operation                  | Name                   | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |                                                                                   
---------------------------------------------------------------------------------------------------------------------                                                                                   
|   0 | SELECT STATEMENT           |                        |     1 |    13 |     0   (0)| 00:00:01 |       |       |                                                                                   
|*  1 |  COUNT STOPKEY             |                        |       |       |            |          |       |       |                                                                                   
|   2 |   VIEW                     |                        |     1 |    13 |     0   (0)| 00:00:01 |       |       |                                                                                   
|*  3 |    SORT ORDER BY STOPKEY   |                        |     1 |    13 |     0   (0)| 00:00:01 |       |       |                                                                                   
|*  4 |     FILTER                 |                        |       |       |            |          |       |       |                                                                                   
|   5 |      SORT GROUP BY NOSORT  |                        |     1 |    13 |     0   (0)| 00:00:01 |       |       |                                                                                   
|   6 |       PARTITION LIST SINGLE|                        |     1 |    13 |     0   (0)| 00:00:01 |     1 |     1 |                                                                                   
|*  7 |        INDEX FULL SCAN     | TRANSACTIONS_SAMPLEI01 |     1 |    13 |     0   (0)| 00:00:01 |     1 |     1 |                                                                                   
---------------------------------------------------------------------------------------------------------------------                                                                                   

Predicate Information (identified by operation id):                                                                                                                                                     
---------------------------------------------------                                                                                                                                                     

   1 - filter(ROWNUM=1)                                                                                                                                                                                 
   3 - filter(ROWNUM=1)                                                                                                                                                                                 
   4 - filter(COUNT("T"."ACCOUNTID")>10)                                                                                                                                                                
   7 - filter("ACCOUNTID" IS NOT NULL)                                                                                                                                                                  

Note                                                                                                                                                                                                    
-----                                                                                                                                                                                                   
   - dynamic sampling used for this statement (level=2)                                                                                                                                                 

或者,如果可以使该列不可为空,则不需要过滤器。

于 2015-11-10T14:41:25.840 回答