Related
I am new to SQL performance tuning. Below is the query that I need to enhance. Can someone please guide me on what or where should I start improving this query. I can only tweak this on query level, I am not allowed to alter the tables used in this SQL.
This is running on Oracle 12g.
This query is running for almost 10 hrs.
If possible, I want to make this query runs significantly with lower runtime.
cursor cursor2 is --logic 2
SELECT /*+ PARALLEL (PRD, 36) */
AST.ASSET_NUM
,AST.STATUS_CD
,PRD.NAME
,PR_ATTR.CHAR_VAL
,U_ATTR.CHAR_VAL
,SRV_AD.ADDR
,SRV_AD.ADDR_LINE_2
,SRV_AD.CITY
,UPPER(SRV_AD.STATE)
,SRV_AD.ZIPCODE
,SRV_AD.COUNTRY
,CX_ACT.OU_NUM
,CX_ACT.X_CCNA_ACNA_FRP
,CX_ACT.NAME
,CX_ACT.OU_TYPE_CD
,CX_ACT_x.ATTRIB_46
,BILL_ACCT.OU_NUM
,DECODE(LIFE_LINE.LIFE_ELIG_FLG, 'Y', 'Y', 'N')
,SR_AC_X.X_LOC_NUM
,SR_AC_X.X_BOOTH_LOC
,SR_AC_X.X_COINLESS_IND
,SERV_ACCT.X_WIRE_CENTER_FRP
,SALES_ORD.ORDER_NUM
,SALES_ORD_X.ATTRIB_06
,TO_CHAR(GREATEST ( NVL(AST.LAST_UPD, TO_DATE('01011900', 'MMDDYYYY'))
, NVL(PRD.LAST_UPD, TO_DATE('01011900', 'MMDDYYYY'))
, NVL(PR_ATTR.LAST_UPD, TO_DATE('01011900', 'MMDDYYYY'))
, NVL(U_ATTR.LAST_UPD, TO_DATE('01011900', 'MMDDYYYY'))
, NVL(SERV_ACCT.LAST_UPD, TO_DATE('01011900',
'MMDDYYYY'))
, NVL(SR_AC_X.LAST_UPD, TO_DATE('01011900', 'MMDDYYYY'))
, NVL(SRV_AD.LAST_UPD, TO_DATE('01011900', 'MMDDYYYY'))
, NVL(CX_ACT.LAST_UPD, TO_DATE('01011900', 'MMDDYYYY'))
, NVL(CX_ACT_X.LAST_UPD, TO_DATE('01011900', 'MMDDYYYY'))
, NVL(LIFE_LINE.LAST_UPD, TO_DATE('01011900', 'MMDDYYYY'))
, NVL(BILL_ACCT.LAST_UPD, TO_DATE('01011900', 'MMDDYYYY'))), 'DD-MM-YYYY HH24:MI:SS')
,CA.NC
,CA.NCI
,CA.SECNCI
,CA.NC1
,CA.ACT
,CA.SPEC
,CA.PIU
,CA.ACTI
,CA.CCNA
FROM A.S_ASSET AST
, A.S_PRD_INT PRD
, A.S_ASSET_XA PR_ATTR
, A.S_ASSET_XA U_ATTR
, A.S_ORG_EXT SERV_ACCT
, A.S_ORG_EXT_X SR_AC_X
, A.S_ADDR_PER SRV_AD
, A.S_ORG_EXT CX_ACT
, A.S_ORG_EXT_X CX_ACT_X
, A.S_SUBSIDY LIFE_LINE
, A.S_ORG_EXT BILL_ACCT
, A.S_BU BU
, A.S_ORDER SALES_ORD
, A.S_ORDER_X SALES_ORD_X
, B.CAMP_TEMP_XA CA
WHERE AST.ROW_ID = CA.ASSET_ROW_ID (+)
AND AST.ROW_ID = AST.ROOT_ASSET_ID
AND (AST.PRD_ID = PRD.ROW_ID AND PRD.part_num IN ('R', 'B', 'R_D', 'B_D', 'ND'))
AND (AST.ROW_ID = PR_ATTR.ASSET_ID AND PR_ATTR.ATTR_NAME = 'NUMBER')
AND (AST.ROW_ID = U_ATTR.ASSET_ID AND U_ATTR.ATTR_NAME = 'UNIVERSE')
AND AST.SERV_ACCT_ID = SERV_ACCT.ROW_ID (+)
AND SERV_ACCT.PAR_ROW_ID = SR_AC_X.PAR_ROW_ID (+)
AND SERV_ACCT.PR_ADDR_ID = SRV_AD.ROW_ID (+)
AND AST.OWNER_ACCNT_ID = CX_ACT.ROW_ID (+)
AND CX_ACT.PAR_ROW_ID = CX_ACT_X.PAR_ROW_ID (+)
AND CX_ACT.ROW_ID = LIFE_LINE.ACCNT_ID (+)
AND AST.X_FIRST_ORDER_NUM = SALES_ORD.ROW_ID (+)
AND SALES_ORD.ROW_ID = SALES_ORD_X.PAR_ROW_ID (+)
AND AST.BILL_ACCNT_ID = BILL_ACCT.ROW_ID (+)
AND SERV_ACCT.BU_ID = BU.ROW_ID
AND UPPER(SRV_AD.STATE) IN ( 'AB', 'CD', 'EF')
AND PR_ATTR.CHAR_VAL IS NOT NULL
AND BU.NAME <> 'WS';
I am trying to write a BQ Merge Query which does something like an upsert. I have a main table(M) and an identical delta table(D) which brings changed records. The merge statement should work like if the keys match it will update all the table fields in the main table with the delta table data and if the keys don't match it will do an insert of the delta table values. I was able to get the Merge statement to work but it seems too long. Is there a shorter way to do this . I have more than 190 columns in my table. Please see the Merge code below.
merge zsac_figl01.zsaplikp02 M
using zsac_figl01.zsaplikp02_delta D
on M.VBELN = D.VBELN
WHEN MATCHED THEN
UPDATE
SET
M.VBELN = D.VBELN ,
M.ERNAM = D.ERNAM ,
M.ERZET = D.ERZET ,
M.ERDAT = D.ERDAT ,
M.BZIRK = D.BZIRK ,
M.VSTEL = D.VSTEL ,
M.VKORG = D.VKORG ,
M.LFART = D.LFART ,
M.AUTLF = D.AUTLF ,
M.KZAZU = D.KZAZU ,
M.WADAT = D.WADAT ,
M.LDDAT = D.LDDAT ,
M.TDDAT = D.TDDAT ,
M.LFDAT = D.LFDAT ,
M.KODAT = D.KODAT ,
M.ABLAD = D.ABLAD ,
M.INCO1 = D.INCO1 ,
M.INCO2 = D.INCO2 ,
M.EXPKZ = D.EXPKZ ,
M.ROUTE = D.ROUTE ,
M.FAKSK = D.FAKSK ,
M.LIFSK = D.LIFSK ,
M.VBTYP = D.VBTYP ,
M.KNFAK = D.KNFAK ,
M.TPQUA = D.TPQUA ,
M.TPGRP = D.TPGRP ,
M.LPRIO = D.LPRIO ,
M.VSBED = D.VSBED ,
M.KUNNR = D.KUNNR ,
M.KUNAG = D.KUNAG ,
M.KDGRP = D.KDGRP ,
M.STZKL = D.STZKL ,
M.STZZU = D.STZZU ,
M.BTGEW = D.BTGEW ,
M.NTGEW = D.NTGEW ,
M.GEWEI = D.GEWEI ,
M.VOLUM = D.VOLUM ,
M.VOLEH = D.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_SYS,
D.SPE_CHNG_SYS,
D.SPE_GEOROUTE,
D.SPE_GEOROUTEIND,
D.SPE_CARRIER_IND,
D.SPE_GTS_REL,
D.SPE_GTS_RT_CDE,
D.SPE_REL_TMSTMP,
D.SPE_UNIT_SYSTEM,
D.SPE_INV_BFR_GI,
D.SPE_QI_STATUS,
D.SPE_RED_IND,
D.SAKES,
D.SPE_LIFEX_TYPE,
D.SPE_TTYPE,
D.SPE_PRO_NUMBER,
D.LOC_GUID,
D.SPE_BILLING_IND,
D.PRINTER_PROFILE,
D.MSR_ACTIVE,
D.PRTNR,
D.STGE_LOC_CHANGE,
D.TM_CTRL_KEY,
D.DLV_SPLIT_INITIA,
D.DLV_VERSION,
D.BEV1_LULEINH,
D.BEV1_RPFAESS,
D.BEV1_RPKIST,
D.BEV1_RPCONT,
D.BEV1_RPSONST,
D.BEV1_RPFLGNR,
D.BORGR_GRP,
D.ZLAND1,
D.ZZTVGW,
D.ZZVTW,
D.ZZFRGEW,
D.ZZANZPAL,
D.ZZKDKG1,
D.ZZVOLLPAL
)`
if the keys match it will update all the table fields in the main table with the delta table data and if the keys don't match it will do an insert of the delta table values
Based on above I assume the schema of main and delta table are identical
So, you can explore option of doing DELETE and INSERT instead of UPDATE and INSERT
Something like
DELETE zsac_figl01.zsaplikp02
WHERE VBELN IN (SELECT VBELN FROM zsac_figl01.zsaplikp02_delta)
And then (note - Omitting column names is in Beta since June 3, 2019, so you should try )
INSERT zsac_figl01.zsaplikp02
SELECT * FROM zsac_figl01.zsaplikp02_delta
I am just getting back into writing SQL Select Statements and I am have an issue. I have an existing report that works perfectly. However, I was asked to add some fields in the result set. The filed is in a table with a foreign key relationship. I am trying to add it to the query and I am failing. I can write the select statement separately and it returns the right results. I just don't know how to add it to the existing statement. I have tried left joins, adding it to the select and where but the result set is wrong. I would like to add the following to return the info if available to the result set in a new column but not prevent other data if null.
I would like to add:
select sc.FACILITY_ID, ce.schd_id
from PA_SCHED sc, PA_CPNT_EVTHST ce
where sc.SCHD_ID = ce.schd_id
To the following:
select s.stud_id as studID, s.lname as lastName, s.fname as firstName, s.mi
as middleName,
s.EMP_STAT_ID ,s.EMP_TYP_ID ,s.JL_ID ,s.JP_ID ,s.TARGET_JP_ID ,
s.JOB_TITLE ,s.DMN_ID ,s.ORG_ID ,s.REGION_ID, s.CO_ID , DECODE
(s.NOTACTIVE,'Y','N','N','Y') as active ,
s.ADDR ,s.CITY ,s.STATE ,s.POSTAL ,s.CNTRY, s.SUPER , s.COACH_STUD_ID ,
s.HIRE_DTE,s.TERM_DTE ,s.EMAIL_ADDR, s.RESUME_LOCN ,s.COMMENTS ,
s.SHIPPING_NAME ,
s.SHIPPING_CONTACT_NAME,
s.SHIPPING_ADDR ,s.SHIPPING_ADDR1 ,s.SHIPPING_CITY ,
s.SHIPPING_STATE,
s.SHIPPING_POSTAL ,s.SHIPPING_CNTRY ,s.SHIPPING_PHON_NUM,
s.SHIPPING_FAX_NUM,
s.SHIPPING_EMAIL_ADDR ,s.STUD_PSWD ,s.PIN ,s.PIN_DATE,
s.ENCRYPTED , s.HAS_ACCESS ,s.BILLING_NAME , s.BILLING_CONTACT_NAME ,
s.BILLING_ADDR ,s.BILLING_ADDR1 ,s.BILLING_CITY ,s.BILLING_STATE ,
s.BILLING_POSTAL,
s.BILLING_CNTRY ,
s.BILLING_PHON_NUM ,s.BILLING_FAX_NUM ,s.BILLING_EMAIL_ADDR ,
s.SELF_REGISTRATION ,s.SELF_REGISTRATION_DATE, s.ACCESS_TO_ORG_FIN_ACT ,
s.NOTIFY_DEV_PLAN_ITEM_ADD , s.NOTIFY_DEV_PLAN_ITEM_MOD ,
s.NOTIFY_DEV_PLAN_ITEM_REMOVE ,
s.NOTIFY_WHEN_SUB_ITEM_COMPLETE ,s.NOTIFY_WHEN_SUB_ITEM_FAILURE ,
s.LOCKED ,s.PASSWORD_EXP_DATE,s.SECURITY_QUESTION ,
s.SECURITY_ANSWER ,s.ROLE_ID ,s.IMAGE_ID ,s.GENDER ,s.PAST_SERVICE,
s.LST_UNLOCK_TSTMP,
s.LST_UPD_USR, s.LST_UPD_TSTMP,s.MANAGE_SUB_SP, s.MANAGE_OWN_SP,
e.cpnt_typ_id as cpntTypeID, e.cpnt_id as cpntID, e.rev_dte as revDate,
e.rev_num as revNum,
e.schd_id as schedID, cp.cpnt_title as cpntDesc, e.grade,
e.compl_dte as complDate, e.cmpl_stat_id as complStatID,
cs.cmpl_stat_desc as complStatDesc, nvl(e.total_hrs, 0) as totalHrs,
nvl(e.credit_hrs, 0) as creditHrs, nvl(e.contact_hrs, 0) as contactHrs,
nvl(e.cpe_hrs, 0) as cpeHrs, nvl(oi.price, 0) as tuition,
e.inst_name as instName, e.comments as eventComments, e.es_user_name as
esUserName,
e.lst_upd_tstmp as lstTmsp,
e.esig_meaning_code_id || ' ' || e.esig_meaning_code_desc as
esigMeaningCode,
e.currency_code as currencyCode, cur.symbol as currencySymbol,
cur.description,
cur.active, cur.is_default, cur.label_id
,s.jl_id
,s.job_title
,s.super
,s.hire_dte
,s.term_dte
,eeno.user_value
,co.user_value
from pa_student s, PA_CPNT_EVTHST e, PA_CMPL_STAT cs,
pa_fin_order_item oi, pa_currency cur, pa_cpnt cp,
(SELECT s.stud_id, f.user_value
FROM PA_STUD_USER f, pa_student s
WHERE s.stud_id = f.stud_id (+)
AND f.col_num (+) = '10') eeno
,(SELECT s.stud_id, f.user_value
FROM PA_STUD_USER f, pa_student s
WHERE s.stud_id = f.stud_id (+)
AND f.col_num (+) = '40') co
where e.stud_id = s.stud_id
and e.cmpl_stat_id = cs.cmpl_stat_id
and e.currency_code = cur.currency_code(+)
and e.order_item_id = oi.order_item_id(+)
and cp.cpnt_typ_id(+) = e.cpnt_typ_id
and cp.cpnt_id(+) = e.cpnt_id
and cp.rev_dte(+) = e.rev_dte
and s.stud_id = eeno.stud_id(+)
AND s.stud_id = co.stud_id(+)
/** and trunc(e.compl_dte) >= [FromDate]
and not trunc(e.compl_dte) > [ToDate]
and s.stud_id in [UserSearch]
and e.cpnt_typ_id in [ItemTypeSearch]
and [security:pa_student s] */
Thank you for your help.
Added this in FROM ...
(select sc.FACILITY_ID, ce.schd_id from PA_SCHED sc
where sc.SCHD_ID (+) = e.schd_id
) sc
and appended sc.FACILITY_ID in the main SELECT ...
See if this works ...
SELECT s.stud_id AS studID,
s.lname AS lastName,
s.fname AS firstName,
s.mi AS middleName,
s.EMP_STAT_ID ,
s.EMP_TYP_ID ,
s.JL_ID ,
s.JP_ID ,
s.TARGET_JP_ID ,
s.JOB_TITLE ,
s.DMN_ID ,
s.ORG_ID ,
s.REGION_ID,
s.CO_ID ,
DECODE (s.NOTACTIVE,'Y','N','N','Y') AS active ,
s.ADDR ,
s.CITY ,
s.STATE ,
s.POSTAL ,
s.CNTRY,
s.SUPER ,
s.COACH_STUD_ID ,
s.HIRE_DTE,
s.TERM_DTE ,
s.EMAIL_ADDR,
s.RESUME_LOCN ,
s.COMMENTS ,
s.SHIPPING_NAME ,
s.SHIPPING_CONTACT_NAME,
s.SHIPPING_ADDR ,
s.SHIPPING_ADDR1 ,
s.SHIPPING_CITY ,
s.SHIPPING_STATE,
s.SHIPPING_POSTAL ,
s.SHIPPING_CNTRY ,
s.SHIPPING_PHON_NUM,
s.SHIPPING_FAX_NUM,
s.SHIPPING_EMAIL_ADDR ,
s.STUD_PSWD ,
s.PIN ,
s.PIN_DATE,
s.ENCRYPTED ,
s.HAS_ACCESS ,
s.BILLING_NAME ,
s.BILLING_CONTACT_NAME ,
s.BILLING_ADDR ,
s.BILLING_ADDR1 ,
s.BILLING_CITY ,
s.BILLING_STATE ,
s.BILLING_POSTAL,
s.BILLING_CNTRY ,
s.BILLING_PHON_NUM ,
s.BILLING_FAX_NUM ,
s.BILLING_EMAIL_ADDR ,
s.SELF_REGISTRATION ,
s.SELF_REGISTRATION_DATE,
s.ACCESS_TO_ORG_FIN_ACT ,
s.NOTIFY_DEV_PLAN_ITEM_ADD ,
s.NOTIFY_DEV_PLAN_ITEM_MOD ,
s.NOTIFY_DEV_PLAN_ITEM_REMOVE ,
s.NOTIFY_WHEN_SUB_ITEM_COMPLETE ,
s.NOTIFY_WHEN_SUB_ITEM_FAILURE ,
s.LOCKED ,
s.PASSWORD_EXP_DATE,
s.SECURITY_QUESTION ,
s.SECURITY_ANSWER ,
s.ROLE_ID ,
s.IMAGE_ID ,
s.GENDER ,
s.PAST_SERVICE,
s.LST_UNLOCK_TSTMP,
s.LST_UPD_USR,
s.LST_UPD_TSTMP,
s.MANAGE_SUB_SP,
s.MANAGE_OWN_SP,
e.cpnt_typ_id AS cpntTypeID,
e.cpnt_id AS cpntID,
e.rev_dte AS revDate,
e.rev_num AS revNum,
e.schd_id AS schedID,
cp.cpnt_title AS cpntDesc,
e.grade,
e.compl_dte AS complDate,
e.cmpl_stat_id AS complStatID,
cs.cmpl_stat_desc AS complStatDesc,
NVL(e.total_hrs, 0) AS totalHrs,
NVL(e.credit_hrs, 0) AS creditHrs,
NVL(e.contact_hrs, 0) AS contactHrs,
NVL(e.cpe_hrs, 0) AS cpeHrs,
NVL(oi.price, 0) AS tuition,
e.inst_name AS instName,
e.comments AS eventComments,
e.es_user_name AS esUserName,
e.lst_upd_tstmp AS lstTmsp,
e.esig_meaning_code_id
|| ' '
|| e.esig_meaning_code_desc AS esigMeaningCode,
e.currency_code AS currencyCode,
cur.symbol AS currencySymbol,
cur.description,
cur.active,
cur.is_default,
cur.label_id ,
s.jl_id ,
s.job_title ,
s.super ,
s.hire_dte ,
s.term_dte ,
eeno.user_value ,
co.user_value,
e.schd_id,
sc.FACILITY_ID
FROM pa_student s,
PA_CPNT_EVTHST e,
PA_CMPL_STAT cs,
pa_fin_order_item oi,
pa_currency cur,
pa_cpnt cp,
(SELECT s.stud_id,
f.user_value
FROM PA_STUD_USER f,
pa_student s
WHERE s.stud_id = f.stud_id (+)
AND f.col_num (+) = '10'
) eeno ,
(SELECT s.stud_id,
f.user_value
FROM PA_STUD_USER f,
pa_student s
WHERE s.stud_id = f.stud_id (+)
AND f.col_num (+) = '40'
) co ,
(select sc.FACILITY_ID, ce.schd_id from PA_SCHED sc
where sc.SCHD_ID (+) = e.schd_id
) sc
WHERE e.stud_id = s.stud_id
AND e.cmpl_stat_id = cs.cmpl_stat_id
AND e.currency_code = cur.currency_code(+)
AND e.order_item_id = oi.order_item_id(+)
AND cp.cpnt_typ_id(+) = e.cpnt_typ_id
AND cp.cpnt_id(+) = e.cpnt_id
AND cp.rev_dte(+) = e.rev_dte
AND s.stud_id = eeno.stud_id(+)
AND s.stud_id = co.stud_id(+);
I would really appreciate your help in tuning the below SQL query. It kept running for 10 mins when I cancelled it.
MARC_SEL gave me 31,253 records in 52 seconds
and MVKE_SEL gave me 431,060 records in 22 seconds
I refactored it to use with clause but nothing much changed.What else can I incorporate to make it faster. Please help.
WITH ALL_XSAP_MATNR
AS (SELECT DISTINCT XSAP.MATNR,XSAP.MTART,XSAP.SOURCE FROM XXX_MAIN.XXX_XSAP XSAP
WHERE SOURCE = 'SP' )
, MARC_SEL AS
( SELECT DISTINCT A.SOURCE
,MARA.MATNR
,MARA.MTART
,MARA.MBRSH
,MARC.WERKS
,NVL(PX.WERKS,'/') DWERK
,NVL(MBEW.HKMAT,'/') HKMAT
,NVL(MBEW.EKALR,'/') EKALR
,NVL(MARC.STAWN,'/') STAWN
FROM ALL_XSAP_MATNR A
, XXX_MAIN.XXX_SAP_MARA MARA
, XXX_MAIN.XXX_SAP_MARC MARC
, XXX_MAIN.XXX_MP_WERKS_PLANT_XREF PX
, XXX_MAIN.XXX_SAP_MBEW MBEW
WHERE A.MATNR = MARA.MATNR
AND A.MTART = MARA.MTART
AND MARA.MATNR = MARC.MATNR
AND MARC.MATNR = MBEW.MATNR
AND MARC.WERKS = MBEW.BWKEY
AND PX.LEGACY_PLANT = MARC.WERKS
AND PX.SOURCE = 'SP'
)
, MVKE_SEL AS
( SELECT DISTINCT
MVKE.MATNR
,'/' LEGACY_ORG
,'/' LEGACY_MATNR
,NVL(MX_VKORG.SAP_DE_VAL,'/') VKORG
,NVL(SUBSTR(MX_VKORG.SAP_DE,6,2),'/') VTWEG
-- ,NVL(TVRKME.MSEH3,'/') VRKME
,NVL(MVKE.KONDM,'/') KONDM
,NVL(MVKE.VERSG,'/') VERSG
,'/' IPRKZ
,'/' MHDRZ,NVL(MVKE.VMSTA,'/') VMSTA
,NVL(TO_CHAR(MVKE.VMSTD ,'YYYYMMDD' ),'/') VMSTD
,NVL(MVKE.PMATN,'/') PMATN
,NVL(MVKE.MVGR2,'/') MVGR2
,NVL(MVKE.MVGR3,'/') MVGR3
,NVL(MVKE.VAVME,'/') VAVME
,'/' MVGR4
,'/' MVGR5
,NVL(MVKE.MTPOS,'/') MTPOS
,NVL(MVKE.PRAT1,'/') PRAT1
,NVL(MVKE.SKTOF,'/') SKTOF
,'/' AUMNG
,NVL(MVKE.PRODH,'/') PRODH
,'/' MVGR1
,NVL(MVKE.KTGRM,'/') KTGRM
,MX_VKORG.DESC4
FROM XXX_MAIN.XXX_SAP_MVKE MVKE
, XXX_MAIN.XXX_MP_VKVT_XREF MX_VKORG
WHERE MX_VKORG.SOURCE_DE_VAL = MVKE.VKORG
AND SUBSTR(MX_VKORG.SAP_DE,6,2) = MVKE.VTWEG
AND MX_VKORG.SOURCE_TBL = 'SP'
AND MX_VKORG.SOURCE_DE = 'MVKE'
AND SUBSTR(MX_VKORG.SAP_DE,1,5)= 'VKORG'
AND MX_VKORG.DESC2 IS NULL )
SELECT DISTINCT
MARC.SOURCE
,MARC.MATNR
,MARC.MTART
,MARC.MBRSH
,MARC.WERKS
,MARC.DWERK
,MARC.HKMAT
,MARC.EKALR
,MARC.STAWN
,MVKE.LEGACY_ORG
,MVKE.LEGACY_MATNR
,MVKE.VKORG
,MVKE.VTWEG
,MVKE.KONDM
,MVKE.VERSG
,MVKE.VMSTA
,MVKE.VMSTD
,MVKE.PMATN
,MVKE.MVGR2
,MVKE.MVGR3
,MVKE.VAVME
,MVKE.MTPOS
,MVKE.PRAT1
,MVKE.SKTOF
,MVKE.PRODH
,MVKE.KTGRM
FROM MARC_SEL MARC
, MVKE_SEL MVKE
WHERE MARC.MATNR = MVKE.MATNR
AND MARC.WERKS = MVKE.DESC4
Start by simplifying your query as you do not need to do multiple DISTINCTs (it's only necessary in the final output) and you are selecting many columns that you are not outputting. You are also joining some tables which you are not selecting from and if there are multiple matching rows for these then it may generate duplicate rows - using something like EXISTS can eliminate these joins.
Like this:
WITH MARC_SEL AS (
SELECT A.SOURCE,
MARA.MATNR,
MARC.WERKS
FROM XXX_MAIN.XXX_XSAP A
INNER JOIN XXX_MAIN.XXX_SAP_MARA MARA
ON ( A.MATNR = MARA.MATNR
AND A.MTART = MARA.MTART )
INNER JOIN XXX_MAIN.XXX_SAP_MARC MARC
ON ( MARA.MATNR = MARC.MATNR )
WHERE EXISTS( SELECT 'X'
FROM XXX_MAIN.XXX_MP_WERKS_PLANT_XREF PX
WHERE PX.LEGACY_PLANT = MARC.WERKS
AND PX.SOURCE = 'SP' )
AND EXISTS( SELECT 'X'
FROM XXX_MAIN.XXX_SAP_MBEW MBEW
WHERE MARC.MATNR = MBEW.MATNR
AND MARC.WERKS = MBEW.BWKEY )
AND A.SOURCE = 'SP'
)
, MVKE_SEL AS (
SELECT NVL(MX_VKORG.SAP_DE_VAL,'/') VKORG,
NVL(SUBSTR(MX_VKORG.SAP_DE,6,2),'/') VTWEG,
MX_VKORG.DESC4
FROM XXX_MAIN.XXX_MP_VKVT_XREF MX_VKORG
WHERE EXISTS ( SELECT 'X'
FROM XXX_MAIN.XXX_SAP_MVKE MVKE
WHERE MX_VKORG.SOURCE_DE_VAL = MVKE.VKORG
AND SUBSTR(MX_VKORG.SAP_DE,6,2) = MVKE.VTWEG )
AND MX_VKORG.SOURCE_TBL = 'SP'
AND MX_VKORG.SOURCE_DE = 'MVKE'
AND SUBSTR(MX_VKORG.SAP_DE,1,5)= 'VKORG'
AND MX_VKORG.DESC2 IS NULL
)
SELECT DISTINCT
MARC.SOURCE,
MARC.MATNR,
MVKE.VKORG,
MARC.WERKS,
MVKE.VTWEG
FROM MARC_SEL MARC
INNER JOIN MVKE_SEL MVKE
ON ( MARC.MATNR = MVKE.MATNR
AND MARC.WERKS = MVKE.DESC4 )
Added hints to subqueries and it came back in a minute.
WITH ALL_XSAP_MATNR
AS (SELECT /*+ materialize */ DISTINCT XSAP.MATNR,XSAP.MTART,XSAP.SOURCE FROM XXX_MAIN.XXX_XSAP XSAP
WHERE SOURCE = 'SP' )
, MARC_SEL AS
( SELECT /*+ materialize */ DISTINCT A.SOURCE
,MARA.MATNR
,MARA.MTART
,MARA.MBRSH
,MARC.WERKS
,NVL(PX.WERKS,'/') DWERK
,NVL(MBEW.HKMAT,'/') HKMAT
,NVL(MBEW.EKALR,'/') EKALR
,NVL(MARC.STAWN,'/') STAWN
FROM ALL_XSAP_MATNR A
, XXX_MAIN.XXX_SAP_MARA MARA
, XXX_MAIN.XXX_SAP_MARC MARC
, XXX_MAIN.XXX_MP_WERKS_PLANT_XREF PX
, XXX_MAIN.XXX_SAP_MBEW MBEW
WHERE A.MATNR = MARA.MATNR
AND A.MTART = MARA.MTART
AND MARA.MATNR = MARC.MATNR
AND MARC.MATNR = MBEW.MATNR
AND MARC.WERKS = MBEW.BWKEY
AND PX.LEGACY_PLANT = MARC.WERKS
AND PX.SOURCE = 'SP'
)
, MVKE_SEL AS
( SELECT /*+ materialize */ DISTINCT
MVKE.MATNR
,'/' LEGACY_ORG
,'/' LEGACY_MATNR
,NVL(MX_VKORG.SAP_DE_VAL,'/') VKORG
,NVL(SUBSTR(MX_VKORG.SAP_DE,6,2),'/') VTWEG
-- ,NVL(TVRKME.MSEH3,'/') VRKME
,NVL(MVKE.KONDM,'/') KONDM
,NVL(MVKE.VERSG,'/') VERSG
,'/' IPRKZ
,'/' MHDRZ,NVL(MVKE.VMSTA,'/') VMSTA
,NVL(TO_CHAR(MVKE.VMSTD ,'YYYYMMDD' ),'/') VMSTD
,NVL(MVKE.PMATN,'/') PMATN
,NVL(MVKE.MVGR2,'/') MVGR2
,NVL(MVKE.MVGR3,'/') MVGR3
,NVL(MVKE.VAVME,'/') VAVME
,'/' MVGR4
,'/' MVGR5
,NVL(MVKE.MTPOS,'/') MTPOS
,NVL(MVKE.PRAT1,'/') PRAT1
,NVL(MVKE.SKTOF,'/') SKTOF
,'/' AUMNG
,NVL(MVKE.PRODH,'/') PRODH
,'/' MVGR1
,NVL(MVKE.KTGRM,'/') KTGRM
,MX_VKORG.DESC4
FROM XXX_MAIN.XXX_SAP_MVKE MVKE
, XXX_MAIN.XXX_MP_VKVT_XREF MX_VKORG
WHERE MX_VKORG.SOURCE_DE_VAL = MVKE.VKORG
AND SUBSTR(MX_VKORG.SAP_DE,6,2) = MVKE.VTWEG
AND MX_VKORG.SOURCE_TBL = 'SP'
AND MX_VKORG.SOURCE_DE = 'MVKE'
AND SUBSTR(MX_VKORG.SAP_DE,1,5)= 'VKORG'
AND MX_VKORG.DESC2 IS NULL )
SELECT DISTINCT /*+ use_hash(MARC,MVKE ) */
MARC.SOURCE
,MARC.MATNR
,MARC.MTART
,MARC.MBRSH
,MARC.WERKS
,MARC.DWERK
,MARC.HKMAT
,MARC.EKALR
,MARC.STAWN
,MVKE.LEGACY_ORG
,MVKE.LEGACY_MATNR
,MVKE.VKORG
,MVKE.VTWEG
,MVKE.KONDM
,MVKE.VERSG
,MVKE.VMSTA
,MVKE.VMSTD
,MVKE.PMATN
,MVKE.MVGR2
,MVKE.MVGR3
,MVKE.VAVME
,MVKE.MTPOS
,MVKE.PRAT1
,MVKE.SKTOF
,MVKE.PRODH
,MVKE.KTGRM
FROM MARC_SEL MARC
, MVKE_SEL MVKE
WHERE MARC.MATNR = MVKE.MATNR
AND MARC.WERKS = MVKE.DESC4
I need execute this SQL query in ZF2:
SELECT FA_VENTAS.EM_CODIGO,
FA_VENDED.VN_NOMBRE AS VENDEDOR ,
FA_CLIEN.CE_NOMBRE AS CLIENTE,
FA_TIPCLI.TC_DESCRI AS CANAL ,
MIN(FA_DETVEN.CL_NOMDEP) AS MARCA ,
IN_ITEM.IT_CODBAR AS CODBARRA ,
IN_ITEM.IT_NOMBRE AS PRODUCTO ,
TO_CHAR(FA_VENTAS.VE_FECHA,'MM')AS MESES ,
TO_CHAR(FA_VENTAS.VE_FECHA,'YYYY')AS ANIO ,
SUM(DV_SUBTOTAL-DV_VALIVA) AS VALOR ,
SUM(DV_CANPED) AS UNIDAD
FROM
DOUS.FA_VENTAS FA_VENTAS,
DOUS.FA_DETVEN FA_DETVEN,
DOUS.IN_ITEM IN_ITEM ,
DOUS.IN_CLASE IN_CLASE ,
DOUS.FA_VENDED FA_VENDED,
DOUS.FA_CLIEN FA_CLIEN,
DOUS.FA_TIPCLI FA_TIPCLI
WHERE
FA_TIPCLI.TC_CODIGO=FA_CLIEN.TC_CODIGO
AND FA_VENTAS.EM_CODIGO = FA_DETVEN.EM_CODIGO
AND FA_VENTAS.VE_CODIGO = FA_DETVEN.VE_CODIGO
AND FA_VENTAS.TP_CODIGO = FA_DETVEN.TP_CODIGO
AND FA_VENTAS.PE_CODIGO = FA_DETVEN.PE_CODIGO
AND FA_DETVEN.EM_CODIGO = IN_ITEM.EM_CODIGO
AND FA_DETVEN.IT_CODIGO = IN_ITEM.IT_CODIGO
AND IN_ITEM.EM_CODIGO = IN_CLASE.EM_CODIGO
AND IN_ITEM.CL_CODIGO = IN_CLASE.CL_CODIGO
AND FA_VENDED.VN_CODIGO=4
AND FA_VENTAS.CE_CODIGO=FA_CLIEN.CE_CODIGO
AND FA_VENTAS.EM_CODIGO = '4'
AND FA_VENTAS.TP_CODIGO = '2'
AND TRUNC(FA_VENTAS.VE_FECHA) between'01-jan-2009' and '31-jan-2009'
GROUP BY FA_VENTAS.EM_CODIGO,
FA_VENDED.VN_NOMBRE,
FA_CLIEN.CE_NOMBRE,
FA_DETVEN.CL_NOMDEP,
FA_VENTAS.VE_FECHA,
IN_ITEM.IT_CODBAR,
IN_ITEM.IT_NOMBRE,
FA_TIPCLI.TC_DESCRI
UNION ALL
SELECT FA_DEVOLU.EM_CODIGO,
FA_VENDED.VN_NOMBRE AS VENDEDOR ,
FA_CLIEN.CE_NOMBRE AS CLIENTE,
FA_TIPCLI.TC_DESCRI AS CANAL ,
MIN(FA_DETDEVOL.CL_NOMDEP)AS MARCA ,
IN_ITEM.IT_CODBAR AS CODBARRA ,
IN_ITEM.IT_NOMBRE AS PRODUCTO ,
TO_CHAR(FA_DEVOLU.DE_FECHA,'MM') AS MESES ,
TO_CHAR(FA_DEVOLU.DE_FECHA,'YYYY') AS ANIO ,
-SUM(FA_DETDEVOL.DD_SUBTOTAL- FA_DETDEVOL.DD_VALIVA) AS VALOR ,
-SUM(DD_CANTID) AS UNIDAD
FROM
DOUS.FA_DEVOLU,
DOUS.FA_DETDEVOL,
DOUS.IN_ITEM IN_ITEM ,
DOUS.IN_CLASE IN_CLASE ,
DOUS.FA_VENDED FA_VENDED,
DOUS.FA_CLIEN FA_CLIEN,
DOUS.FA_TIPCLI FA_TIPCLI
WHERE
FA_TIPCLI.TC_CODIGO=FA_CLIEN.TC_CODIGO
AND FA_DEVOLU.EM_CODIGO = FA_DETDEVOL.EM_CODIGO
AND FA_DEVOLU.DE_CODIGO = FA_DETDEVOL.DE_CODIGO
AND FA_DEVOLU.TP_CODIGO = FA_DETDEVOL.TP_CODIGO
AND FA_DETDEVOL.EM_CODIGO = IN_ITEM.EM_CODIGO
AND FA_DETDEVOL.IT_CODIGO = IN_ITEM.IT_CODIGO
AND IN_ITEM.EM_CODIGO = IN_CLASE.EM_CODIGO
AND IN_ITEM.CL_CODIGO = IN_CLASE.CL_CODIGO
AND FA_DETDEVOL.EM_CODIGO = 4
AND FA_DETDEVOL.TP_CODIGO = '2'
AND TRUNC(FA_DEVOLU.DE_FECHA) between '01-jan-2009' and '31-jan-2009'
AND FA_VENDED.VN_CODIGO=4
AND FA_DEVOLU.CE_CODIGO=FA_CLIEN.CE_CODIGO
GROUP BY FA_DEVOLU.EM_CODIGO,
FA_VENDED.VN_NOMBRE,
FA_CLIEN.CE_NOMBRE,
FA_DETDEVOL.CL_NOMDEP,
FA_DEVOLU.DE_FECHA,
IN_ITEM.IT_CODBAR,
IN_ITEM.IT_NOMBRE,
FA_TIPCLI.TC_DESCRI
but I don't understand what model, view and command use.
Please help me
With a query like this, you should use \Zend\Db\Adapter\Adapter
$sql = "SELECT SELECT FA_VENTAS.EM_CODIGO, ...";
$statement = $adapter->query($sql);
$result = $statement->execute();
hth