oracle 优化SQL连接(40M到240M行之间)和case语句查询

uubf1zoe  于 2022-11-28  发布在  Oracle
关注(0)|答案(1)|浏览(131)

我正在使用Oracle SQL Developer,并且有一个使用5个输入表的查询:

  • hit 4表,具有40 M行(用于连接语句)
  • 具有240 M行的trx表(用于join语句)
  • 具有1个浮点值的avg_hits_pk表(用于case语句)
  • 具有1个浮点值的avg_hits_npk表(在case语句中使用)
  • params,即10行带参数的表(用于select as语句)

而且需要花费无数的时间。我能做些什么来优化这个查询吗?

SELECT DISTINCT
  trx.trx_id,
  hit4.customer_id,
  1 as value_pattern,
  hit4.trx_date,
  trx.trx_amount,
  role,
  tiv,
  tov,
  ratio,
  number_hits,
  CASE
      WHEN segment = 'PK' THEN round((SELECT avg_hits FROM avg_hits_pk))
      ELSE round((SELECT avg_hits FROM avg_hits_npk))
  END AS avg_hits,
  (SELECT param_value FROM params WHERE param_name = 'CSTR') as CSTR,
  trx.trx_type
FROM hit4
LEFT JOIN trx
  ON hit4.customer_id = trx.customer_id AND hit4.trx_date = trx.trx_date

已解决
我在trx.trx_date和trx.customer_id中添加了索引,从语句中删除了distinct,并过滤掉hit 4表。执行需要大约7分钟。谢谢帮助!

yqkkidmi

yqkkidmi1#

根据已经给出的建议,进一步删除SUBSELECT并查看性能如何。

SELECT 
      trx.trx_id,
      hit4.customer_id,
      1 as value_pattern,
      hit4.trx_date,
      trx.trx_amount,
      role,
      tiv,
      tov,
      ratio,
      number_hits,
      CASE
          WHEN segment = 'PK' THEN round(avg_hits_pk.avg_hits)
          ELSE round(avg_hits_npk.avg_hits)
      END AS avg_hits,
      params.param_value as CSTR,
      trx.trx_type
    FROM hit4,avg_hits_pk,avg_hits_npk,params
    LEFT JOIN trx
      ON hit4.customer_id = trx.customer_id AND hit4.trx_date = trx.trx_date
    where params.param_name='CSTR';

相关问题