Link to home
Create AccountLog in
Avatar of Sandeep rathore
Sandeep rathoreFlag for India

asked on

MS Query Tuning

Hi,
Experts could you please let me know how  i can improve the query , if i have written it right .Please suggest if you think that this query can be written in better way.Thanks.

SELECT RWA.EFF_DT AS EFF_DT,
         RWA.LoadStatusId AS LoadStatusId,
         RWA.InsertType AS InsertType,
         SURR_ID,
         FACT_DT,
         RWA_DT,
         AR_ID,
         CONVERT(NVARCHAR(20), RWA_ID) AS CLC_CODE,
         FACILITET_LBNR,
         LINE_KEY,
         IDKT,
         VALUTA_KD_OPR,
         RWA.IP_ID as IP_ID,
         RISIKO_TYPE,
         AFTALE_ID,
         GUARANTOR_MK,
         CF_DOWNTURN_PM,
         COL_USED_DKK,
         EAD_AMOUNT_DKK,
         EXPECTED_LOSS_DKK,
         EXPOSURE_DKK,
         LGD_MIN_PM,
         MATURITY_YEARS,
         PD_MIN_PM,
         PROVISIONS_DKK,
         RISK_WEIGHT_PM,
         RWA_DKK,
         VOLATILITY_ADJ_DKK,
         AKTIVKLASSENR,
         CREDIT_RM_KD,
         DEFAULT_RWA_MK,
         LTRIM(RTRIM(EXPOSURE_TYPE_KD)) AS EXPOSURE_TYPE_KD,
         RWA_METHOD_KD,
         RWA_RELEVANT_MK,
         RWA_USED_MK,
         KONTOHAV_REGNR,
         LTRIM(RTRIM(SKEMAKEY)) AS SKEMAKEY,
         SELSKABSNR_RWA,
         NAVN,
         FORTROLIGHEDS_KD,
         IP_DATA_GR,
         CASE
                  WHEN RIGHT(OBLIGOR_GRADE,1) = '' THEN '0' + OBLIGOR_GRADE
                  ELSE OBLIGOR_GRADE
         END AS OBLIGOR_GRADE,
         RAROC_KLASS,
         KUNDEPLEJ_REGNR,
         KUNDE_GRP,
         COALESCE(IP.IP_ID,'') AS CST_ID,
         CST_CL_PORTAL_VAL_ID,
         CST_CL_MODL_RSLT_VAL_ID,
         CONFD_CODE,
         IP_ID_KC_NAVN,
         IP_ID_KC,
         CAST(CASE
                        WHEN TOTALFLOW_KD='' THEN 'XYZ'
                        WHEN TOTALFLOW_KD='IN' THEN 'IN'
                        ELSE TOTALFLOW_KD
                  END AS NVARCHAR(32)) AS TOTALFLOW_KD,
         EKSTERN_MK,
         CASE
                  WHEN ROLE='' THEN 'XYZ'
                  ELSE ROLE
         END AS ROLE,
         CASE
                  WHEN SECURITI_TYPE='' THEN 'XYZ'
                  ELSE SECURITI_TYPE
       END AS SECURITI_TYPE,
       PRODUKTKILDE,
       UDNYTTELSE_DKK,
       SIKKERHEDSTYPE_KD,
       EJENDOMS_TP,
       SIKKERHEDAFT_ID,
       EC_RELEVANT_MK,
       KTTP,
       HANDELANLAEG_MK,
       CF_DOWNTURN_PM_ROW,
       CAST(CASE
                        WHEN IP_ID_KC = '' THEN 'XYZ'
                        WHEN LEN(RTRIM(LTRIM((IP_ID_KC)))) > 3 THEN LEFT(RWA.IP_ID,LEN(RTRIM(LTRIM(IP_ID_KC)))-3)
                        ELSE IP_ID_KC
                  END AS NVARCHAR(32)) AS UNQ_ID_IN_SRC_STM_RWA
         FROM CPY.cpy.GDWH_RskWghtAstFact AS RWA
         LEFT OUTER JOIN SOR.sor.vwIP_S AS IP ON IP.SRC_STM_ID = 2 AND
                                                                        CASE
                                                                              WHEN LEN(RTRIM(LTRIM((RWA.IP_ID)))) > 3 THEN LEFT(RWA.IP_ID,LEN(RTRIM(LTRIM(RWA.IP_ID)))-3)
                                                                              ELSE NULL
                                                                        END = IP.UNQ_ID_IN_SRC_STM
         LEFT OUTER JOIN sor.sor.CST_CL AS CST_CL ON IP.IP_ID = CST_CL.CST_ID AND
                                                                           CST_CL.EFF_DT <= '2013-06-30 23:59:59.9999999' AND
                                                                           CST_CL.END_DT > '2013-06-30 23:59:59.9999999'
         WHERE RWA.LoadStatusId = 103559843
         ORDER BY UNQ_ID_IN_SRC_STM_RWA ASC
Avatar of ralmada
ralmada
Flag of Canada image

It's hard without knowing your data, but one thing is for sure the case -else on the left join will be quite a dog. Have you considered changing it to

 LEFT OUTER JOIN SOR.sor.vwIP_S AS IP ON IP.SRC_STM_ID = 2 AND
                  RWA.IP_ID like IP.UNQ_ID_IN_SRC_STM + %

and make sure you have indexes defined for those columns.
Avatar of didnthaveaname
didnthaveaname

If you would be kind enough to give us the execution plan from your query, that would be immensely helpful as well.
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Sandeep rathore

ASKER

Hi Experts thanks for your suggestions.i will close this questions now.
The solution is very much appropriate and fine.