【问题标题】:how to reduce cost如何降低成本
【发布时间】:2022-01-16 22:09:40
【问题描述】:
select *
from inv_dist_stg(table_name)
where interface_line_id in (select interface_line_id from inv_lines_stg
                            where status = 'processed'
                              and trun(creation_date) <= (trun(sysdate)-10))

inv_dist_stg 表中的 interface_line_id 列已经有索引

【问题讨论】:

  • 您使用的是哪个 dbms? (也许是甲骨文?)
  • 欢迎来到 Stack Overflow!为了帮助您解决query-optimization 的问题,我们需要更多信息。请read this,然后edit您的问题。特别是,我们需要知道inv_dist_stg() 中的内容。
  • inv_dist_stg 是表名
  • oracle 正在使用

标签: sql oracle query-optimization


【解决方案1】:

in 会很慢,请改用连接。 查询是这样的:

select *
from inv_dist_stg a,inv_lines_stg b
where a.interface_line_id=b.interface_line_id 
and b.status = 'processed' and trun(b.creation_date) <= (trun(sysdate)-10)

statuscreation_date 上创建索引

然后你就可以走了!!!

【讨论】:

    【解决方案2】:

    您的查询看起来不错。最好您的表在interface_line_id 上有一个索引。因此,您可以快速访问您在子查询中找到的内容。

    现在您需要子查询的索引。这是一个应该包含statuscreation_date 的索引。由于我不知道哪个更有选择性,您可能想尝试两个索引并查看 DBMS 使用哪个索引。我想trun 是一个错字,实际上是trunc。您现在可以在 trunc(creation_date) 上创建索引,或者将查询更改为使用 creation_date 而不使用 trunc。最后,您希望索引包含主查询所需的 interface_line_id

    我建议

    select *
    from inv_dist_stg
    where interface_line_id in 
    (
      select interface_line_id
      from inv_lines_stg
      where status = 'processed'
      and creation_date < trunc(sysdate) - 9
    );
    

    create index idx1 on inv_dist_stg (status, creation_date, interface_line_id);
    create index idx2 on inv_dist_stg (creation_date, status, interface_line_id);
    

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 2019-08-15
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多