Optimize SQL Server query for speed - sql

How can I optimize my SQL Server query ? Here is the code that I want to optimize
CREATE TABLE #Temp
(
TransactionId int PRIMARY KEY,
TransactionStepId int
)
INSERT INTO #Temp(TransactionId, TransactionStepId)
SELECT
TransactionId, MAX(TransactionStepId) TransactionStepId
FROM
[WarehouseMgmt].[FactPaymentTrans] FPT
JOIN
WarehouseMgmt.DimTimeZone DTZ on FPT.[TimeId] = DTZ.TimeUTCId
WHERE
FactType = 'SOURCE'
AND (DTZ.TimeId BETWEEN #DimStartDate AND #DimEndDate)
GROUP BY
TransactionId
IF(UPPER(#ReportBy) = 'TRANSACTION')
BEGIN
SET #sql = '
INSERT INTO #Results (
[PaymentTypeId],
[TransactionDate],
[PaymentMethodId],
[3DSecureId],
[ProductId],
[ProductTypeId],
[TransactionStatusId],
[Amount],
[Currency],
[PlayerId],
[PlayerSourceOrigId],
[Username],
[FirstName],
[LastName],
[BrandId],
[VIPLevelId],
[MarketingChannelId],
[MarketingSourceId],
[CommentId],
[CommentRefId],
[AdminName],
[OriginalTransactionId],
[TransactionId],
[RelatedTransactionId],
[ProviderTransactionOrigId]
)
SELECT
DTST.[Id],
FPT.[StartTime],
FPT.[PaymentMethodId],
FPT.[3DSecureId],
FPT.[ProductId],
DPT.[Id],
FPT.[TransactionStatusId],
FPT.[Amount],
FPT.CurrencyId,
FPT.[PlayerId],
DPL.[SourceOrigId],
DPL.[Username],
DPL.[FirstName],
DPL.[LastName],
DPL.[BrandId],
DPL.[VIPLevelId],
DPL.[MarketingChannelId],
DPL.[MarketingSourceId],
FPT.[PaymentReasonTextId],
FPT_Ref.[PaymentReasonTextId],
FPT.CreatedByAdminId,
FPT.[OriginalTransactionId],
FPT.[TransactionId],
FPT_Ref.[OriginalTransactionId],
FPT.[ProviderTransactionOrigId]
FROM WarehouseMgmt.FactPaymentTrans AS FPT
JOIN #Temp T ON FPT.TransactionId = T.TransactionId AND FPT.TransactionStepId = T.TransactionStepId
JOIN WarehouseMgmt.DimTransactionStepType AS DTST ON FPT.[TransactionStepTypeId] = DTST.[Id]
JOIN WarehouseMgmt.DimPlayer AS DPL ON FPT.PlayerId = DPL.Id
JOIN WarehouseMgmt.DimProduct AS DP ON DP.Id = FPT.ProductId
JOIN WarehouseMgmt.DimProductType AS DPT ON DPT.Id = DP.ProductTypeId
--JOIN WarehouseMgmt.DimTimeZone DTZ on FPT.[TimeId] = DTZ.TimeUTCId
JOIN [WarehouseMgmt].[DimLoyaltyProgramLevel] DLPL ON DLPL.[Id]=DPL.VipLevelId
LEFT JOIN
(
SELECT FPT_Ref_1.TransactionId,FPT_Ref_1.FactType,FPT_Ref_1.OriginalTransactionId,FPT_Ref_1.[PaymentReasonTextId]
FROM WarehouseMgmt.FactPaymentTrans AS FPT_Ref_1
JOIN #Temp T ON T.TransactionId = FPT_Ref_1.OriginalTransactionId AND T.TransactionStepId = FPT_Ref_1.TransactionStepId
) AS FPT_Ref ON FPT_Ref.OriginalTransactionId = FPT.TransactionId AND FPT_Ref.FactType = ''SOURCE''
WHERE (FPT.FactType = ''SOURCE'') ' + #sqlFilters
I tried to put this
SELECT TransactionId,MAX(TransactionStepId) TransactionStepId
FROM [WarehouseMgmt].[FactPaymentTrans]
WHERE FactType = ''SOURCE''
GROUP BY TransactionId
in a temp table, but this is even worst that without temp table. I want to select latest TransactionId(thats made by MAX(TransactionStepId) and also to select last TransactionId in the left JOIN
My execution plan is:

Have you considered using ROW_NUMBER instead of joining your table twice? Please try this:
;WITH FactPaymentTrans_Last
AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY TransactionID ORDER BY TransactionStepID DESC) AS RN
FROM WarehouseMgmt.FactPaymentTrans
)
SELECT DTST.[Id]
, FPT.[StartTime]
, FPT.[PaymentMethodId]
, FPT.[3DSecureId]
, FPT.[ProductId]
, DPT.[Id]
, FPT.[TransactionStatusId]
, FPT.[Amount]
, FPT.CurrencyId
, FPT.[PlayerId]
, DPL.[SourceOrigId]
, DPL.[Username]
, DPL.[FirstName]
, DPL.[LastName]
, DPL.[BrandId]
, DPL.[VIPLevelId]
, DPL.[MarketingChannelId]
, DPL.[MarketingSourceId]
, FPT.[PaymentReasonTextId]
, FPT_Ref.[PaymentReasonTextId]
, FPT.CreatedByAdminId
, FPT.[OriginalTransactionId]
, FPT.[TransactionId]
, FPT_Ref.[OriginalTransactionId]
, FPT.[ProviderTransactionOrigId]
FROM FactPaymentTrans_Last AS FPT
INNER JOIN WarehouseMgmt.DimTransactionStepType AS DTST
ON FPT.[TransactionStepTypeId] = DTST.[Id]
INNER JOIN WarehouseMgmt.DimPlayer AS DPL
ON FPT.PlayerId = DPL.Id
INNER JOIN WarehouseMgmt.DimProduct AS DP
ON DP.Id = FPT.ProductId
WHERE FPT.RN = 1;
It's just a snippet to give you idea how you can use this to get latest TransactionIds based on its TransactionStepId.
If it's not enough - please post these:
Your table structure
Indices on them
Your execution plan
It will help to give you suggestions.

There are multiple factors which can improve query performance and one of them which can be done, without necessarily knowing details about indexes, database schema, data distribution etc. is the order of the JOINs in the query.
I have refactored your query and I think you should get the same result as before, but with an improved execution time:
SELECT DTST.[Id]
,FPT.[StartTime]
,FPT.[PaymentMethodId]
,FPT.[3DSecureId]
,FPT.[ProductId]
,DPT.[Id]
,FPT.[TransactionStatusId]
,FPT.[Amount]
,FPT.CurrencyId
,FPT.[PlayerId]
,DPL.[SourceOrigId]
,DPL.[Username]
,DPL.[FirstName]
,DPL.[LastName]
,DPL.[BrandId]
,DPL.[VIPLevelId]
,DPL.[MarketingChannelId]
,DPL.[MarketingSourceId]
,FPT.[PaymentReasonTextId]
,FPT_Ref.[PaymentReasonTextId]
,FPT.CreatedByAdminId
,FPT.[OriginalTransactionId]
,FPT.[TransactionId]
,FPT_Ref.[OriginalTransactionId]
,FPT.[ProviderTransactionOrigId]
FROM WarehouseMgmt.FactPaymentTrans AS FPT
INNER JOIN WarehouseMgmt.DimPlayer AS DPL
ON FPT.PlayerId = DPL.Id
INNER JOIN WarehouseMgmt.DimProduct AS DP
ON DP.Id = FPT.ProductId
INNER JOIN WarehouseMgmt.DimProductType AS DPT
ON DPT.Id = DP.ProductTypeId
INNER JOIN WarehouseMgmt.DimTransactionStepType AS DTST
ON FPT.[TransactionStepTypeId] = DTST.[Id]
WHERE (FPT.FactType = '' SOURCE '')
AND EXISTS (SELECT 1
FROM [WarehouseMgmt].[FactPaymentTrans]
WHERE FactType = '' SOURCE ''
AND FPT.TransactionId = TransactionId
AND FPT.TransactionStepId = TransactionStepId)
AND EXISTS (SELECT 1
FROM [WarehouseMgmt].[DimLoyaltyProgramLevel] DLPL
WHERE DLPL.[Id] = DPL.VipLevelId)
AND EXISTS (SELECT 1
FROM WarehouseMgmt.DimTimeZone DTZ
WHERE FPT.[TimeId] = DTZ.TimeUTCId
AND DTZ.TimeId BETWEEN #DimStartDate AND #DimEndDate)

Related

Which query is faster to execute

Following are two options to query CTE and NFSE data. Pls advise !
A similar approach may have to be used in many places.
I would like you to verify them and suggest, if there are better options.
Option 1: Query CTE with all necessary joins UNION with query on NFSe with all necessary joins
SELECT DISTINCT 'CTE' as docTypeID
, cte.isqn_mstr_cd as ctePk
, cte.ct_e_cd
, cter.stat_desc
, awb.CREATE_DT
, awb.AWB_NBR
, awb.SHPR_NM
, awbs.DEST_LOC_CD
, nfe.NT_FSCL_CD
, nfe.FSCL_DOC_NBR
FROM cte_identity_master cte
INNER JOIN cte_response_detail cter ON (cte.isqn_mstr_cd = cter.isqn_mstr_cd)
LEFT JOIN match_ref_awb mawb ON (cte.isqn_ref_cd = mawb.isqn_mstr_cd)
LEFT JOIN awb_cust_master awb ON (mawb.awb_nbr = awb.awb_nbr)
LEFT JOIN awb_shipment_detail awbs ON (awb.awb_nbr = awbs.awb_nbr)
LEFT JOIN match_ref_nfe mnfe ON (cte.isqn_ref_cd = mnfe.isqn_mstr_cd)
LEFT JOIN nfe_identity_master nfe ON (mnfe.nt_fscl_cd = nfe.nt_fscl_cd)
UNION
SELECT DISTINCT 'NFSE' as docTypeID
, nfse.isqn_mstr_cd as nfsePk
, nfse.rp_s_id
, nfser.stat_desc
, awb.CREATE_DT
, awb.AWB_NBR
, awb.SHPR_NM
, awbs.DEST_LOC_CD
, nfe.NT_FSCL_CD
, nfe.FSCL_DOC_NBR
FROM nfse_request_detail nfse
INNER JOIN nfse_response_detail nfser ON (nfse.isqn_mstr_cd = nfser.isqn_mstr_cd)
LEFT JOIN match_ref_awb mawb ON (nfse.isqn_ref_cd = mawb.isqn_mstr_cd)
LEFT JOIN awb_cust_master awb ON (mawb.awb_nbr = awb.awb_nbr)
LEFT JOIN awb_shipment_detail awbs ON (awb.awb_nbr = awbs.awb_nbr)
LEFT JOIN match_ref_nfe mnfe ON (nfse.isqn_ref_cd = mnfe.isqn_mstr_cd)
LEFT JOIN nfe_identity_master nfe ON (mnfe.nt_fscl_cd = nfe.nt_fscl_cd)
;
Option 2: Use Union of CTe and NFSe first and then apply joins with other tables
SELECT ctnf.*
, awb.CREATE_DT
, awb.AWB_NBR
, awb.SHPR_NM
, awbs.DEST_LOC_CD
, nfe.NT_FSCL_CD
, nfe.FSCL_DOC_NBR
FROM (
SELECT DISTINCT 'CTE' as docTypeID
, cte.isqn_mstr_cd as docPk
, cte.ct_e_cd as docNbr
, cter.stat_desc as docStat
, cte.isqn_ref_cd as matchRef
FROM cte_identity_master cte
INNER JOIN cte_response_detail cter ON (cte.isqn_mstr_cd = cter.isqn_mstr_cd)
UNION
SELECT DISTINCT 'NFSE' as docTypeID
, nfse.isqn_mstr_cd as nfsePk
, nfse.rp_s_id
, nfser.stat_desc
, nfse.isqn_ref_cd
FROM nfse_request_detail nfse
INNER JOIN nfse_response_detail nfser ON (nfse.isqn_mstr_cd = nfser.isqn_mstr_cd)
) ctnf
LEFT JOIN match_ref_awb mawb ON (ctnf.matchRef = mawb.isqn_mstr_cd)
LEFT JOIN awb_cust_master awb ON (mawb.awb_nbr = awb.awb_nbr)
LEFT JOIN awb_shipment_detail awbs ON (awb.awb_nbr = awbs.awb_nbr)
LEFT JOIN match_ref_nfe mnfe ON (ctnf.matchRef = mnfe.isqn_mstr_cd)
LEFT JOIN nfe_identity_master nfe ON (mnfe.nt_fscl_cd = nfe.nt_fscl_cd)
;
Both approaches of the above will have following Where Clause:
WHERE lower(cte.sttn_cd) = lower(:stationId)
and (:documentType is null or lower(:documentType) = 'cte')
and (:shipperName is null or lower(awb.shipperNm) like lower(concat(concat('%',:shipperName),'%')))
and (:awbCreated is null or to_char(awb.createDt, 'MM-DD-YYYY') = :awbCreated)
and (:awbNumber is null or m2.awbNbr like concat(concat('%',:awbNumber),'%'))
and (:serviceType = 0 or awbs.baseServiceCd = :serviceType)
and (:commitmentDate is null or awbs.commitmentDate = :commitmentDate)
and (:ursa is null or lower(awbs.ursaCd) like lower(concat(concat('%',:ursa),'%')))
and (:destLocationId is null or lower(awbs.destLocCd) like lower(concat(concat('%',:destLocationId),'%')))
and (:nfeNumber is null or nfe.fiscalDocumentNumber like concat(concat('%',:nfeNumber),'%'))
PLEASE SUGGEST - Output of these two approaches to get data which one will be better to fix at Java End to retrieve the data.
Any help will be greatly appreciated. Also suggest if there is any other better query apart from these two!

Data show by list when group by is not include data

I have a sql command like this
select mainPOID.EstAPDate,mainPOID.POID,TTm.ID,TTMAmount=TTMD.InvoiceAmount
From TTBeforeMms TTM
inner join TTBeforeMms_Detail TTMD on TTM.ID = TTMD.ID
inner join (
select distinct main.EstAPDate,main_D.POID
From AP main
left join AP_Detail main_D on main.ID = main_D.ID
where main.Type ='PA' and main.EstAPDate between '2018/6/01' AND '2018/6/15' and left(main_D.InvoiceNo,4) != '1111' ) mainPOID on TTMD.poid =mainPOID.POID and TTM.EstAPdate<=mainPOID.EstAPdate and mainPOID.POID='CM3PU18030009'
order by mainPOID.EstAPDate,mainPOID.POID
sql result will like this
My question is
How can Data show by list when group by is not include data?
For example
ID will show by list When I group by EstAPDate、POID and sum(TTMAmount)
You can store in one temp table or use CTE
CREATE TABLE [dbo].#Columnss(
espapdate date ,
poid varchar(max),
id varchar(max),amount decimal(22,6))
GO
insert into #Columnss values('2018-06-15','cm3','pt20',19988.8900)
insert into #Columnss values('2018-06-15','cm3','pt21',265.8900)
SELECT
REPLACE(ESPAPDATE,'-','/') ESPAPDATE, POID,
STUFF(
(SELECT ' , ' + OD.ID
FROM #COLUMNSS OD
WHERE OD.ESPAPDATE = O.ESPAPDATE
AND OD.POID = O.POID
FOR XML PATH('')), 1, 2, ''
) PRODUCTNAMES,SUM(AMOUNT)AMOUNT
FROM #COLUMNSS O
GROUP BY ESPAPDATE, POID
output
espapdate poid ProductNames amount
2018/06/15 cm3 pt20 , pt21 20254.780000
there are lots of CSV example using FOR XML PATH. For your case here, i am wrapping your existing query in a CTE and then from there generate the CSV string for ID
; with
cte as
(
select mainPOID.EstAPDate, mainPOID.POID, TTm.ID, TTMAmount=TTMD.InvoiceAmount
From TTBeforeMms TTM
inner join TTBeforeMms_Detail TTMD on TTM.ID = TTMD.ID
inner join (
select distinct main.EstAPDate,main_D.POID
From AP main
left join AP_Detail main_D on main.ID = main_D.ID
where main.Type ='PA'
and main.EstAPDate between '2018/6/01' AND '2018/6/15'
and left(main_D.InvoiceNo,4) != '1111'
) mainPOID on TTMD.poid = mainPOID.POID
and TTM.EstAPdate <= mainPOID.EstAPdate
and mainPOID.POID = 'CM3PU18030009'
)
SELECT EstAPDate, POID,
ID = STUFF(c.ID, 1, 1, ''),
TTMAmount = SUM(TTMAmount)
FROM cte
CROSS APPLY
(
SELECT ',' + ID
FROM cte x
WHERE x.EstAPDate = cte.EstAPDate
AND x.POID = cte.POID
FOR XML PATH ('')
) c (ID)
GROUP BY EstAPDate, POID
ORDER BY EstAPDate, POID

how to insert random records without violating primary constraint

Please pardon my title if misleading, but I have a table tb_party with PRIMARY KEY on party_key
I have following query -
insert TMS..tb_party
(
[party_key]
,[party_first_name]
,[tax_id]
,[party_type_cd]
,[citizenship_country_cd]
,[domicile_country_cd]
,[party_num]
,[batch_dt]
)
select distinct
[party_key]
,[party_first_name]
,[tax_id]
,[party_type_cd]
,[cit]
,[dom]
,[party_num]
,[batch_dt]
from
(select distinct
sca.[party_key]
,sca.[party_first_name]
,sca.[tax_id]
,pt.party_type_cd
,tc_cit.COUNTRY_ID as cit
,tc_dom.COUNTRY_ID as dom
,sca.[party_num]
,getdate() as batch_dt
,dense_rank() over(partition by sca.[party_key] order by sca.party_key) as rnk
from Iteration_3.dbo.staging_cust_acct sca (nolock)
join Iteration_3..STG_PARTY_UPLOAD (nolock) stg_party
on sca.party_key = stg_party.PARTY_KEY
left join Iteration_3..STG_COUNTRY_ISO tc_dom (nolock)
on sca.[domicile_country] = tc_dom.COUNTRY_NAME
left join Iteration_3..STG_COUNTRY_ISO tc_cit (nolock)
on sca.[citizenship_country] = tc_cit.COUNTRY_NAME
left join TMS..tb_party_type pt (nolock)
on sca.[party_type] = pt.party_type_desc
WHERE
SCA.party_type IS NOT NULL
) x
where rnk = 1
The insert fails because it is trying to insert duplicate party key and since the distinct is on ALL columns, it is picking up duplicate party_keys.
What I want - I want to pick up all distinct party_keys and insert 1 row in tb_party. the other rows can be ignored. Is this possible?
Here is where row_number came in handy.
select distinct
[party_key]
,[party_first_name]
,[tax_id]
,[party_type_cd]
,[cit]
,[dom]
,[party_num]
,[batch_dt]
from
(
select distinct
sca.[party_key]
,sca.[party_first_name]
,sca.[tax_id]
,pt.party_type_cd
,tc_cit.COUNTRY_ID as cit
,tc_dom.COUNTRY_ID as dom
,sca.[party_num]
,getdate() as batch_dt
--,dense_rank() over(partition by sca.[party_key] order by sca.party_key,sca.[tax_id],sca.[party_num],sca.[party_first_name]*/) as rnk
,row_number() over (partition by sca.party_key order by sca.party_key) rn
from Iteration_3.dbo.staging_cust_acct sca (nolock)
join Iteration_3..STG_PARTY_UPLOAD (nolock) stg_party
on sca.party_key = stg_party.PARTY_KEY
left join Iteration_3..STG_COUNTRY_ISO tc_dom (nolock)
on sca.[domicile_country] = tc_dom.COUNTRY_NAME
left join Iteration_3..STG_COUNTRY_ISO tc_cit (nolock)
on sca.[citizenship_country] = tc_cit.COUNTRY_NAME
left join TMS..tb_party_type pt (nolock)
on sca.[party_type] = pt.party_type_desc
WHERE --SCA.update_source = 'ECM' AND SCA.update_dt = #ECM_MAX_DATE
--and
SCA.party_type IS NOT NULL
) x
where rn = 1
Try merging into your target table on the unique columns. When not matched, insert. When matched update like this:
...
set target.[column] = coalesce(source.[column], target.[column])
...
This way you update all your target columns when you have source values, otherwise they remain unchanged.)

How do I remove duplicate results by looking for max date?

I know I have duplicate results from this query because the tables ReleaseHistory and IterationHistory have multiple records per ReleaseID and IterationID. I would like to only select the records with max date from dbo.ReleaseHistory and dbo.IterationHistory. How would I do that in this query? SQL SERVER 2008
SELECT dbo.Assignable.AssignableID AS ID,
dbo.EntityType.Abbreviation AS Entity,
dbo.General.Name, dbo.Assignable.Effort,
dbo.Assignable.EffortCompleted,
dbo.Assignable.EffortToDo,
dbo.EntityState.Name AS State,
dbo.ReleaseHistory.Name AS Release,
dbo.IterationHistory.Name AS Iteration,
dbo.General.CustomField3 AS [Scrum Team]
FROM dbo.Assignable INNER JOIN
dbo.General ON dbo.Assignable.AssignableID =
dbo.General.GeneralID INNER JOIN
dbo.EntityType ON dbo.General.EntityTypeID =
dbo.EntityType.EntityTypeID INNER JOIN
dbo.EntityState ON dbo.Assignable.EntityStateID =
dbo.EntityState.EntityStateID AND
dbo.EntityType.EntityTypeID =
dbo.EntityState.EntityTypeID INNER JOIN
dbo.ReleaseHistory ON dbo.Assignable.ReleaseID =
dbo.ReleaseHistory.ReleaseID INNER JOIN
dbo.IterationHistory ON
dbo.Assignable.IterationID =
dbo.IterationHistory.IterationID LEFT OUTER JOIN
dbo.CustomField ON dbo.General.CustomField3 =
dbo.CustomField.CustomFieldID
WHERE (dbo.Assignable.ProjectID = 4054)
GROUP BY dbo.Assignable.AssignableID,
dbo.EntityType.Abbreviation,
dbo.General.Name,
dbo.Assignable.Effort,
dbo.Assignable.EffortCompleted,
dbo.Assignable.EffortToDo,
dbo.EntityState.Name,
dbo.ReleaseHistory.Name,
dbo.IterationHistory.Name,
dbo.General.CustomField3
Brian,
I am amusing you are doing this in MS SQL and there will always be at least one record in ReleaseHistory and IterationHistory tables. If assumptions are correct then you can simply use CROSS APPLY to get top 1 record from both tables.
SELECT
dbo.Assignable.AssignableID AS ID ,
dbo.EntityType.Abbreviation AS Entity ,
dbo.General.Name ,
dbo.Assignable.Effort ,
dbo.Assignable.EffortCompleted ,
dbo.Assignable.EffortToDo ,
dbo.EntityState.Name AS State ,
Release ,
Iteration ,
dbo.General.CustomField3 AS [Scrum Team]
FROM
dbo.Assignable
INNER JOIN dbo.General ON dbo.Assignable.AssignableID = dbo.General.GeneralID
INNER JOIN dbo.EntityType ON dbo.General.EntityTypeID = dbo.EntityType.EntityTypeID
INNER JOIN dbo.EntityState ON dbo.Assignable.EntityStateID = dbo.EntityState.EntityStateID
AND dbo.EntityType.EntityTypeID = dbo.EntityState.EntityTypeID
CROSS APPLY( SELECT TOP 1 name Release FROM ReleaseHistory WHERE ReleaseID = Assignable.ReleaseID ORDER BY MaxDateColumn) a
CROSS APPLY( SELECT TOP 1 name Iteration FROM IterationHistory WHERE IterationID = Assignable.IterationID ORDER BY MaxDateColumn) b
LEFT OUTER JOIN dbo.CustomField ON dbo.General.CustomField3 = dbo.CustomField.CustomFieldID
WHERE
( dbo.Assignable.ProjectID = 4054 )
GROUP BY
dbo.Assignable.AssignableID ,
dbo.EntityType.Abbreviation ,
dbo.General.Name ,
dbo.Assignable.Effort ,
dbo.Assignable.EffortCompleted ,
dbo.Assignable.EffortToDo ,
dbo.EntityState.Name ,
dbo.ReleaseHistory.Name ,
dbo.IterationHistory.Name ,
dbo.General.CustomField3

DB2 issue Getting -101 for 300 records but same query working for 1000 and 10000 records

I am facing strange issue in Db2 .
I am getting DB2 SQL error: SQLCODE: -101, SQLSTATE: 54001, SQLERRMC: 1
The same query works for 1000 even 10000 records but does not works for particualar sets of records(150-300).I am able to reproduce now with those records.
Query uses Select and With cause only.
So I donot think increasing STMT Heap Memory will make any sense as the query works for larger records.
Query is Big and Consists of only Select Join and few Case statement.
Query is also using With statement.
#Note Every Record is independent in its own have relation with another record.
Here is Query
WITH
itemStyle_ AS (
SELECT * FROM item.itemStyle WHERE itemSTY_SEQ
---- This Parameter gets changes to 1000 only
IN( 'awudhjqwdvwd12' )
),CustomsInfo AS
(SELECT 3 AS ReitemrtGrp, 3 AS REitemRTGRP2, HSYS_NO, NHSYS_NO, itemCUSTOMSINFO.itemDEL_SEQ,-1 AS COL_NO,
itemStyle.MyApp_item_NO, itemStyle.SIZE_TNAME, itemStyle.CS_DATE, itemStyle.SIZ1TABID, itemDELIVERY.DS_NO,
itemStyle.ABM_Date, itemStyle.RS_Date,itemStyle.CustCo_NO, itemStyle.Cust_Code, itemStyle.UCustCo_No, itemStyle.Prodiv_desc, itemStyle.Busunidesc,itemStyle.COno, itemStyle.STY_NO
FROM item.itemCUSTOMSINFO itemCUSTOMSINFO INNER JOIN item.itemDELIVERY itemDELIVERY ON itemCUSTOMSINFO.itemDEL_SEQ = itemDELIVERY.itemDEL_SEQ, itemStyle_ itemStyle
WHERE (HSYS_NO IS NOT NULL OR NHSYS_NO IS NOT NULL)
AND itemCUSTOMSINFO.itemSTY_SEQ = itemStyle.itemSTY_SEQ
),
SizeInfo AS(
SELECT 1 AS ReitemrtGrp2,
itemStyle.SIZ1TABID, itemDELCOLOR.itemDEL_SEQ, itemStyle.MyApp_item_NO, itemStyle.CS_DATE, itemStyle.STY_NO,
itemStyle.ABM_Date, itemStyle.RS_Date,itemStyle.CustCo_NO, itemStyle.Cust_Code, itemStyle.UCustCo_No, itemStyle.Prodiv_desc, itemStyle.Busunidesc,itemStyle.COno,itemDELCOLOR.DS_NO,
itemColor.COL_NO,CUSIZ_CODE,
itemDELSIZE.QUANTITY,itemDELSIZE.OCP_FACTOR,itemDELSIZE.SKU_CODE,
itemSIZE.SIZE_CODE, itemSIZE.UNIT_PRC,itemSIZE.SURCHARGE,itemSIZE.SIZNUM itemSIZE_NUM, itemSIZE.UNIT_DISC, itemSIZE.PACK_UNIT
FROM
itemStyle_ AS itemStyle
INNER JOIN item.itemColor itemColor ON itemStyle.itemSTY_SEQ = itemColor.itemSTY_SEQ
INNER JOIN item.itemDELCOLOR itemDELCOLOR ON itemDELCOLOR.itemCOL_SEQ = itemColor.itemCOL_SEQ
INNER JOIN item.itemDELSIZE itemDELSIZE ON itemDELCOLOR.itemDELCOL_SEQ = itemDELSIZE.itemDELCOL_SEQ
INNER JOIN item.itemSIZE itemSIZE ON itemDELSIZE.itemSIZ_SEQ = itemSIZE.itemSIZ_SEQ
),OrderChar AS(
SELECT 3 AS ReitemrtGrp2, 2.6 AS ReitemrtGrp,
ordchar_desc, itemStyle.COno, CS_Date, SIZ1TABID, itemStyle.MyApp_item_NO, itemStyle.STY_NO,
itemStyle.ABM_Date, itemStyle.RS_Date,itemStyle.CustCo_NO, itemStyle.Cust_Code, itemStyle.UCustCo_No, itemStyle.Prodiv_desc, itemStyle.Busunidesc,itemDELIVERY.itemDEL_SEQ FROM
item.itemORDERCHAR itemOrderChar INNER JOIN itemStyle_ itemStyle ON itemOrderChar.itemSty_Seq = itemStyle.itemSty_Seq
INNER JOIN item.itemDELIVERY itemDELIVERY ON itemStyle.itemSTY_SEQ = itemDELIVERY.itemSTY_SEQ
),
Supp_Address AS (
SELECT ADDRESS.COMPANY1 AS SuppAdd_COMPANY1,
ADDRESS.COMPANY2 AS SuppAdd_COMPANY2,
ADDRESS.COMPANY3 AS SuppAdd_COMPANY3,
ADDRESS.STREET1 AS SuppAdd_STREET1,
ADDRESS.STREET2 AS SuppAdd_STREET2,
ADDRESS.STREET3 AS SuppAdd_STREET3,
ADDRESS.CITY AS SuppAdd_CITY,
ADDRESS.STATENAME AS SuppAdd_STATENAME,
ADDRESS.COUNTRY AS SuppAdd_COUNTRY,
ADDRESS.ZIP_CODE AS SuppAdd_ZIP_CODE,
SUPPLIER.SUP1COMID,
CONTACTINF.C_PHONENO, CONTACTINF.A_PHONENO, CONTACTINF.M_PHONE ,
ADDRESS.ZitemST_BOX , ADDRESS.itemST_BOX
FROM item.ADDRESS ADDRESS,
item.ORGANZN ORGANZN
LEFT OUTER JOIN item.SUPPLIER SUPPLIER
ON ORGANZN.ORG_ID=SUPPLIER.ORG_ID,
item.CONTACTINF CONTACTINF WHERE
( ADDRESS.ORG_ID=ORGANZN.ORG_ID ) AND
( CONTACTINF.ADDRESS_ID=ADDRESS.ADDRESS_ID ) AND
(
SUPPLIER.SUPP_FLAG = 'Y'
)) ,
Cust_Address AS (
SELECT VALUE(ADDRESS.COMPANY1,'') CustAdd_COMPANY1 ,
VALUE(ADDRESS.COMPANY2,'') AS CustAdd_COMPANY2,
VALUE(ADDRESS.COMPANY3,'') AS CustAdd_COMPANY3,
VALUE(ADDRESS.STREET1,'') AS CustAdd_STREET1,
VALUE(ADDRESS.STREET2,'') AS CustAdd_STREET2,
VALUE(ADDRESS.STREET3 ,'') AS CustAdd_STREET3,
VALUE(ADDRESS.CITY,'') AS CustAdd_CITY,
VALUE(ADDRESS.STATENAME,'') AS CustAdd_STATENAME,
VALUE(ADDRESS.COUNTRY ,'') AS CustAdd_COUNTRY,
VALUE(ADDRESS.ZIP_CODE ,'')AS CustAdd_ZIPCODE,
VALUE(CUSTOMER.CUST_ID ,'')AS CustAdd_CUST_ID,
VALUE(ADDRESS.ZitemST_BOX,'') AS CustAdd_ZitemST_BOX ,
VALUE(ADDRESS.itemST_BOX,'') AS CustAdd_itemST_BOX
FROM item.ADDRESS ADDRESS,
item.CUSTOMER CUSTOMER
LEFT OUTER JOIN item.ORGANZN ORGANZN
ON ORGANZN.ORG_ID=CUSTOMER.ORG_ID,
item.ORGDEFAULTS ORGDEFAULTSWHERE
( ADDRESS.ORG_ID=ORGANZN.ORG_ID )
AND (
ADDRESS.ADDRESS_ID=ORGDEFAULTS.DEFAULT_VALUE_ID ) AND
( CUSTOMER.CUST_FLAG='Y' )
AND ORGDEFAULTS.DEFAULT_ITEM = 'MAILING_ADDRESS'
SELECT VALUE(CustomsInfo.ReitemrtGrp, OrderChar.ReitemrtGrp, 3) AS ReitemrtGrp,
VALUE(SizeInfo.ReitemrtGrp2, OrderChar.ReitemrtGrp2, CustomsInfo.ReitemrtGrp2, 4) AS ReitemrtGrp2,
VALUE(itemStyle.MyApp_item_NO, CUSTOMSINFO.MyApp_item_NO, SizeInfo.MyApp_item_NO,OrderChar .MyApp_item_NO) AS MyApp_item_NO ,
itemStyle.COno ,
itemStyle.STAT_DESC ,
itemStyle.CUST_CODE ,
itemStyle.CUST_NAME ,
itemStyle.CUSTCO_NO ,
itemStyle.UCUST_CODE ,
itemStyle.UCUST_NAME ,
itemStyle.DSHIP_FLAG ,
itemStyle.STY_NO ,
itemStyle.STY_DESC ,
itemStyle.BRAND_DESC ,
itemStyle.PRODIV_DESC ,
itemStyle.CUSTY_DESC ,
itemStyle.SEA_DESC ,
itemStyle.LINNAMDESC ,
itemStyle.SUPP_CODE ,
itemStyle.SUPP_NAME,
itemStyle.CONCEPDESC ,
itemStyle.RS_DATE ,
itemStyle.ABM_DATE ,
VALUE(itemStyle.CS_DATE, CustomsInfo.CS_DATE, SizeInfo.CS_DATE, OrderChar.CS_Date) AS CS_DATE ,
itemStyle.REMARKS ,
itemStyle.LASTUPDATE ,
itemStyle.FACT_CODE ,
itemStyle.C_ORIGIN ,
itemStyle.itemDDESC,
VALUE(itemDELIVERY.DS_NO, SizeInfo.DS_NO,CustomsInfo.DS_NO) AS DS_NO ,
itemDELIVERY.CUST_DS_NO ,
itemDELIVERY.ULTCUST_DS_NO,
itemDELIVERY.LCS_DATE ,
itemDELIVERY.DTERM_DESC ,
itemSHIPINST.SHIP_MARKS ,
itemDELIVERY.PACK_INST,
itemStyle.KEY_SIZE ,
itemSHIPINST.SMODE_DESC ,
itemSHIPINST.ORIGN_itemRT ,
itemSHIPINST.DEST_itemRT ,
itemSHIPINST.CARR_DESC ,
itemSHIPINST.SPL_INST,
itemStyle.PAYM_DESC ,
itemStyle.PAYT_DESC ,
itemStyle.UCUSTCO_NO ,
itemStyle.ORD_RDATE ,
itemStyle.ORG_NAME ,
itemStyle.CUSTY_NO ,
VALUE(itemDELADDRESS.COMPANY1,'') AS DELCOMPANY1 ,
VALUE(itemDELADDRESS.COMPANY2,'') AS DELCOMPANY2 ,
VALUE(itemDELADDRESS.COMPANY3,'') AS DELCOMPANY3 ,
VALUE(itemDELADDRESS.STREET1,'') AS DELSTREET1,
VALUE(itemDELADDRESS.STREET2,'') AS DELSTREET2,
VALUE(itemDELADDRESS.STREET3,'') AS DELSTREET3,
VALUE(itemDELADDRESS.CITY,'') AS DELCITY,
VALUE(itemDELADDRESS.STATENAME,'') AS DELSTATENAME,
VALUE(itemDELADDRESS.COUNTRY,'') AS DELCOUNTRY,
VALUE(itemDELADDRESS.ZIP_CODE,'') AS DELZIP ,
VALUE(itemDELIVERY.itemDEL_SEQ , CustomsInfo.itemDEL_SEQ, SizeInfo.itemDEL_SEQ, OrderChar.itemDEL_SEQ) AS itemDEL_SEQ ,
itemStyle.DEBTOR_CODE ,
VALUE(itemADDRESS.STATENAME,'') AS STATENAME,
VALUE(itemADDRESS.COMPANY1,'') AS COMPANY1,
VALUE(itemADDRESS.COMPANY2,'') AS COMPANY2,
VALUE(itemADDRESS.COMPANY3,'') AS COMPANY3,
VALUE(itemADDRESS.STREET1,'') AS STREET1,
VALUE(itemADDRESS.STREET2,'') AS STREET2,
VALUE(itemADDRESS.STREET3,'') AS STREET3,
VALUE(itemADDRESS.CITY,'') AS CITY,
VALUE(itemADDRESS.COUNTRY,'') AS itemADDRCOUNTRY,
VALUE(itemADDRESS.ZIP_CODE,'') AS ZIP_CODE,
itemStyle.CURRENCY ,
itemStyle.CREATEDATE ,
itemStyle.SIZE_TNAME ,
itemStyle.BUSUNIDESC ,
VALUE (itemStyle.SIZ1TABID, CustomsInfo.SIZ1TABID, SizeInfo.SIZ1TABID, OrderChar.SIZ1TABID) AS SIZ1TABID,
itemStyle.DISC_VALUE ,
HSYS_NO, NHSYS_NO ,
VALUE(itemColor.COL_NO, SizeInfo.COL_NO, CustomsInfo.COL_NO) AS COL_NO,
itemColor.PROCH_DESC ,
itemColor.PROREQDESC ,
itemColor.COL_DESC ,
VALUE (itemColor.HAND_CHARGE,0) HAND_CHARGE ,
VALUE(itemColor.OTHER_CHARGES, 0) OTHER_CHARGES ,
itemColor.PERF_CODE,
SizeInfo.QUANTITY ,
SizeInfo.OCP_FACTOR ,
SizeInfo.SKU_CODE ,
SizeInfo.SIZE_CODE ,
SizeInfo.UNIT_PRC ,
SizeInfo.SURCHARGE ,
SizeInfo.itemSIZE_NUM ,
SizeInfo.UNIT_DISC,
SizeInfo.CUSIZ_CODE,
SizeInfo.PACK_UNIT,
YOUTSOLE.ITMDES AS OUTSOLEDESC, YOUTSOLE.OUTSOLE AS OUTSOLECODE,
/* (SELECT ITMDES FROM item.YCATEGO YCATEGO WHERE YCATEGO.categoid = art1sty.categoid) AS CategoDesc,
(SELECT ITMDES FROM item.YACTGRP YACTGRP WHERE YACTGRP.actgrpid = art1sty.actgrpid) AS ACTGRPDesc,*/
LEADTM_DESC AS LeadTime,
/* (SELECT SUM(VALUE(hand_charge,0) + VALUE(other_charges,0)) AS TotalCharge FROM item.itemColor itemColor WHERE itemColor.itemSTY_SEQ = itemStyle.itemSTY_SEQ) AS TotalCharge,*/
itemDelivery.AMD_Date,
OrderChar.ordchar_desc,
YPROCON.ITMDES AS PromotionContract,
(SELECT SUM(QUANTITY) FROM SizeInfo WHERE SizeInfo.MyApp_item_NO = itemStyle.MyApp_item_NO) AS OrderTotalQty,
Supp_Address.*,
Cust_Address.*,
CASE WHEN 'MyAppitemNo' = 'csDate' THEN VALUE(itemStyle.CS_Date, CustomsInfo.CS_Date, SizeInfo.CS_Date, OrderChar.CS_Date)
WHEN 'MyAppitemNo' = 'rsDate' THEN VALUE(itemStyle.RS_Date, CustomsInfo.RS_Date,SizeInfo.RS_Date, OrderChar.RS_Date)
WHEN 'MyAppitemNo' = 'lcsDate' THEN VALUE(itemStyle.CS_Date, CustomsInfo.CS_Date, SizeInfo.CS_Date, OrderChar.CS_Date)
ELSE '2010-01-01'
END AS SortDate,
CASE WHEN 'MyAppitemNo' = 'MyAppitemNo' THEN VALUE(itemStyle.MyApp_item_NO, CustomsInfo.MyApp_item_NO, SizeInfo.MyApp_item_NO, OrderChar.MyApp_item_NO)
WHEN 'MyAppitemNo' = 'MyAppCoNo' THEN VALUE(itemStyle.COno, CustomsInfo.COno, SizeInfo.COno, OrderChar.COno)
WHEN 'MyAppitemNo' = 'custCoNo' THEN VALUE(itemStyle.CustCO_NO, CustomsInfo.CustCO_NO, SizeInfo.CustCO_NO, OrderChar.CustCO_NO)
WHEN 'MyAppitemNo' = 'custCode' THEN VALUE(itemStyle.Cust_Code, CustomsInfo.Cust_Code,SizeInfo.Cust_Code, OrderChar.Cust_Code)
WHEN 'MyAppitemNo' = 'ucustCoNo' THEN VALUE(itemStyle.UCustCO_NO, CustomsInfo.UCustCO_NO, SizeInfo.UCustCO_NO, OrderChar.UCustCO_NO)
WHEN 'MyAppitemNo' = 'proDivDesc' THEN VALUE(itemStyle.Prodiv_Desc, CustomsInfo.Prodiv_Desc, SizeInfo.Prodiv_Desc, OrderChar.Prodiv_Desc)
WHEN 'MyAppitemNo' = 'busUniDesc' THEN VALUE(itemStyle.BUSUNIDESC, CustomsInfo.BusuniDesc, SizeInfo.BusuniDesc, OrderChar.BusuniDesc)
WHEN 'MyAppitemNo' = 'styNo' THEN VALUE(itemStyle.STY_NO, CustomsInfo.STY_NO, SizeInfo.STY_NO, OrderChar.STY_NO)
ELSE 'xxx'
END AS SortField
FROM itemStyle_ AS itemStyle
INNER JOIN item.itemDELIVERY itemDELIVERY ON itemStyle.itemSTY_SEQ = itemDELIVERY.itemSTY_SEQ
INNER JOIN item.itemDELCOLOR itemDELCOLOR ON itemDELCOLOR.itemDEL_SEQ = itemDELIVERY.itemDEL_SEQ
INNER JOIN item.itemColor itemColor ON itemDELCOLOR.itemCOL_SEQ = itemColor.itemCOL_SEQ
INNER JOIN item.itemSHIPINST itemSHIPINST ON itemSHIPINST.itemDEL_SEQ = itemDELIVERY.itemDEL_SEQ
INNER JOIN item.itemDELADDRESS itemDELADDRESS ON itemDELADDRESS.itemDEL_SEQ = itemDELIVERY.itemDEL_SEQ
INNER JOIN item.itemADDRESS itemADDRESS ON itemADDRESS.itemSTY_SEQ = itemStyle.itemSTY_SEQ
INNER JOIN item.ART1STY ART1STY ON ART1STY.ART1STYID = itemStyle.ART1STYID
INNER JOIN Supp_Address ON itemStyle.SUPP_ID = Supp_Address.SUP1COMID
INNER JOIN Cust_Address ON itemStyle.CUST_ID = Cust_Address.CustAdd_Cust_ID
LEFT OUTER JOIN item.ART4COS ART4COS ON itemColor.ART4COSID = ART4COS.ART4COSID
LEFT OUTER JOIN item.ART2STS ART2STS ON ART2STS.ART2STSID = ART4COS.ART2STSID
LEFT JOIN item.YPROCON YPROCON ON ART2STS.PROCONID = YPROCON.PROCONID
LEFT OUTER JOIN item.YOUTSOLE YOUTSOLE ON YOUTSOLE.OUTSOLEID = ART1STY.OUTSOLEID
FULL OUTER JOIN CustomsInfo ON 1=2
FULL OUTER JOIN SizeInfo ON 1=2
FULL OUTER JOIN OrderChar ON 1=2
ORDER BY
SortDate DESC, SortField DESC,
MyApp_item_NO ,
VALUE(DS_NO,0),
VALUE(COL_NO,0),
itemDEL_SEQ,
ReitemrtGrp,
ReitemrtGrp2 DESC, SizeInfo.itemSIZE_NUM
FOR READ ONLY
Update:
My DB is having no triggers and procedures
The Strange part is for those set of (300) records query fails but works fine for 1000 rows/records/input parameters. Any Idea.
It is because of the query statement exceed the default size of 4KB. You can set the statement heap size as the following.
db2 update db cfg for YOUR_DATABASE_NAME using STMTHEAP 8192 AUTOMATIC