问题
I have below query. the below query has union all twice. Due to this ,query is waiting for the same resource and wait time is too high. Below is the output of sql trace.
Elapsed times include waiting on following events:
  Event waited on                             Times   Max. Wait  Total Waited
  ----------------------------------------   Waited  ----------  ------------
  SQL*Net message to client                       1        0.00          0.00
  Disk file operations I/O                     1047        0.00          0.15
  db file sequential read                    167048        0.64       1584.33
  db file parallel read                         216        0.05          1.37
  read by other session                      520944        0.71       5113.10
  latch: cache buffers chains                 31768        0.00          1.32
  db file scattered read                         50        0.01          0.14
  wait list latch free                            1        0.00          0.00
you can see read by other session parameter. is there anyway to make below query so that this time can be reduced?
 SELECT 
 mmt . transaction_date Txn_date ,  
   GCC.SEGMENT1||'
'||GCC.SEGMENT2||'
'||GCC.SEGMENT3||'
'||GCC.SEGMENT4||'
'||GCC.SEGMENT5||'
'||GCC.SEGMENT6 C_acct_flex1 , 
 MSI.SEGMENT1 C_item_flex1 , 
 msi . description Description , 
 mtt.transaction_type_name Type ,
 mmt . transaction_source_type_id Type_id , 
 mmt . organization_id Org_id , 
 DECODE ( mmt . transaction_source_type_id ,
           1 , to_char ( mmt . transaction_source_id ) ,
           2 ,  MKTS.SEGMENT1||''||MKTS.SEGMENT2||''||MKTS.SEGMENT3 ,
           3 ,  GL.SEGMENT1||''||GL.SEGMENT2||''||GL.SEGMENT3||''||GL.SEGMENT4||''||GL.SEGMENT5||''||GL.SEGMENT6 , 
           5 , to_char ( mmt . transaction_source_id ) , 
           6 ,  MDSP.SEGMENT1 , 
           7 , to_char ( mmt . transaction_source_id ) ,
           8 ,  MKTS.SEGMENT1||''||MKTS.SEGMENT2||''||MKTS.SEGMENT3 ,
           9 , to_char ( mmt . transaction_source_id ) , 
           10 , to_char ( mmt . transaction_source_id ) , 
           11 , to_char ( mmt . transaction_source_id ) ,
           12 ,  MKTS.SEGMENT1||''||MKTS.SEGMENT2||''||MKTS.SEGMENT3 , mmt . transaction_source_name ) 
Source , 
decode ( cal . accounted_dr ,
          NULL , - 1 * ABS ( mmt . primary_quantity ) , ABS ( mmt . primary_quantity ) ) Quantity , 
msi . primary_uom_code Primary_uom , 
round ( cal . rate_or_amount * : P_EXCHANGE_RATE , : cp_ext_precision ) Cost ,
 nvl ( nvl ( cal . accounted_dr , - 1 * cal . accounted_cr ) , 0 ) * : P_EXCHANGE_RATE Value , 
 cal . ae_line_id 
FROM 
cst_ae_headers cah ,
 mtl_material_transactions mmt  , 
 mtl_item_categories mic , 
 mtl_system_items msi ,
 MTL_SALES_ORDERS mkts ,
 MTL_GENERIC_DISPOSITIONS mdsp ,
 gl_code_combinations gl  ,
 mtl_transaction_types mtt ,
 cst_ae_lines cal , 
 gl_code_combinations gcc
 WHERE 
 cah . ae_header_id = cal . ae_header_id AND 
 cah . accounting_event_id = mmt . transaction_id AND 
 cah . acct_event_source_table = 'MMT' AND 
 cal . code_combination_id = gcc . code_combination_id AND 
 mmt . inventory_item_id = msi . inventory_item_id AND
 mmt . organization_id = msi . organization_id AND
 mmt . organization_id = nvl ( mmt . owning_organization_id , mmt . organization_id ) AND 
 nvl ( mmt . owning_tp_type , 2 ) = 2 AND 
 cah . cost_group_id = : P_cost_group_id AND
 cah . cost_type_id = : P_cost_type_id AND 
 cah . period_id = : P_period_id AND  
 GCC.SEGMENT1 = '0731' AND 
 GCC.SEGMENT2 BETWEEN '75000' AND '95999' AND 
 GCC.SEGMENT3 BETWEEN '00000' AND '99999' AND 
 GCC.SEGMENT4 BETWEEN '00000' AND '99999' AND 
 GCC.SEGMENT5 = '0000' AND 
 GCC.SEGMENT6 = '00000' AND  1 = 1 AND  1 = 1  AND
 mmt.transaction_type_id = mtt.transaction_type_id AND 
 mmt.inventory_item_id = mic.inventory_item_id AND 
 mmt.organization_id = mic.organization_id AND
 mic.category_set_id = 1    AND 
 ( mmt . transaction_source_type_id = : P_source_type_id OR : P_source_type_id IS NULL ) AND 
 ( mmt . transaction_type_id = : P_txn_type_id OR : P_txn_type_id IS NULL ) AND 
 mmt . transaction_source_id = mkts . sales_order_id (+) AND 
 mmt . transaction_source_id = mdsp . disposition_id (+) AND
 mmt . transaction_source_id = gl . code_combination_id (+)
 UNION ALL 
 SELECT 
 rt . transaction_date Txn_date , 
   GCC.SEGMENT1||'
'||GCC.SEGMENT2||'
'||GCC.SEGMENT3||'
'||GCC.SEGMENT4||'
'||GCC.SEGMENT5||'
'||GCC.SEGMENT6 C_acct_flex1 ,  
  MSI.SEGMENT1 C_item_flex1 ,
  msi . description Description , 
  rt.transaction_type Type ,
  0 Type_id ,
  0 Org_id ,
  null Source ,
  decode ( rt . po_distribution_id , 
          NULL , decode ( cal . accounted_dr , NULL , - 1 * ABS ( rt . primary_quantity ) , 
          ABS ( rt . primary_quantity ) ) * POD . QUANTITY_ORDERED / POLL . QUANTITY , decode ( cal . accounted_dr , NULL , - 1 * ABS ( rt . primary_quantity ) , ABS ( rt . primary_quantity ) ) ) Quantity , msi . primary_uom_code Primary_uom , round ( cal . rate_or_amount * : P_EXCHANGE_RATE , : cp_ext_precision ) Cost , nvl ( nvl ( cal . accounted_dr , - 1 * cal . accounted_cr ) , 0 ) * : P_EXCHANGE_RATE Value , cal . ae_line_id 
FROM
 cst_ae_headers cah , 
 cst_ae_lines cal ,
 mtl_system_items_vl msi , 
 rcv_transactions rt ,
 rcv_shipment_lines rsl , 
 gl_code_combinations gcc ,
 po_distributions_all pod , 
 po_line_locations_all poll  ,
 mtl_item_categories mic
 WHERE
 pod . line_location_id = poll . line_location_id and
 poll . line_location_id = rt . po_line_location_id and 
 pod . po_distribution_id = nvl ( rt . po_distribution_id , pod . po_distribution_id ) and
 pod . po_distribution_id = cal . po_distribution_id and 
 cah . ae_header_id = cal . ae_header_id AND 
 cah . accounting_event_id = rt . transaction_id AND
 cah . acct_event_source_table = 'RT' AND 
 cal . code_combination_id = gcc . code_combination_id AND
 rt . shipment_line_id = rsl . shipment_line_id AND
 NVL ( rt . consigned_flag , 'N' ) = 'N' AND 
 rsl . item_id = msi . inventory_item_id (+) AND 
 cah . organization_id = msi . organization_id AND 
 cah . cost_group_id = : P_cost_group_id AND 
 cah . cost_type_id = : P_cost_type_id AND 
 cah . period_id = : P_period_id AND
 GCC.SEGMENT1 = '0731' AND 
 GCC.SEGMENT2 BETWEEN '75000' AND '95999' AND 
 GCC.SEGMENT3 BETWEEN '00000' AND '99999' AND
 GCC.SEGMENT4 BETWEEN '00000' AND '99999' AND 
 GCC.SEGMENT5 = '0000' AND 
 GCC.SEGMENT6 = '00000' AND  1 = 1 AND  1 = 1 AND 
 rsl.item_id = mic.inventory_item_id
          AND cah.organization_id = mic.organization_id 
          AND mic.category_set_id  = 1    and 1 = 1 
UNION ALL 
SELECT rae . transaction_date Txn_date ,  GCC.SEGMENT1||'
'||GCC.SEGMENT2||'
'||GCC.SEGMENT3||'
'||GCC.SEGMENT4||'
'||GCC.SEGMENT5||'
'||GCC.SEGMENT6 C_acct_flex1 , 
 MSI.SEGMENT1 C_item_flex1 , 
 msi . description Description , 
 decode ( : P_type_option ,
          1 , rt . source_document_code , decode ( rae . event_type_id , 18 , raet . description , 19 , raet . description , 20 , raet . description , raet . event_type_name ) ) Type ,
          0 Type_id , 0 Org_id , null Source , decode ( cal . accounted_dr , NULL , - 1 * ABS ( rae . primary_quantity ) , ABS ( rae . primary_quantity ) ) Quantity , 
msi . primary_uom_code Primary_uom ,
round ( cal . rate_or_amount * : P_EXCHANGE_RATE , : cp_ext_precision ) Cost , 
nvl ( nvl ( cal . accounted_dr , - 1 * cal . accounted_cr ) , 0 ) * : P_EXCHANGE_RATE Value ,
 cal . ae_line_id 
 FROM 
 cst_ae_headers cah ,
 cst_ae_lines cal , 
 mtl_system_items msi ,
 rcv_transactions rt , 
 rcv_accounting_events rae ,
 rcv_shipment_lines rsl ,
 gl_code_combinations gcc , 
 rcv_Accounting_event_types raet  ,
 mtl_item_categories mic 
 WHERE 
 cah . ae_header_id = cal . ae_header_id AND 
 cah . accounting_event_id = rae . accounting_event_id and 
 rae . rcv_transaction_id = rt . transaction_id AND 
 cah . acct_event_source_table = 'RAE' AND
 cal . code_combination_id = gcc . code_combination_id AND 
 rt . shipment_line_id = rsl . shipment_line_id AND 
 NVL ( rt . consigned_flag , 'N' ) = 'N' AND 
 rsl . item_id = msi . inventory_item_id (+) AND 
 cah . organization_id = msi . organization_id AND 
 cah . cost_group_id = : P_cost_group_id AND 
 cah . cost_type_id = : P_cost_type_id AND
 cah . period_id = : P_period_id AND
 raet . event_type_id = rae . event_type_id AND  
 GCC.SEGMENT1 = '0731' AND
 GCC.SEGMENT2 BETWEEN '75000' AND '95999' 
 AND GCC.SEGMENT3 BETWEEN '00000' AND '99999' 
 AND GCC.SEGMENT4 BETWEEN '00000' AND '99999' 
 AND GCC.SEGMENT5 = '0000' 
 AND GCC.SEGMENT6 = '00000' AND  1 = 1 AND  1 = 1 
 AND rsl.item_id = mic.inventory_item_id
 AND cah.organization_id = mic.organization_id 
 AND mic.category_set_id  = 1   
 ORDER BY 2 asc , 1 asc , 3 asc , 5 asc , 7 asc , 8 asc , 9 asc , 11 asc 
Is there anyway to optimize the above query? even your small suggestion would be highly helpful
来源:https://stackoverflow.com/questions/57020943/query-very-slow-the-issue-is-read-by-other-session-due-to-union-all