I could use some pointers on my query I have three external tables that i need to combine and create a .dat-file. But my query isn’t very efficient and could use some help on fixing/improving this one. It consist of two part one main table which is dc_item_loc_sourcing (1) and two translation tables dc_ccn190_sid_vtb(2) and dc_item_loc_vert_pim(3). If the item + loc combination doesn’t exist in 1 & 2 it should check 1 & 3.
SET heading OFF;
SET feedback OFF;
SET verify OFF;
SET echo OFF;
SET linesize 1000;
SET trimspool ON;
SET termout OFF;
SET newpage NONE;
SPOOL ../data/dc_sourcing.dat;
SELECT DISTINCT TO_CHAR(item)
|| '|' ||store
|| '|' ||source_method
|| '|' ||primary_supp
|| '|' ||source_wh
|| '|' ||actie
|| '|' ||reward_eligible_ind
FROM ((SELECT dpac_tbl.item
,st.store
,dc_iloc.source_method
,dc_iloc.primary_supp
,dc_iloc.source_wh
,dc_iloc.actie
,dc_iloc.reward_eligible_ind
,MAX(dc_iloc.source_method) OVER (PARTITION BY dpac_tbl.item,st.store) max_src_pack
FROM dc_item_loc_sourcing dc_iloc ,
dc_ccn190_sid_vtb dpac_tbl ,
store st ,
item_master im ,
item_loc il
WHERE dc_iloc.dpac = dpac_tbl.dpac
AND dpac_tbl.item = im.item
AND CAST(dc_iloc.loc AS VARCHAR2(150byte)) = st.store_name_secondary
AND st.store = il.loc
AND dpac_tbl.item = il.item
AND im.status = 'A'
AND st.store_close_date >= SYSDATE
AND il.status = 'A'
AND dpac_tbl.ITEM NOT IN
(SELECT IA.ITEM
FROM ITEM_ATTRIBUTES IA
WHERE IA.SH_STORE_ORDER_UNIT = 'N'
AND IA.SH_TRADE_UNIT = 'Y')
UNION
SELECT DISTINCT pi.item
,st.store
,dc_iloc.source_method
,dc_iloc.primary_supp
,dc_iloc.source_wh
,dc_iloc.actie
,dc_iloc.reward_eligible_ind
,MAX(dc_iloc.source_method) OVER (PARTITION BY pi.item,st.store) max_src_pack
FROM dc_item_loc_sourcing dc_iloc ,
dc_ccn190_sid_vtb dpac_tbl ,
store st ,
packitem pi ,
item_master im ,
item_loc il
WHERE dc_iloc.dpac = dpac_tbl.dpac
AND pi.pack_no = dpac_tbl.item
AND pi.item = im.item
AND CAST(dc_iloc.loc AS VARCHAR2(150byte)) = st.store_name_secondary
AND il.item = pi.item
AND il.loc = st.store
AND im.status = 'A'
AND im.dept NOT IN (900,910,920,930)
AND st.store_close_date >= SYSDATE
AND il.status = 'A'
AND PI.ITEM NOT IN
(SELECT IA.ITEM
FROM ITEM_ATTRIBUTES IA
WHERE IA.SH_STORE_ORDER_UNIT = 'N'
AND IA.SH_TRADE_UNIT = 'Y'))
UNION
(SELECT dpac_tbl.item ,
st.store ,
dc_iloc.source_method ,
dc_iloc.primary_supp ,
dc_iloc.source_wh ,
dc_iloc.actie ,
dc_iloc.reward_eligible_ind
,MAX(dc_iloc.source_method) OVER (PARTITION BY dpac_tbl.item,st.store) max_src_pack
FROM dc_item_loc_sourcing dc_iloc ,
dc_item_loc_vert_pim dpac_tbl ,
store st ,
item_master im ,
item_loc il
WHERE dc_iloc.dpac = dpac_tbl.dpac
AND dpac_tbl.item = im.item
AND CAST(dc_iloc.loc AS VARCHAR2(150 byte)) = st.store_name_secondary
AND il.item = dpac_tbl.item
AND il.loc = st.store
AND im.status = 'A'
AND dpac_tbl.artikel_type_lms NOT IN ('V','S')
AND st.store_close_date >= SYSDATE
AND il.status = 'A'
AND inventory_item_status_code = 'Active'
AND dpac_tbl.ITEM NOT IN
(SELECT IA.ITEM
FROM ITEM_ATTRIBUTES IA
WHERE IA.SH_STORE_ORDER_UNIT = 'N'
AND IA.SH_TRADE_UNIT = 'Y')
UNION
SELECT DISTINCT pi.item ,
st.store ,
dc_iloc.source_method ,
dc_iloc.primary_supp ,
dc_iloc.source_wh ,
dc_iloc.actie ,
dc_iloc.reward_eligible_ind
,MAX(dc_iloc.source_method) OVER (PARTITION BY pi.item,st.store) max_src_pack
FROM dc_item_loc_sourcing dc_iloc ,
dc_item_loc_vert_pim dpac_tbl ,
store st ,
packitem pi ,
item_master im ,
item_loc il
WHERE dc_iloc.dpac = dpac_tbl.dpac
AND pi.pack_no = dpac_tbl.item
AND pi.item = im.item
AND CAST(dc_iloc.loc AS VARCHAR2(150 byte)) = st.store_name_secondary
AND il.item = pi.item
AND il.loc = st.store
AND im.status = 'A'
AND dpac_tbl.artikel_type_lms NOT IN ('V','S')
AND im.dept NOT IN (900,910,920,930)
AND st.store_close_date >= SYSDATE
AND il.status = 'A'
AND inventory_item_status_code = 'Active'
AND pi.ITEM NOT IN
(SELECT IA.ITEM
FROM ITEM_ATTRIBUTES IA
WHERE IA.SH_STORE_ORDER_UNIT = 'N'
AND IA.SH_TRADE_UNIT = 'Y')))
WHERE source_method = max_src_pack;
SPOOL OFF;
This hasn’t been tested – I’m away from an SQL instance at the moment, and obviously I don’t have access to your tables. I also don’t make any guarantee about performance, as I don’t know about current indicies.
Your biggest problem appears to stem from using the implicit-join syntax. Don’t use it, it’s an anti-pattern; it also allows for some ‘surprising’ behaviour. Always explicitly specify your joins, and put (as many as possible) relevant conditions in the
ONclause – only use theWHEREclause when dealing with the table reference in theFROMclause.I also have a huge problem with this line:
AND CAST(dc_iloc.loc AS VARCHAR2(150byte)) = st.store_name_secondaryYou will not (likely… there are some caveats) be using an index on this comparison, which won’t help. It’s also a terrible thing, semantically, to join on – why is an item-location table keyed by (apparently) the store name? It should by keyed by store id – which should be the same datatype (no conversions), and should be the internal id, not something as transient as a ‘name’.
I believe this is correct, although a definition of your tables (and sample data) would help immensely in this regard – especially in how
packitemis defined.