select
tmp.inv_qty,
barcode_sub||'-'|| (CASE
WHEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('390') ) >= TO_NUMBER('390')
THEN TO_NUMBER('390')
WHEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('390') ) < TO_NUMBER('390')
THEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('390') )
END)||'-'||un||decode(barcode2,000000,null,'-E') as BARCODE1,
decode(barcode2,000000,null,barcode2)as barcode2,
decode(barcode2_name,'-0',null,barcode2_name) as barcode2_name,
barcode_sub||'-'|| (CASE
WHEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('390') ) >= TO_NUMBER('390')
THEN TO_NUMBER('390')
WHEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('390') ) < TO_NUMBER('390')
THEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('390') )
END)||'-'||un||decode(barcode2,000000,null,'-E')||decode(barcode2,000000,null,'-'||barcode2)as barcode3,
mat_no,
bat_no,
Des,
(CASE
WHEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('390') ) >= TO_NUMBER('390')
THEN TO_NUMBER('390')
WHEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('390') ) < TO_NUMBER('390')
THEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('390') )
END)||' '||un as qty,
add_date,
user_id,
user_name,
inv_no
FROM
(
select
IVBH.BATCH_SEQ,
podt.item_no||'-'||ivbh.batch_no as barcode_sub ,
podt.SALES_NO||podt.sales_seq as barcode2 ,
podt.SALES_NO||'-'||
CASE WHEN TRIM(TRANSLATE(podt.sales_seq, '0123456789', ' ')) IS NULL
THEN TO_CHAR(TO_NUMBER(podt.sales_seq))
ELSE podt.sales_seq
END AS barcode2_name,
podt.item_no as mat_no,
ivbh.batch_no as bat_no,
ivdt.description_loc as Des ,
substr(ivhd.VENDOR_GI_DATE,0,4)||'/'||substr(ivhd.VENDOR_GI_DATE,5,2)||'/'||substr(ivhd.VENDOR_GI_DATE,7,2) as add_date,
ivbh.add_user_id as user_id,
ivdt.add_user_name_loc as user_name,
ivdt.inv_no||'/'||
CASE WHEN TRIM(TRANSLATE(ivdt.inv_seq, '0123456789', ' ')) IS NULL
THEN TO_CHAR(TO_NUMBER(ivdt.inv_seq))
ELSE ivdt.inv_seq
END AS inv_no,
ivdt.unit_measure as un ,
ivdt.inv_qty
from
icoypodt podt,
icomivbh ivbh,
icomivdt ivdt,
icomivhd ivhd
where
IVHD.HOUSE_CODE = '100'
AND podt.po_no=ivdt.po_no
AND IVDT.VENDOR_CODE = '100011'
and podt.po_seq= ivdt.po_seq
and ivdt.inv_no = ivbh.INV_NO
and ivdt.inv_seq=ivbh.inv_seq
and ivdt.inv_no=ivhd.inv_no
and podt.po_no=ivbh.po_no
and podt.po_seq=ivbh.po_seq
and podt.status !='D'
and ivbh.status !='D'
and ivdt.status !='D'
and ivhd.status !='D'
AND IVDT.INV_NO = '1800631513'
AND IVDT.INV_SEQ = '000010'
AND IVBH.BATCH_NO = '1408290206'
AND IVBH.BATCH_SEQ = '000001') TMP, COPY_T TT
where
rownum <='3'
UNION all
select
tmp.inv_qty,
barcode_sub||'-'|| (CASE
WHEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('130') ) >= TO_NUMBER('130')
THEN TO_NUMBER('130')
WHEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('130') ) < TO_NUMBER('130')
THEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('130') )
END)||'-'||un||decode(barcode2,000000,null,'-E') as BARCODE1,
decode(barcode2,000000,null,barcode2)as barcode2,
decode(barcode2_name,'-0',null,barcode2_name) as barcode2_name,
barcode_sub||'-'|| (CASE
WHEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('130') ) >= TO_NUMBER('130')
THEN TO_NUMBER('130')
WHEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('130') ) < TO_NUMBER('130')
THEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('130') )
END)||'-'||un||decode(barcode2,000000,null,'-E')||decode(barcode2,000000,null,'-'||barcode2)as barcode3,
mat_no,
bat_no,
Des,
(CASE
WHEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('130') ) >= TO_NUMBER('130')
THEN TO_NUMBER('130')
WHEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('130') ) < TO_NUMBER('130')
THEN NVL(TMP.inv_qty, 0) - ( TT.NO * TO_NUMBER('130') )
END)||' '||un as qty,
add_date,
user_id,
user_name,
inv_no
FROM
(
select
IVBH.BATCH_SEQ,
podt.item_no||'-'||ivbh.batch_no as barcode_sub ,
podt.SALES_NO||podt.sales_seq as barcode2 ,
podt.SALES_NO||'-'||
CASE WHEN TRIM(TRANSLATE(podt.sales_seq, '0123456789', ' ')) IS NULL
THEN TO_CHAR(TO_NUMBER(podt.sales_seq))
ELSE podt.sales_seq
END AS barcode2_name,
podt.item_no as mat_no,
ivbh.batch_no as bat_no,
ivdt.description_loc as Des ,
substr(ivhd.VENDOR_GI_DATE,0,4)||'/'||substr(ivhd.VENDOR_GI_DATE,5,2)||'/'||substr(ivhd.VENDOR_GI_DATE,7,2) as add_date,
ivbh.add_user_id as user_id,
ivdt.add_user_name_loc as user_name,
ivdt.inv_no||'/'||
CASE WHEN TRIM(TRANSLATE(ivdt.inv_seq, '0123456789', ' ')) IS NULL
THEN TO_CHAR(TO_NUMBER(ivdt.inv_seq))
ELSE ivdt.inv_seq
END AS inv_no,
ivdt.unit_measure as un ,
ivdt.inv_qty
from
icoypodt podt,
icomivbh ivbh,
icomivdt ivdt,
icomivhd ivhd
where
IVHD.HOUSE_CODE = '100'
AND podt.po_no=ivdt.po_no
AND IVDT.VENDOR_CODE = '100011'
and podt.po_seq= ivdt.po_seq
and ivdt.inv_no = ivbh.INV_NO
and ivdt.inv_seq=ivbh.inv_seq
and ivdt.inv_no=ivhd.inv_no
and podt.po_no=ivbh.po_no
and podt.po_seq=ivbh.po_seq
and podt.status !='D'
and ivbh.status !='D'
and ivdt.status !='D'
and ivhd.status !='D'
AND IVDT.INV_NO = '1800631513'
AND IVDT.INV_SEQ = '000010'
AND IVBH.BATCH_NO = '1408290206'
AND IVBH.BATCH_SEQ = '000001') TMP, COPY_T TT
where
rownum <='8'
오라클 쿼리문입니다.
그리고 첨부한 그림이 결과 값이고요
노란색의 빨간글씨같이 갯수가 저렇게 나오게 쿼리문튜닝즘
부탁드립니다.
오라클에 돌리는 11초나 걸리는데 너무 오래걸리는 것 같아서요 ㅠㅠ
TMP 가 1건 가져오는게 아닐까 생각되는데? 맞나요?
네 맞습니다. 1건의 정보를4개의 테이블에 저장되어있어 이걸 함친 다음에 수량을 노나서 출력하는 것입니다.
1건 가져오는데 5초 걸린다면? 적절한 인덱스가 없는듯 한데요?
해당 테이블의 인덱스들은 존재 합니다. 각각의 대표 PK키들이 인덱스인데...
두개 Union 쿼리를 보면 TMP 부분은 동일하고
- TO_NUMBER('390') 과 rownum <='3'
- TO_NUMBER('130') 과 rownum <='8'
이렇게 차이가 나네요?
조건절을 다음과 같이 바꿔 보세요.
SELECT * FROM icoypodt podt , icomivbh ivbh , icomivdt ivdt , icomivhd ivhd WHERE ivbh.inv_no = ivdt.inv_no AND ivbh.inv_seq = ivdt.inv_seq AND ivbh.house_code = ivdt.house_code AND ivbh.po_no = podt.po_no AND ivbh.po_seq = podt.po_seq AND ivbh.house_code = podt.house_code AND ivbh.inv_no = ivhd.inv_no AND ivbh.house_code = ivhd.house_code AND ivbh.house_code = '100' AND ivbh.inv_no = '1800631513' AND ivbh.inv_seq = '000010' AND ivbh.batch_no = '1408290206' AND ivbh.batch_seq = '000001' AND podt.status != 'D' AND ivbh.status != 'D' AND ivdt.status != 'D' AND ivhd.status != 'D' ;
SELECT a.inv_qty , barcode_sub ||'-' || CASE WHEN a.inv_qty >= b.x * c.no THEN b.x ELSE MOD(a.inv_qty, b.x) END ||'-'|| un || barcode1 AS barcode1 , barcode2 , barcode2_name , barcode_sub ||'-' || CASE WHEN a.inv_qty >= b.x * c.no THEN b.x ELSE MOD(a.inv_qty, b.x) END ||'-'|| un || barcode1 || barcode2 AS barcode3 , mat_no , bat_no , Des , CASE WHEN a.inv_qty >= b.x * c.no THEN b.x ELSE MOD(a.inv_qty, b.x) END ||' '|| un AS qty , add_date , user_id , user_name , inv_no FROM (SELECT ivbh.batch_seq , podt.item_no ||'-'|| ivbh.batch_no AS barcode_sub , NVL2(podt.sales_no, '-E-' , '') AS barcode1 , NVL2(podt.sales_no, podt.sales_no || podt.sales_seq , '') AS barcode2 , NVL2(podt.sales_no, podt.sales_no ||'-'|| LTRIM(podt.sales_seq, '0'), '') AS barcode2_name , podt.item_no AS mat_no , ivbh.batch_no AS bat_no , ivdt.description_loc AS Des , TO_CHAR(TO_DATE(ivhd.vendor_gi_date , 'yyyymmdd'), 'yyyy/mm/dd') AS add_date , ivbh.add_user_id AS user_id , ivdt.add_user_name_loc AS user_name , ivdt.inv_no ||'/'|| LTRIM(ivdt.inv_seq, '0') AS inv_no , ivdt.unit_measure AS un , ivdt.inv_qty FROM icoypodt podt , icomivbh ivbh , icomivdt ivdt , icomivhd ivhd WHERE ivbh.inv_no = ivdt.inv_no AND ivbh.inv_seq = ivdt.inv_seq AND ivbh.house_code = ivdt.house_code AND ivbh.po_no = podt.po_no AND ivbh.po_seq = podt.po_seq AND ivbh.house_code = podt.house_code AND ivbh.inv_no = ivhd.inv_no AND ivbh.house_code = ivhd.house_code AND ivbh.house_code = '100' AND ivbh.inv_no = '1800631513' AND ivbh.inv_seq = '000010' AND ivbh.batch_no = '1408290206' AND ivbh.batch_seq = '000001' AND podt.status != 'D' AND ivbh.status != 'D' AND ivdt.status != 'D' AND ivhd.status != 'D' ) a , (SELECT 1 no, 390 x FROM dual UNION ALL SELECT 2, 130 FROM dual ) b , copy_t c WHERE c.no <= CEIL(a.inv_qty / b.x) ORDER BY b.no, c.no ;