【问题标题】:Oracle query optimisation max aggregate not using indexOracle查询优化最大聚合不使用索引
【发布时间】:2018-04-08 00:41:38
【问题描述】:

我有一个表“AvoirStatuts”,其定义如下:

这是另外两个表的关联表,外键是'num_compte'和'id_statut'

我有一个查询(我在这里创建了一个视图)来选择所有帐户的最新状态,如下所示:

  CREATE OR REPLACE FORCE VIEW "HR"."ETATACTUELCOMPTES"  
 ("id_statut", "num_compte") AS 
      select r2."id_statut",r2."num_compte" from 
            (
              select "num_compte",max("createdAt") createdAt 
              from "AvoirStatuts" 
              group by "num_compte"
            ) r1, "AvoirStatuts" r2 
            where 
               r1."num_compte"=r2."num_compte" 
               and r1.createdAt=r2."createdAt";

我想用索引加速这个查询,所以我创建了一个这样的索引

  create index creation_index on "AvoirStatuts" 
       ("num_compte","createdAt" desc );

但是查询没有使用那个索引,我不知道为什么!

【问题讨论】:

  • “AvoirStatuts”表中有多少行?如果只有少数,优化器可能会决定读取整个表并扫描它比使用索引更快。计划中的 FULL TABLE SCAN 操作的成本仅为 5,因此很难改进。

标签: oracle indexing query-optimization


【解决方案1】:

创建这个索引:

create index covering_index on "AvoirStatuts" 
       ("num_compte","createdAt", "id_statut");

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 2021-09-03
    • 2010-10-23
    相关资源
    最近更新 更多