Oracle in 查詢優化

秦曉武發表於2021-06-07

用習慣了 Mysql,今天突然遇到 Oracle 裡的 in 查詢賊慢,記錄下優化過程

參考此文章:《oracle where in 優化,如何優化 in 操作》,hint 的確很有用:
Select /*+ use_concat */ Item.* From Item Item Where Item.Item_Id in (15056,14697);
但這隻能解決單表查詢的問題

我遇到的是:

select 
  A.user_id,
  sum(B.money) as amount
from
  A inner join B on A.id = B.master_id
  inner join C on B.c_id = C.id
where
  C.category_id in (1,2,...,100)
group by A.user_id

其中涉及到 explain 直譯器,索引方式,最終結論:

select 
  A.user_id,
  sum(B.money) as amount
from
  A inner join B on A.id = B.master_id
  inner join C on B.c_id = C.id
where
  (
  C.category_id = 1
  or C.category_id = 2,
  ...
  or C.category_id = 100
  )
group by A.user_id

90秒變2秒

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章