Query performance issue when adding UNION to CTE/Query

I have the below CTE/Query running on Oracle database.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>WITH Txn_Cost as
(SELECT * FROM
(SELECT A.INVENTORY_ITEM_ID
,C.SUBINVENTORY_CODE,
TO_CHAR(A.REC_TRXN_ID) REC_TRXN_ID,
TO_CHAR(B.TRANSACTION_ID) TRANSACTION_ID,
TO_CHAR(B.COST_DATE) COST_DATE,
TO_CHAR(A.QUANTITY_ONHAND) QUANTITY_ONHAND,
B.TRANSACTION_COST
FROM CST_ONHAND_V A,
CST_ITEM_COST_HISTORY_V B,
CST_TXN_LAYER_DTLS_V C
WHERE 1=1
AND A.INVENTORY_ITEM_ID= :p_inv_num
AND A.REC_TRXN_ID=B.TRANSACTION_ID
AND A.COST_ORG_ID = B.COST_ORG_ID
AND A.COST_BOOK_ID = B.COST_BOOK_ID
AND A.INVENTORY_ITEM_ID=B.INVENTORY_ITEM_ID
--AND B.COST_DATE=(SELECT MAX(COST_DATE) FROM CST_ITEM_COST_HISTORY_V B_COST WHERE
--B_COST.INVENTORY_ITEM_ID = B.INVENTORY_ITEM_ID
--TRANSACTION_ID=B.TRANSACTION_ID
--)
-- AND B.TRANSACTION_ID = C.TRANSACTION_ID
AND A.REC_TRXN_ID=C.REC_TRXN_ID
--AND C.SUBINVENTORY_CODE = IOQD.secondary_inventory
ORDER BY B.COST_DATE DESC NULLS LAST
)
WHERE ROWNUM = 1
)
Select * from
((
SELECT item_number,item_DESCRIPTION
,decode(substr(subinventory_code,1,3),'11C','11CCL'
,'11O','11OPR'
,'18O','18OPR'
,'41S','24000'
,'41O','41OPR'
,'60O','60OPR'
,'70O','70OPR'
,'70C','70CCL'
,subinventory_code
) subinventory_code
,p_inv,
round(Cost,5) Cost,Patient_Chargeable,Chargeable_code,
patient_charges,STOCK_ENABLED_FLAG,INV_LOCATOR ,rownum ivt_seqnum
, CONSIGNED_FLAG
FROM (
SELECT DISTINCT ESI.item_number,
replace(replace(ESI.LONG_DESCRIPTION,chr(10)),chr(13)) item_DESCRIPTION,
IOQD.subinventory_code,
IOQD.inventory_item_id p_inv,
TC.TRANSACTION_COST Cost,
ESI.attribute5
Patient_Chargeable,
ESI.attribute2
Chargeable_code,
ESI.attribute3
patient_charges,
ESI.STOCK_ENABLED_FLAG,
(select SEGMENT1||'-'||SEGMENT2||'-'||SEGMENT3||'-'||SEGMENT4||'-'||SEGMENT5
from inv_item_locations
where inventory_location_id= ioqd.locator_id and length(Segment1) < 3
and length(Segment1) >= 1
) INV_LOCATOR,
CASE WHEN ESI.CONSIGNED_FLAG = '1' THEN 'Y'
WHEN ESI.CONSIGNED_FLAG = '2' THEN 'N'
END AS CONSIGNED_FLAG
FROM inv_onhand_quantities_detail IOQD,
egp_system_items ESI,
Txn_Cost TC
WHERE 1 = 1
AND IOQD.inventory_item_id = ESI.inventory_item_id
AND IOQD.organization_id = ESI.organization_id
AND ESI.inventory_item_id = :p_inv_num
AND TC.subinventory_code(+) = IOQD.subinventory_code
AND IOQD.subinventory_code IN (select distinct SECONDARY_INVENTORY_NAME from inv_secondary_inventories where attribute1='Y' ))
) --)
UNION ALL
(SELECT DISTINCT ESI.item_number,
replace(replace(ESI.LONG_DESCRIPTION,chr(10)),chr(13)) item_DESCRIPTION
-- IOQD.secondary_inventory subinventory_code,
,decode(substr(IOQD.secondary_inventory,1,3),'11C','11CCL'
,'11O','11OPR'
,'18O','18OPR'
,'41S','24000'
,'41O','41OPR'
,'60O','60OPR'
,'70O','70OPR'
,'70C','70CCL'
,IOQD.secondary_inventory
) subinventory_code,
IOQD.inventory_item_id
p_inv,
TC.TRANSACTION_COST Cost,
ESI.attribute5
Patient_Chargeable,
ESI.attribute2
Chargeable_code,
ESI.attribute3
patient_charges,
ESI.STOCK_ENABLED_FLAG,
case when
(SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
FROM inv_item_loc_defaults iild,
inv_item_locations iil
WHERE iild.inventory_item_id = esi.inventory_item_id
AND iild.subinventory_code = IOQD.secondary_inventory
AND iild.locator_id = iil.inventory_location_id
AND rownum= 1 and length(Segment1) >= 1 ) is not null
then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
FROM inv_item_loc_defaults iild,
inv_item_locations iil
WHERE iild.inventory_item_id = esi.inventory_item_id
AND iild.subinventory_code = IOQD.secondary_inventory
AND iild.locator_id = iil.inventory_location_id
AND rownum= 1 and length(Segment1) >= 1 )
when (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
FROM inv_secondary_locators iild,
inv_item_locations iil
WHERE iild.inventory_item_id = esi.inventory_item_id
AND iild.subinventory_code = IOQD.secondary_inventory
AND iild.SECONDARY_LOCATOR= iil.inventory_location_id
AND rownum = 1 and length(Segment1) >= 1 ) is not null
Then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
FROM inv_secondary_locators iild,
inv_item_locations iil
WHERE iild.inventory_item_id = esi.inventory_item_id
AND iild.subinventory_code = IOQD.secondary_inventory
AND iild.SECONDARY_LOCATOR= iil.inventory_location_id
AND rownum = 1 and length(Segment1) >= 1 )
Else 'AAAAAAA'
END as INV_LOCATOR,
rownum ivt_seqnum
, CASE WHEN ESI.CONSIGNED_FLAG = '1' THEN 'Y'
WHEN ESI.CONSIGNED_FLAG = '2' THEN 'N'
END AS CONSIGNED_FLAG
FROM INV_ITEM_SUB_INVENTORIES IOQD,
egp_system_items ESI,
Txn_Cost TC
WHERE 1 = 1
AND IOQD.inventory_item_id = ESI.inventory_item_id
AND IOQD.organization_id = ESI.organization_id
AND ESI.inventory_item_id = :p_inv_num
AND IOQD.secondary_inventory IN (select distinct SECONDARY_INVENTORY_NAME from inv_secondary_inventories where attribute1='Y')
AND NOT EXISTS (select 1
FROM inv_onhand_quantities_detail
WHERE subinventory_code = IOQD.secondary_inventory
AND inventory_item_id = ESI.inventory_item_id
)
AND TC.subinventory_code(+) = IOQD.secondary_inventory
) )results
order by subinventory_code, INV_LOCATOR
</code>
<code>WITH Txn_Cost as (SELECT * FROM (SELECT A.INVENTORY_ITEM_ID ,C.SUBINVENTORY_CODE, TO_CHAR(A.REC_TRXN_ID) REC_TRXN_ID, TO_CHAR(B.TRANSACTION_ID) TRANSACTION_ID, TO_CHAR(B.COST_DATE) COST_DATE, TO_CHAR(A.QUANTITY_ONHAND) QUANTITY_ONHAND, B.TRANSACTION_COST FROM CST_ONHAND_V A, CST_ITEM_COST_HISTORY_V B, CST_TXN_LAYER_DTLS_V C WHERE 1=1 AND A.INVENTORY_ITEM_ID= :p_inv_num AND A.REC_TRXN_ID=B.TRANSACTION_ID AND A.COST_ORG_ID = B.COST_ORG_ID AND A.COST_BOOK_ID = B.COST_BOOK_ID AND A.INVENTORY_ITEM_ID=B.INVENTORY_ITEM_ID --AND B.COST_DATE=(SELECT MAX(COST_DATE) FROM CST_ITEM_COST_HISTORY_V B_COST WHERE --B_COST.INVENTORY_ITEM_ID = B.INVENTORY_ITEM_ID --TRANSACTION_ID=B.TRANSACTION_ID --) -- AND B.TRANSACTION_ID = C.TRANSACTION_ID AND A.REC_TRXN_ID=C.REC_TRXN_ID --AND C.SUBINVENTORY_CODE = IOQD.secondary_inventory ORDER BY B.COST_DATE DESC NULLS LAST ) WHERE ROWNUM = 1 ) Select * from (( SELECT item_number,item_DESCRIPTION ,decode(substr(subinventory_code,1,3),'11C','11CCL' ,'11O','11OPR' ,'18O','18OPR' ,'41S','24000' ,'41O','41OPR' ,'60O','60OPR' ,'70O','70OPR' ,'70C','70CCL' ,subinventory_code ) subinventory_code ,p_inv, round(Cost,5) Cost,Patient_Chargeable,Chargeable_code, patient_charges,STOCK_ENABLED_FLAG,INV_LOCATOR ,rownum ivt_seqnum , CONSIGNED_FLAG FROM ( SELECT DISTINCT ESI.item_number, replace(replace(ESI.LONG_DESCRIPTION,chr(10)),chr(13)) item_DESCRIPTION, IOQD.subinventory_code, IOQD.inventory_item_id p_inv, TC.TRANSACTION_COST Cost, ESI.attribute5 Patient_Chargeable, ESI.attribute2 Chargeable_code, ESI.attribute3 patient_charges, ESI.STOCK_ENABLED_FLAG, (select SEGMENT1||'-'||SEGMENT2||'-'||SEGMENT3||'-'||SEGMENT4||'-'||SEGMENT5 from inv_item_locations where inventory_location_id= ioqd.locator_id and length(Segment1) < 3 and length(Segment1) >= 1 ) INV_LOCATOR, CASE WHEN ESI.CONSIGNED_FLAG = '1' THEN 'Y' WHEN ESI.CONSIGNED_FLAG = '2' THEN 'N' END AS CONSIGNED_FLAG FROM inv_onhand_quantities_detail IOQD, egp_system_items ESI, Txn_Cost TC WHERE 1 = 1 AND IOQD.inventory_item_id = ESI.inventory_item_id AND IOQD.organization_id = ESI.organization_id AND ESI.inventory_item_id = :p_inv_num AND TC.subinventory_code(+) = IOQD.subinventory_code AND IOQD.subinventory_code IN (select distinct SECONDARY_INVENTORY_NAME from inv_secondary_inventories where attribute1='Y' )) ) --) UNION ALL (SELECT DISTINCT ESI.item_number, replace(replace(ESI.LONG_DESCRIPTION,chr(10)),chr(13)) item_DESCRIPTION -- IOQD.secondary_inventory subinventory_code, ,decode(substr(IOQD.secondary_inventory,1,3),'11C','11CCL' ,'11O','11OPR' ,'18O','18OPR' ,'41S','24000' ,'41O','41OPR' ,'60O','60OPR' ,'70O','70OPR' ,'70C','70CCL' ,IOQD.secondary_inventory ) subinventory_code, IOQD.inventory_item_id p_inv, TC.TRANSACTION_COST Cost, ESI.attribute5 Patient_Chargeable, ESI.attribute2 Chargeable_code, ESI.attribute3 patient_charges, ESI.STOCK_ENABLED_FLAG, case when (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5 FROM inv_item_loc_defaults iild, inv_item_locations iil WHERE iild.inventory_item_id = esi.inventory_item_id AND iild.subinventory_code = IOQD.secondary_inventory AND iild.locator_id = iil.inventory_location_id AND rownum= 1 and length(Segment1) >= 1 ) is not null then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5 FROM inv_item_loc_defaults iild, inv_item_locations iil WHERE iild.inventory_item_id = esi.inventory_item_id AND iild.subinventory_code = IOQD.secondary_inventory AND iild.locator_id = iil.inventory_location_id AND rownum= 1 and length(Segment1) >= 1 ) when (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5 FROM inv_secondary_locators iild, inv_item_locations iil WHERE iild.inventory_item_id = esi.inventory_item_id AND iild.subinventory_code = IOQD.secondary_inventory AND iild.SECONDARY_LOCATOR= iil.inventory_location_id AND rownum = 1 and length(Segment1) >= 1 ) is not null Then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5 FROM inv_secondary_locators iild, inv_item_locations iil WHERE iild.inventory_item_id = esi.inventory_item_id AND iild.subinventory_code = IOQD.secondary_inventory AND iild.SECONDARY_LOCATOR= iil.inventory_location_id AND rownum = 1 and length(Segment1) >= 1 ) Else 'AAAAAAA' END as INV_LOCATOR, rownum ivt_seqnum , CASE WHEN ESI.CONSIGNED_FLAG = '1' THEN 'Y' WHEN ESI.CONSIGNED_FLAG = '2' THEN 'N' END AS CONSIGNED_FLAG FROM INV_ITEM_SUB_INVENTORIES IOQD, egp_system_items ESI, Txn_Cost TC WHERE 1 = 1 AND IOQD.inventory_item_id = ESI.inventory_item_id AND IOQD.organization_id = ESI.organization_id AND ESI.inventory_item_id = :p_inv_num AND IOQD.secondary_inventory IN (select distinct SECONDARY_INVENTORY_NAME from inv_secondary_inventories where attribute1='Y') AND NOT EXISTS (select 1 FROM inv_onhand_quantities_detail WHERE subinventory_code = IOQD.secondary_inventory AND inventory_item_id = ESI.inventory_item_id ) AND TC.subinventory_code(+) = IOQD.secondary_inventory ) )results order by subinventory_code, INV_LOCATOR </code>
WITH Txn_Cost as
(SELECT * FROM 
                 (SELECT A.INVENTORY_ITEM_ID
                          ,C.SUBINVENTORY_CODE,
                          TO_CHAR(A.REC_TRXN_ID) REC_TRXN_ID,
                          TO_CHAR(B.TRANSACTION_ID) TRANSACTION_ID,
                          TO_CHAR(B.COST_DATE) COST_DATE,
                          TO_CHAR(A.QUANTITY_ONHAND) QUANTITY_ONHAND,
                          B.TRANSACTION_COST
                      FROM CST_ONHAND_V A,
                           CST_ITEM_COST_HISTORY_V B,
                           CST_TXN_LAYER_DTLS_V C
                     WHERE 1=1
                       AND A.INVENTORY_ITEM_ID= :p_inv_num
                       AND A.REC_TRXN_ID=B.TRANSACTION_ID
                   
                       AND A.COST_ORG_ID = B.COST_ORG_ID
                       AND A.COST_BOOK_ID = B.COST_BOOK_ID
                       AND A.INVENTORY_ITEM_ID=B.INVENTORY_ITEM_ID
                       --AND B.COST_DATE=(SELECT MAX(COST_DATE) FROM CST_ITEM_COST_HISTORY_V B_COST WHERE 
                       --B_COST.INVENTORY_ITEM_ID = B.INVENTORY_ITEM_ID
                       --TRANSACTION_ID=B.TRANSACTION_ID
                       --)
                     --  AND B.TRANSACTION_ID = C.TRANSACTION_ID
                       AND A.REC_TRXN_ID=C.REC_TRXN_ID
                    --AND C.SUBINVENTORY_CODE = IOQD.secondary_inventory     
    ORDER BY B.COST_DATE DESC NULLS LAST
    )
    WHERE ROWNUM = 1    

)
        
Select * from 
(( 
SELECT item_number,item_DESCRIPTION   
      ,decode(substr(subinventory_code,1,3),'11C','11CCL'
              ,'11O','11OPR'
              ,'18O','18OPR'
              ,'41S','24000'
              ,'41O','41OPR'
              ,'60O','60OPR'
              ,'70O','70OPR'
              ,'70C','70CCL'
              ,subinventory_code
             ) subinventory_code
      ,p_inv,
       round(Cost,5) Cost,Patient_Chargeable,Chargeable_code,
       patient_charges,STOCK_ENABLED_FLAG,INV_LOCATOR ,rownum ivt_seqnum 
       ,               CONSIGNED_FLAG
FROM (
SELECT DISTINCT ESI.item_number,
                replace(replace(ESI.LONG_DESCRIPTION,chr(10)),chr(13))                  item_DESCRIPTION,
                IOQD.subinventory_code,
                IOQD.inventory_item_id                 p_inv,
                TC.TRANSACTION_COST   Cost,
                ESI.attribute5
                Patient_Chargeable,
                ESI.attribute2
                Chargeable_code,
                ESI.attribute3
                patient_charges,
                ESI.STOCK_ENABLED_FLAG,
(select SEGMENT1||'-'||SEGMENT2||'-'||SEGMENT3||'-'||SEGMENT4||'-'||SEGMENT5
                   from inv_item_locations
                  where inventory_location_id= ioqd.locator_id and length(Segment1) < 3
and length(Segment1) >= 1
                ) INV_LOCATOR,
        CASE WHEN ESI.CONSIGNED_FLAG = '1' THEN 'Y'
                           WHEN ESI.CONSIGNED_FLAG = '2' THEN 'N'
                          END AS CONSIGNED_FLAG
FROM   inv_onhand_quantities_detail IOQD,
       egp_system_items ESI, 
       Txn_Cost TC
WHERE  1 = 1
       AND IOQD.inventory_item_id = ESI.inventory_item_id
       AND IOQD.organization_id = ESI.organization_id
       AND ESI.inventory_item_id = :p_inv_num
        AND TC.subinventory_code(+) = IOQD.subinventory_code
       AND IOQD.subinventory_code IN (select distinct SECONDARY_INVENTORY_NAME from inv_secondary_inventories where attribute1='Y'  ))
)  --)

UNION ALL

(SELECT DISTINCT ESI.item_number,
                replace(replace(ESI.LONG_DESCRIPTION,chr(10)),chr(13))                  item_DESCRIPTION
--                IOQD.secondary_inventory subinventory_code,
,decode(substr(IOQD.secondary_inventory,1,3),'11C','11CCL'
              ,'11O','11OPR'
              ,'18O','18OPR'
              ,'41S','24000'
              ,'41O','41OPR'
              ,'60O','60OPR'
              ,'70O','70OPR'
              ,'70C','70CCL'
              ,IOQD.secondary_inventory
             ) subinventory_code,
                IOQD.inventory_item_id
                p_inv,
                TC.TRANSACTION_COST   Cost,
                ESI.attribute5
                Patient_Chargeable,
                ESI.attribute2
                Chargeable_code,
                ESI.attribute3
                patient_charges,
                ESI.STOCK_ENABLED_FLAG,
case when 
 (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
           FROM inv_item_loc_defaults iild, 
                inv_item_locations iil
          WHERE iild.inventory_item_id = esi.inventory_item_id
            AND iild.subinventory_code = IOQD.secondary_inventory
            AND iild.locator_id       = iil.inventory_location_id
            AND rownum= 1 and length(Segment1) >= 1 ) is not null 
then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
           FROM inv_item_loc_defaults iild, 
                inv_item_locations iil
          WHERE iild.inventory_item_id = esi.inventory_item_id
            AND iild.subinventory_code = IOQD.secondary_inventory
            AND iild.locator_id       = iil.inventory_location_id
            AND rownum= 1 and length(Segment1) >= 1 )   

when (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
           FROM inv_secondary_locators iild, 
                inv_item_locations iil
          WHERE iild.inventory_item_id = esi.inventory_item_id
            AND iild.subinventory_code = IOQD.secondary_inventory
            AND iild.SECONDARY_LOCATOR= iil.inventory_location_id
            AND rownum = 1 and length(Segment1) >= 1 ) is not null 
        Then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
           FROM inv_secondary_locators iild, 
                inv_item_locations iil
          WHERE iild.inventory_item_id = esi.inventory_item_id
            AND iild.subinventory_code = IOQD.secondary_inventory
            AND iild.SECONDARY_LOCATOR= iil.inventory_location_id
            AND rownum = 1 and length(Segment1) >= 1 )

Else 'AAAAAAA'
END as      INV_LOCATOR, 
 rownum ivt_seqnum
        , CASE WHEN ESI.CONSIGNED_FLAG = '1' THEN 'Y'
                           WHEN ESI.CONSIGNED_FLAG = '2' THEN 'N'
                          END AS CONSIGNED_FLAG

FROM   INV_ITEM_SUB_INVENTORIES IOQD,
       egp_system_items ESI,
       Txn_Cost TC
WHERE  1 = 1
       AND IOQD.inventory_item_id = ESI.inventory_item_id
       AND IOQD.organization_id = ESI.organization_id
       AND ESI.inventory_item_id = :p_inv_num
       AND IOQD.secondary_inventory IN (select distinct SECONDARY_INVENTORY_NAME from inv_secondary_inventories where attribute1='Y')
       AND NOT EXISTS (select 1 
                         FROM inv_onhand_quantities_detail
                        WHERE subinventory_code = IOQD.secondary_inventory
                          AND inventory_item_id  = ESI.inventory_item_id
                      ) 
      AND TC.subinventory_code(+) = IOQD.secondary_inventory

) )results 

order by subinventory_code, INV_LOCATOR

The above runs in an acceptable amount of time, however when I add this bit of code below it takes significantly longer. Below is the exact code added, and then (below that) I have it incorporated into the entire query as well.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> UNION
SELECT ITM.INVENTORY_ITEM_ID,
SEC.SECONDARY_INVENTORY_NAME SUBINVENTORY_CODE,
'',
'',
'',
'',
round((LN.UNIT_PRICE / UOM.CONVERSION_RATE),3) TRANSACTION_COST
FROM INV_ITEM_SUB_INVENTORIES INV
,inv_secondary_inventories SEC
,PO_LINES_ALL LN
,PO_HEADERS_ALL HDR
,INV_UOM_CONVERSIONS UOM
,EGP_SYSTEM_ITEMS ITM
WHERE SEC.ASSET_INVENTORY = 2
AND SEC.SECONDARY_INVENTORY_NAME = INV.SECONDARY_INVENTORY
AND SEC.organization_id = INV.organization_id
AND LN.ITEM_ID = INV.INVENTORY_ITEM_ID
AND (LN.EXPIRATION_DATE IS NULL OR LN.EXPIRATION_DATE > sysdate)
AND LN.LINE_STATUS <> 'CANCELED'
AND HDR.PO_HEADER_ID = LN.PO_HEADER_ID
AND HDR.type_lookup_code = 'BLANKET'
AND UOM.INVENTORY_ITEM_ID = INV.INVENTORY_ITEM_ID
AND UOM.UOM_CODE = LN.UOM_CODE
AND ITM.INVENTORY_ITEM_ID = INV.INVENTORY_ITEM_ID
AND ITM.organization_id = 300000526890796
AND SUBSTR(SEC.SECONDARY_INVENTORY_NAME,1,2) = '70'
AND ITM.INVENTORY_ITEM_ID = :p_inv_num
</code>
<code> UNION SELECT ITM.INVENTORY_ITEM_ID, SEC.SECONDARY_INVENTORY_NAME SUBINVENTORY_CODE, '', '', '', '', round((LN.UNIT_PRICE / UOM.CONVERSION_RATE),3) TRANSACTION_COST FROM INV_ITEM_SUB_INVENTORIES INV ,inv_secondary_inventories SEC ,PO_LINES_ALL LN ,PO_HEADERS_ALL HDR ,INV_UOM_CONVERSIONS UOM ,EGP_SYSTEM_ITEMS ITM WHERE SEC.ASSET_INVENTORY = 2 AND SEC.SECONDARY_INVENTORY_NAME = INV.SECONDARY_INVENTORY AND SEC.organization_id = INV.organization_id AND LN.ITEM_ID = INV.INVENTORY_ITEM_ID AND (LN.EXPIRATION_DATE IS NULL OR LN.EXPIRATION_DATE > sysdate) AND LN.LINE_STATUS <> 'CANCELED' AND HDR.PO_HEADER_ID = LN.PO_HEADER_ID AND HDR.type_lookup_code = 'BLANKET' AND UOM.INVENTORY_ITEM_ID = INV.INVENTORY_ITEM_ID AND UOM.UOM_CODE = LN.UOM_CODE AND ITM.INVENTORY_ITEM_ID = INV.INVENTORY_ITEM_ID AND ITM.organization_id = 300000526890796 AND SUBSTR(SEC.SECONDARY_INVENTORY_NAME,1,2) = '70' AND ITM.INVENTORY_ITEM_ID = :p_inv_num </code>
    UNION 
          SELECT ITM.INVENTORY_ITEM_ID,              
                SEC.SECONDARY_INVENTORY_NAME SUBINVENTORY_CODE,
                '',
                '',
                '',
                '',
                round((LN.UNIT_PRICE / UOM.CONVERSION_RATE),3) TRANSACTION_COST 
            FROM INV_ITEM_SUB_INVENTORIES INV
               ,inv_secondary_inventories SEC
                 ,PO_LINES_ALL LN
                ,PO_HEADERS_ALL HDR
                ,INV_UOM_CONVERSIONS UOM
                ,EGP_SYSTEM_ITEMS ITM           
                             
            WHERE SEC.ASSET_INVENTORY = 2
             AND SEC.SECONDARY_INVENTORY_NAME = INV.SECONDARY_INVENTORY
             AND SEC.organization_id = INV.organization_id
              AND LN.ITEM_ID = INV.INVENTORY_ITEM_ID
              AND (LN.EXPIRATION_DATE IS NULL OR LN.EXPIRATION_DATE > sysdate)
              AND LN.LINE_STATUS <> 'CANCELED'
              AND HDR.PO_HEADER_ID = LN.PO_HEADER_ID
              AND HDR.type_lookup_code = 'BLANKET'
              AND UOM.INVENTORY_ITEM_ID = INV.INVENTORY_ITEM_ID
              AND UOM.UOM_CODE = LN.UOM_CODE
              AND ITM.INVENTORY_ITEM_ID = INV.INVENTORY_ITEM_ID
              AND ITM.organization_id = 300000526890796
              AND SUBSTR(SEC.SECONDARY_INVENTORY_NAME,1,2) = '70'
              AND ITM.INVENTORY_ITEM_ID = :p_inv_num  

Entire Query with the above section added that is causing performance issue:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>WITH Txn_Cost as
(SELECT * FROM
(SELECT A.INVENTORY_ITEM_ID
,C.SUBINVENTORY_CODE,
TO_CHAR(A.REC_TRXN_ID) REC_TRXN_ID,
TO_CHAR(B.TRANSACTION_ID) TRANSACTION_ID,
TO_CHAR(B.COST_DATE) COST_DATE,
TO_CHAR(A.QUANTITY_ONHAND) QUANTITY_ONHAND,
B.TRANSACTION_COST
FROM CST_ONHAND_V A,
CST_ITEM_COST_HISTORY_V B,
CST_TXN_LAYER_DTLS_V C
WHERE 1=1
AND A.INVENTORY_ITEM_ID= :p_inv_num
AND A.REC_TRXN_ID=B.TRANSACTION_ID
AND A.COST_ORG_ID = B.COST_ORG_ID
AND A.COST_BOOK_ID = B.COST_BOOK_ID
AND A.INVENTORY_ITEM_ID=B.INVENTORY_ITEM_ID
--AND B.COST_DATE=(SELECT MAX(COST_DATE) FROM CST_ITEM_COST_HISTORY_V B_COST WHERE
--B_COST.INVENTORY_ITEM_ID = B.INVENTORY_ITEM_ID
--TRANSACTION_ID=B.TRANSACTION_ID
--)
-- AND B.TRANSACTION_ID = C.TRANSACTION_ID
AND A.REC_TRXN_ID=C.REC_TRXN_ID
--AND C.SUBINVENTORY_CODE = IOQD.secondary_inventory
ORDER BY B.COST_DATE DESC NULLS LAST
)
WHERE ROWNUM = 1
--BEGIN CODE CHANGES!!!!-----------------------------------
UNION
SELECT ITM.INVENTORY_ITEM_ID,
SEC.SECONDARY_INVENTORY_NAME SUBINVENTORY_CODE,
'',
'',
'',
'',
round((LN.UNIT_PRICE / UOM.CONVERSION_RATE),3) TRANSACTION_COST
FROM INV_ITEM_SUB_INVENTORIES INV
,inv_secondary_inventories SEC
,PO_LINES_ALL LN
,PO_HEADERS_ALL HDR
,INV_UOM_CONVERSIONS UOM
,EGP_SYSTEM_ITEMS ITM
WHERE SEC.ASSET_INVENTORY = 2
AND SEC.SECONDARY_INVENTORY_NAME = INV.SECONDARY_INVENTORY
AND SEC.organization_id = INV.organization_id
AND LN.ITEM_ID = INV.INVENTORY_ITEM_ID
AND (LN.EXPIRATION_DATE IS NULL OR LN.EXPIRATION_DATE > sysdate)
AND LN.LINE_STATUS <> 'CANCELED'
AND HDR.PO_HEADER_ID = LN.PO_HEADER_ID
AND HDR.type_lookup_code = 'BLANKET'
AND UOM.INVENTORY_ITEM_ID = INV.INVENTORY_ITEM_ID
AND UOM.UOM_CODE = LN.UOM_CODE
AND ITM.INVENTORY_ITEM_ID = INV.INVENTORY_ITEM_ID
AND ITM.organization_id = 300000526890796
AND SUBSTR(SEC.SECONDARY_INVENTORY_NAME,1,2) = '70'
AND ITM.INVENTORY_ITEM_ID = :p_inv_num
--END CODE CHANGES!!!!-----------------------------------
)
Select * from
((
SELECT item_number,item_DESCRIPTION
,decode(substr(subinventory_code,1,3),'11C','11CCL'
,'11O','11OPR'
,'18O','18OPR'
,'41S','24000'
,'41O','41OPR'
,'60O','60OPR'
,'70O','70OPR'
,'70C','70CCL'
,subinventory_code
) subinventory_code
,p_inv,
round(Cost,5) Cost,Patient_Chargeable,Chargeable_code,
patient_charges,STOCK_ENABLED_FLAG,INV_LOCATOR ,rownum ivt_seqnum
, CONSIGNED_FLAG
FROM (
SELECT DISTINCT ESI.item_number,
replace(replace(ESI.LONG_DESCRIPTION,chr(10)),chr(13)) item_DESCRIPTION,
IOQD.subinventory_code,
IOQD.inventory_item_id p_inv,
TC.TRANSACTION_COST Cost,
ESI.attribute5
Patient_Chargeable,
ESI.attribute2
Chargeable_code,
ESI.attribute3
patient_charges,
ESI.STOCK_ENABLED_FLAG,
(select SEGMENT1||'-'||SEGMENT2||'-'||SEGMENT3||'-'||SEGMENT4||'-'||SEGMENT5
from inv_item_locations
where inventory_location_id= ioqd.locator_id and length(Segment1) < 3
and length(Segment1) >= 1
) INV_LOCATOR,
CASE WHEN ESI.CONSIGNED_FLAG = '1' THEN 'Y'
WHEN ESI.CONSIGNED_FLAG = '2' THEN 'N'
END AS CONSIGNED_FLAG
FROM inv_onhand_quantities_detail IOQD,
egp_system_items ESI,
Txn_Cost TC
WHERE 1 = 1
AND IOQD.inventory_item_id = ESI.inventory_item_id
AND IOQD.organization_id = ESI.organization_id
AND ESI.inventory_item_id = :p_inv_num
AND TC.subinventory_code(+) = IOQD.subinventory_code
AND IOQD.subinventory_code IN (select distinct SECONDARY_INVENTORY_NAME from inv_secondary_inventories where attribute1='Y' ))
) --)
UNION ALL
(SELECT DISTINCT ESI.item_number,
replace(replace(ESI.LONG_DESCRIPTION,chr(10)),chr(13)) item_DESCRIPTION
-- IOQD.secondary_inventory subinventory_code,
,decode(substr(IOQD.secondary_inventory,1,3),'11C','11CCL'
,'11O','11OPR'
,'18O','18OPR'
,'41S','24000'
,'41O','41OPR'
,'60O','60OPR'
,'70O','70OPR'
,'70C','70CCL'
,IOQD.secondary_inventory
) subinventory_code,
IOQD.inventory_item_id
p_inv,
TC.TRANSACTION_COST Cost,
ESI.attribute5
Patient_Chargeable,
ESI.attribute2
Chargeable_code,
ESI.attribute3
patient_charges,
ESI.STOCK_ENABLED_FLAG,
case when
(SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
FROM inv_item_loc_defaults iild,
inv_item_locations iil
WHERE iild.inventory_item_id = esi.inventory_item_id
AND iild.subinventory_code = IOQD.secondary_inventory
AND iild.locator_id = iil.inventory_location_id
AND rownum= 1 and length(Segment1) >= 1 ) is not null
then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
FROM inv_item_loc_defaults iild,
inv_item_locations iil
WHERE iild.inventory_item_id = esi.inventory_item_id
AND iild.subinventory_code = IOQD.secondary_inventory
AND iild.locator_id = iil.inventory_location_id
AND rownum= 1 and length(Segment1) >= 1 )
when (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
FROM inv_secondary_locators iild,
inv_item_locations iil
WHERE iild.inventory_item_id = esi.inventory_item_id
AND iild.subinventory_code = IOQD.secondary_inventory
AND iild.SECONDARY_LOCATOR= iil.inventory_location_id
AND rownum = 1 and length(Segment1) >= 1 ) is not null
Then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
FROM inv_secondary_locators iild,
inv_item_locations iil
WHERE iild.inventory_item_id = esi.inventory_item_id
AND iild.subinventory_code = IOQD.secondary_inventory
AND iild.SECONDARY_LOCATOR= iil.inventory_location_id
AND rownum = 1 and length(Segment1) >= 1 )
Else 'AAAAAAA'
END as INV_LOCATOR,
rownum ivt_seqnum
, CASE WHEN ESI.CONSIGNED_FLAG = '1' THEN 'Y'
WHEN ESI.CONSIGNED_FLAG = '2' THEN 'N'
END AS CONSIGNED_FLAG
FROM INV_ITEM_SUB_INVENTORIES IOQD,
egp_system_items ESI,
Txn_Cost TC
WHERE 1 = 1
AND IOQD.inventory_item_id = ESI.inventory_item_id
AND IOQD.organization_id = ESI.organization_id
AND ESI.inventory_item_id = :p_inv_num
AND IOQD.secondary_inventory IN (select distinct SECONDARY_INVENTORY_NAME from inv_secondary_inventories where attribute1='Y')
AND NOT EXISTS (select 1
FROM inv_onhand_quantities_detail
WHERE subinventory_code = IOQD.secondary_inventory
AND inventory_item_id = ESI.inventory_item_id
)
AND TC.subinventory_code(+) = IOQD.secondary_inventory
--Order by addition
) )results
order by subinventory_code, INV_LOCATOR
</code>
<code>WITH Txn_Cost as (SELECT * FROM (SELECT A.INVENTORY_ITEM_ID ,C.SUBINVENTORY_CODE, TO_CHAR(A.REC_TRXN_ID) REC_TRXN_ID, TO_CHAR(B.TRANSACTION_ID) TRANSACTION_ID, TO_CHAR(B.COST_DATE) COST_DATE, TO_CHAR(A.QUANTITY_ONHAND) QUANTITY_ONHAND, B.TRANSACTION_COST FROM CST_ONHAND_V A, CST_ITEM_COST_HISTORY_V B, CST_TXN_LAYER_DTLS_V C WHERE 1=1 AND A.INVENTORY_ITEM_ID= :p_inv_num AND A.REC_TRXN_ID=B.TRANSACTION_ID AND A.COST_ORG_ID = B.COST_ORG_ID AND A.COST_BOOK_ID = B.COST_BOOK_ID AND A.INVENTORY_ITEM_ID=B.INVENTORY_ITEM_ID --AND B.COST_DATE=(SELECT MAX(COST_DATE) FROM CST_ITEM_COST_HISTORY_V B_COST WHERE --B_COST.INVENTORY_ITEM_ID = B.INVENTORY_ITEM_ID --TRANSACTION_ID=B.TRANSACTION_ID --) -- AND B.TRANSACTION_ID = C.TRANSACTION_ID AND A.REC_TRXN_ID=C.REC_TRXN_ID --AND C.SUBINVENTORY_CODE = IOQD.secondary_inventory ORDER BY B.COST_DATE DESC NULLS LAST ) WHERE ROWNUM = 1 --BEGIN CODE CHANGES!!!!----------------------------------- UNION SELECT ITM.INVENTORY_ITEM_ID, SEC.SECONDARY_INVENTORY_NAME SUBINVENTORY_CODE, '', '', '', '', round((LN.UNIT_PRICE / UOM.CONVERSION_RATE),3) TRANSACTION_COST FROM INV_ITEM_SUB_INVENTORIES INV ,inv_secondary_inventories SEC ,PO_LINES_ALL LN ,PO_HEADERS_ALL HDR ,INV_UOM_CONVERSIONS UOM ,EGP_SYSTEM_ITEMS ITM WHERE SEC.ASSET_INVENTORY = 2 AND SEC.SECONDARY_INVENTORY_NAME = INV.SECONDARY_INVENTORY AND SEC.organization_id = INV.organization_id AND LN.ITEM_ID = INV.INVENTORY_ITEM_ID AND (LN.EXPIRATION_DATE IS NULL OR LN.EXPIRATION_DATE > sysdate) AND LN.LINE_STATUS <> 'CANCELED' AND HDR.PO_HEADER_ID = LN.PO_HEADER_ID AND HDR.type_lookup_code = 'BLANKET' AND UOM.INVENTORY_ITEM_ID = INV.INVENTORY_ITEM_ID AND UOM.UOM_CODE = LN.UOM_CODE AND ITM.INVENTORY_ITEM_ID = INV.INVENTORY_ITEM_ID AND ITM.organization_id = 300000526890796 AND SUBSTR(SEC.SECONDARY_INVENTORY_NAME,1,2) = '70' AND ITM.INVENTORY_ITEM_ID = :p_inv_num --END CODE CHANGES!!!!----------------------------------- ) Select * from (( SELECT item_number,item_DESCRIPTION ,decode(substr(subinventory_code,1,3),'11C','11CCL' ,'11O','11OPR' ,'18O','18OPR' ,'41S','24000' ,'41O','41OPR' ,'60O','60OPR' ,'70O','70OPR' ,'70C','70CCL' ,subinventory_code ) subinventory_code ,p_inv, round(Cost,5) Cost,Patient_Chargeable,Chargeable_code, patient_charges,STOCK_ENABLED_FLAG,INV_LOCATOR ,rownum ivt_seqnum , CONSIGNED_FLAG FROM ( SELECT DISTINCT ESI.item_number, replace(replace(ESI.LONG_DESCRIPTION,chr(10)),chr(13)) item_DESCRIPTION, IOQD.subinventory_code, IOQD.inventory_item_id p_inv, TC.TRANSACTION_COST Cost, ESI.attribute5 Patient_Chargeable, ESI.attribute2 Chargeable_code, ESI.attribute3 patient_charges, ESI.STOCK_ENABLED_FLAG, (select SEGMENT1||'-'||SEGMENT2||'-'||SEGMENT3||'-'||SEGMENT4||'-'||SEGMENT5 from inv_item_locations where inventory_location_id= ioqd.locator_id and length(Segment1) < 3 and length(Segment1) >= 1 ) INV_LOCATOR, CASE WHEN ESI.CONSIGNED_FLAG = '1' THEN 'Y' WHEN ESI.CONSIGNED_FLAG = '2' THEN 'N' END AS CONSIGNED_FLAG FROM inv_onhand_quantities_detail IOQD, egp_system_items ESI, Txn_Cost TC WHERE 1 = 1 AND IOQD.inventory_item_id = ESI.inventory_item_id AND IOQD.organization_id = ESI.organization_id AND ESI.inventory_item_id = :p_inv_num AND TC.subinventory_code(+) = IOQD.subinventory_code AND IOQD.subinventory_code IN (select distinct SECONDARY_INVENTORY_NAME from inv_secondary_inventories where attribute1='Y' )) ) --) UNION ALL (SELECT DISTINCT ESI.item_number, replace(replace(ESI.LONG_DESCRIPTION,chr(10)),chr(13)) item_DESCRIPTION -- IOQD.secondary_inventory subinventory_code, ,decode(substr(IOQD.secondary_inventory,1,3),'11C','11CCL' ,'11O','11OPR' ,'18O','18OPR' ,'41S','24000' ,'41O','41OPR' ,'60O','60OPR' ,'70O','70OPR' ,'70C','70CCL' ,IOQD.secondary_inventory ) subinventory_code, IOQD.inventory_item_id p_inv, TC.TRANSACTION_COST Cost, ESI.attribute5 Patient_Chargeable, ESI.attribute2 Chargeable_code, ESI.attribute3 patient_charges, ESI.STOCK_ENABLED_FLAG, case when (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5 FROM inv_item_loc_defaults iild, inv_item_locations iil WHERE iild.inventory_item_id = esi.inventory_item_id AND iild.subinventory_code = IOQD.secondary_inventory AND iild.locator_id = iil.inventory_location_id AND rownum= 1 and length(Segment1) >= 1 ) is not null then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5 FROM inv_item_loc_defaults iild, inv_item_locations iil WHERE iild.inventory_item_id = esi.inventory_item_id AND iild.subinventory_code = IOQD.secondary_inventory AND iild.locator_id = iil.inventory_location_id AND rownum= 1 and length(Segment1) >= 1 ) when (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5 FROM inv_secondary_locators iild, inv_item_locations iil WHERE iild.inventory_item_id = esi.inventory_item_id AND iild.subinventory_code = IOQD.secondary_inventory AND iild.SECONDARY_LOCATOR= iil.inventory_location_id AND rownum = 1 and length(Segment1) >= 1 ) is not null Then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5 FROM inv_secondary_locators iild, inv_item_locations iil WHERE iild.inventory_item_id = esi.inventory_item_id AND iild.subinventory_code = IOQD.secondary_inventory AND iild.SECONDARY_LOCATOR= iil.inventory_location_id AND rownum = 1 and length(Segment1) >= 1 ) Else 'AAAAAAA' END as INV_LOCATOR, rownum ivt_seqnum , CASE WHEN ESI.CONSIGNED_FLAG = '1' THEN 'Y' WHEN ESI.CONSIGNED_FLAG = '2' THEN 'N' END AS CONSIGNED_FLAG FROM INV_ITEM_SUB_INVENTORIES IOQD, egp_system_items ESI, Txn_Cost TC WHERE 1 = 1 AND IOQD.inventory_item_id = ESI.inventory_item_id AND IOQD.organization_id = ESI.organization_id AND ESI.inventory_item_id = :p_inv_num AND IOQD.secondary_inventory IN (select distinct SECONDARY_INVENTORY_NAME from inv_secondary_inventories where attribute1='Y') AND NOT EXISTS (select 1 FROM inv_onhand_quantities_detail WHERE subinventory_code = IOQD.secondary_inventory AND inventory_item_id = ESI.inventory_item_id ) AND TC.subinventory_code(+) = IOQD.secondary_inventory --Order by addition ) )results order by subinventory_code, INV_LOCATOR </code>
WITH Txn_Cost as
    (SELECT * FROM 
                     (SELECT A.INVENTORY_ITEM_ID
                              ,C.SUBINVENTORY_CODE,
                              TO_CHAR(A.REC_TRXN_ID) REC_TRXN_ID,
                              TO_CHAR(B.TRANSACTION_ID) TRANSACTION_ID,
                              TO_CHAR(B.COST_DATE) COST_DATE,
                              TO_CHAR(A.QUANTITY_ONHAND) QUANTITY_ONHAND,
                              B.TRANSACTION_COST
                          FROM CST_ONHAND_V A,
                               CST_ITEM_COST_HISTORY_V B,
                               CST_TXN_LAYER_DTLS_V C
                         WHERE 1=1
                           AND A.INVENTORY_ITEM_ID= :p_inv_num
                           AND A.REC_TRXN_ID=B.TRANSACTION_ID
                       
                           AND A.COST_ORG_ID = B.COST_ORG_ID
                           AND A.COST_BOOK_ID = B.COST_BOOK_ID
                           AND A.INVENTORY_ITEM_ID=B.INVENTORY_ITEM_ID
                           --AND B.COST_DATE=(SELECT MAX(COST_DATE) FROM CST_ITEM_COST_HISTORY_V B_COST WHERE 
                           --B_COST.INVENTORY_ITEM_ID = B.INVENTORY_ITEM_ID
                           --TRANSACTION_ID=B.TRANSACTION_ID
                           --)
                         --  AND B.TRANSACTION_ID = C.TRANSACTION_ID
                           AND A.REC_TRXN_ID=C.REC_TRXN_ID
                        --AND C.SUBINVENTORY_CODE = IOQD.secondary_inventory     
        ORDER BY B.COST_DATE DESC NULLS LAST
        )
        WHERE ROWNUM = 1
 --BEGIN CODE CHANGES!!!!-----------------------------------   
    UNION 
      SELECT ITM.INVENTORY_ITEM_ID,              
            SEC.SECONDARY_INVENTORY_NAME SUBINVENTORY_CODE,
            '',
            '',
            '',
            '',
            round((LN.UNIT_PRICE / UOM.CONVERSION_RATE),3) TRANSACTION_COST 
        FROM INV_ITEM_SUB_INVENTORIES INV
           ,inv_secondary_inventories SEC
             ,PO_LINES_ALL LN
            ,PO_HEADERS_ALL HDR
            ,INV_UOM_CONVERSIONS UOM
            ,EGP_SYSTEM_ITEMS ITM                   
                                     
        
         WHERE SEC.ASSET_INVENTORY = 2
         AND SEC.SECONDARY_INVENTORY_NAME = INV.SECONDARY_INVENTORY
         AND SEC.organization_id = INV.organization_id
          AND LN.ITEM_ID = INV.INVENTORY_ITEM_ID
          AND (LN.EXPIRATION_DATE IS NULL OR LN.EXPIRATION_DATE > sysdate)
          AND LN.LINE_STATUS <> 'CANCELED'
          AND HDR.PO_HEADER_ID = LN.PO_HEADER_ID
          AND HDR.type_lookup_code = 'BLANKET'
          AND UOM.INVENTORY_ITEM_ID = INV.INVENTORY_ITEM_ID
          AND UOM.UOM_CODE = LN.UOM_CODE
          AND ITM.INVENTORY_ITEM_ID = INV.INVENTORY_ITEM_ID
          AND ITM.organization_id = 300000526890796
          AND SUBSTR(SEC.SECONDARY_INVENTORY_NAME,1,2) = '70'
          AND ITM.INVENTORY_ITEM_ID = :p_inv_num  
    --END CODE CHANGES!!!!----------------------------------- 
    )
                   
    
    Select * from 
    (( 
    SELECT item_number,item_DESCRIPTION       
          ,decode(substr(subinventory_code,1,3),'11C','11CCL'
                  ,'11O','11OPR'
                  ,'18O','18OPR'
                  ,'41S','24000'
                  ,'41O','41OPR'
                  ,'60O','60OPR'
                  ,'70O','70OPR'
                  ,'70C','70CCL'
                  ,subinventory_code
                 ) subinventory_code
          ,p_inv,
           round(Cost,5) Cost,Patient_Chargeable,Chargeable_code,
           patient_charges,STOCK_ENABLED_FLAG,INV_LOCATOR ,rownum ivt_seqnum 
           ,               CONSIGNED_FLAG
    FROM (
    SELECT DISTINCT ESI.item_number,
                    replace(replace(ESI.LONG_DESCRIPTION,chr(10)),chr(13))                  item_DESCRIPTION,
                    IOQD.subinventory_code,
                    IOQD.inventory_item_id                 p_inv,
                    TC.TRANSACTION_COST   Cost,
                    ESI.attribute5
                    Patient_Chargeable,
                    ESI.attribute2
                    Chargeable_code,
                    ESI.attribute3
                    patient_charges,
                    ESI.STOCK_ENABLED_FLAG,
    (select SEGMENT1||'-'||SEGMENT2||'-'||SEGMENT3||'-'||SEGMENT4||'-'||SEGMENT5
                       from inv_item_locations
                      where inventory_location_id= ioqd.locator_id and length(Segment1) < 3
    and length(Segment1) >= 1
                    ) INV_LOCATOR,
            CASE WHEN ESI.CONSIGNED_FLAG = '1' THEN 'Y'
                               WHEN ESI.CONSIGNED_FLAG = '2' THEN 'N'
                              END AS CONSIGNED_FLAG
    FROM   inv_onhand_quantities_detail IOQD,
           egp_system_items ESI, 
           Txn_Cost TC
    WHERE  1 = 1
           AND IOQD.inventory_item_id = ESI.inventory_item_id
           AND IOQD.organization_id = ESI.organization_id
           AND ESI.inventory_item_id = :p_inv_num
            AND TC.subinventory_code(+) = IOQD.subinventory_code
           AND IOQD.subinventory_code IN (select distinct SECONDARY_INVENTORY_NAME from inv_secondary_inventories where attribute1='Y'  ))
    )  --)
    
    UNION ALL
    
    (SELECT DISTINCT ESI.item_number,
                    replace(replace(ESI.LONG_DESCRIPTION,chr(10)),chr(13))                  item_DESCRIPTION
    --                IOQD.secondary_inventory subinventory_code,
    ,decode(substr(IOQD.secondary_inventory,1,3),'11C','11CCL'
                  ,'11O','11OPR'
                  ,'18O','18OPR'
                  ,'41S','24000'
                  ,'41O','41OPR'
                  ,'60O','60OPR'
                  ,'70O','70OPR'
                  ,'70C','70CCL'
                  ,IOQD.secondary_inventory
                 ) subinventory_code,
                    IOQD.inventory_item_id
                    p_inv,
                    TC.TRANSACTION_COST   Cost,
                    ESI.attribute5
                    Patient_Chargeable,
                    ESI.attribute2
                    Chargeable_code,
                    ESI.attribute3
                    patient_charges,
                    ESI.STOCK_ENABLED_FLAG,
    case when 
     (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
               FROM inv_item_loc_defaults iild, 
                    inv_item_locations iil
              WHERE iild.inventory_item_id = esi.inventory_item_id
                AND iild.subinventory_code = IOQD.secondary_inventory
                AND iild.locator_id       = iil.inventory_location_id
                AND rownum= 1 and length(Segment1) >= 1 ) is not null 
    then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
               FROM inv_item_loc_defaults iild, 
                    inv_item_locations iil
              WHERE iild.inventory_item_id = esi.inventory_item_id
                AND iild.subinventory_code = IOQD.secondary_inventory
                AND iild.locator_id       = iil.inventory_location_id
                AND rownum= 1 and length(Segment1) >= 1 )   
    
    when (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
               FROM inv_secondary_locators iild, 
                    inv_item_locations iil
              WHERE iild.inventory_item_id = esi.inventory_item_id
                AND iild.subinventory_code = IOQD.secondary_inventory
                AND iild.SECONDARY_LOCATOR= iil.inventory_location_id
                AND rownum = 1 and length(Segment1) >= 1 ) is not null 
            Then (SELECT iil.SEGMENT1||'-'||iil.SEGMENT2||'-'||iil.SEGMENT3||'-'||iil.SEGMENT4||'-'||iil.SEGMENT5
               FROM inv_secondary_locators iild, 
                    inv_item_locations iil
              WHERE iild.inventory_item_id = esi.inventory_item_id
                AND iild.subinventory_code = IOQD.secondary_inventory
                AND iild.SECONDARY_LOCATOR= iil.inventory_location_id
                AND rownum = 1 and length(Segment1) >= 1 )
    
    Else 'AAAAAAA'
    END as      INV_LOCATOR, 
     rownum ivt_seqnum
            , CASE WHEN ESI.CONSIGNED_FLAG = '1' THEN 'Y'
                               WHEN ESI.CONSIGNED_FLAG = '2' THEN 'N'
                              END AS CONSIGNED_FLAG
    
    FROM   INV_ITEM_SUB_INVENTORIES IOQD,
           egp_system_items ESI,
           Txn_Cost TC
    WHERE  1 = 1
           AND IOQD.inventory_item_id = ESI.inventory_item_id
           AND IOQD.organization_id = ESI.organization_id
           AND ESI.inventory_item_id = :p_inv_num
           AND IOQD.secondary_inventory IN (select distinct SECONDARY_INVENTORY_NAME from inv_secondary_inventories where attribute1='Y')
           AND NOT EXISTS (select 1 
                             FROM inv_onhand_quantities_detail
                            WHERE subinventory_code = IOQD.secondary_inventory
                              AND inventory_item_id  = ESI.inventory_item_id
                          ) 
          AND TC.subinventory_code(+) = IOQD.secondary_inventory
    --Order by addition 
    ) )results 
    
    order by subinventory_code, INV_LOCATOR

I did change some of the Subqueries in the WHERE clause to be fully joined tables, and also re-worked the INV_LOCATOR CASE statements but that did not improve performance. I am looking for assistance in how I can improve performance from the section of code I added. Thank you.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật