简体   繁体   中英

create oracle inline view

I have the following query I am trying to optimize:

select * from table1 where header_id in 
(select b.header_id from table2 b where event_id in 
(select d.accounting_event_id from table3 d where d.invoice_id=1234 
union select aida.bc_event_id from table3 aida where ida.invoice_id=1234 
union select d.accounting_event_id from table4 d where d.invoice_id=1234
union select d.accounting_event_id from table5 d where d.check_id in (select a.check_id from table4 a where a.invoice_id=1234)
 union select d.accounting_event_id from table6 d where d.invoice_id=1234))

The problem seems to be with the nested loops on table2.
One possibility would be to create an inline view for all the unions and then join the view to table2 so that it doesn't execute the unions over and over.
How would I create such inline view? Thanks for any information.

You could try if perhaps using EXISTS instead of IN solves the Nested Loops issue:

WITH t as (
SELECT d.accounting_event_id
  FROM table3 d
 WHERE d.invoice_id = 1234
UNION ALL
SELECT aida.bc_event_id
  FROM table3 aida
 WHERE ida.invoice_id = 1234
UNION ALL
SELECT d.accounting_event_id
  FROM table4 d
 WHERE d.invoice_id = 1234
UNION ALL
SELECT d.accounting_event_id
  FROM table5 d
 WHERE d.check_id IN (SELECT a.check_id FROM table4 a WHERE a.invoice_id = 1234)
UNION ALL
SELECT d.accounting_event_id
  FROM table6 d
 WHERE d.invoice_id = 1234)
-- 
SELECT *
  FROM table1 t1
 WHERE EXISTS (SELECT 1
                 FROM table2 b
                WHERE t1.header_id = b.header_id
                  AND EXISTS (SELECT 1
                                FROM t
                               WHERE t.accounting_event_id = b.event_id))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM