Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Improves Query Execution time

$
0
0

Hi All,

I have this query which is running on a database whose few tables are partitioned and few are not  (there is a valid reason for few tables not to partition-- Please do not ask why)

This Query executes in 00:00:43 on a non-partitiioned database but it take approx 7 mins to run again the partitioned database

Forcing (FORCESEEK) executes this query in lest than 30 second. wnhhy

Can we improve the performance of this query without any modication in the Query.(Its a legacy code which is developed by a third party with no support)

SELECT
PP.PORTFOLIOID ,
PP.CompanyId ,
PP.SourceType ,
PP.ProductType ,
PP.ProductSubType ,
PP.CurrencyCode ,
CM.BrokerMnemonic as CptyCode ,
CP.CounterPartyName,
PP.SecRef ,
PP.ReferenceObligation ,
(CASE WHEN PP.EntityName like 'UBS%' THEN (PP.NotionalTotal*-1) ELSE PP.NotionalTotal END) AS NotionalTotal,
(CASE WHEN PP.EntityName like 'UBS%' THEN (PP.SecondNotionalTotal*-1) ELSE PP.SecondNotionalTotal END) AS SecondNotionalTotal,
PP.Account ,
PP.BrokerTradeNumber ,
PP.Status ,
PP.EntityName ,
PP.CptyEntityName ,
PP.UnderlyingSecurityRefId ,
PP.LastCommentDate ,
PP.Rate ,
PP.ClientId ,
PP.ProductCategory ,
PP.IsValuationable ,
PP.IsValuationReq ,
PP.IsPositionInErr ,
PP.LastValuationDate ,
PD.LastDisputedDate ,
PP.TradeDate ,
PP.EffectiveDate ,
PP.SettlementDate ,
PP.ExpirationDate ,
PP.InitialPmtAmt ,
PP.InitialPayment ,
PP.StrikePrice ,
(case when (UPPER(PP.DirectionFlag) = 'SELL') then 'BUY' when (UPPER(PP.DirectionFlag) = 'BUY') then 'SELL' when (UPPER(PP.DirectionFlag) = 'B') then 'S' when (UPPER(PP.DirectionFlag) = 'S') then 'B' when (UPPER(PP.DirectionFlag) = 'LONG') then 'SHORT' when (UPPER(PP.DirectionFlag) = 'SHORT') then 'LONG' end) AS DirectionFlag ,
PP.IssuerName ,
PP.OptionStyle ,
PP.Spread ,
PP.PutCall ,
(case when (UPPER(PP.OptionBuySellIndicator) = 'SELL') then 'BUY' when (UPPER(PP.OptionBuySellIndicator) = 'BUY') then 'SELL' when (UPPER(PP.OptionBuySellIndicator) = 'B') then 'S' when (UPPER(PP.OptionBuySellIndicator) = 'S') then 'B' when (UPPER(PP.OptionBuySellIndicator) = 'LONG') then 'SHORT' when (UPPER(PP.OptionBuySellIndicator) = 'SHORT') then 'LONG' end ) AS OptionBuySellIndicator ,
PP.NoOfOptions ,
PP.OptionExpirationDate ,
PP.ModifyDate ,
PP.Tier ,
PP.PayerReceiver,
PP.IndependentAmountInCurrency ,
PP.PVTradeId ,
PP.LastValuationSubmissionTime,
PD.Type as DisputeType,
PD.DisputedTo,
PD.Stage as DisputeStage,
PD.ActionDate as DisputeActionDate,
PP.AlternateSwapId,
PP.GroupID,
PD.ID as DisputeId,
PP.IndependentAmount ,
PP.IndependentAmountCCY ,
PP.IndependentAmountType,
PP.IndependentAmountPercentage
,PP.Cusip,
PP.ISIN,
PP.PaymentFrequency,
PP.TicketNo,
PP.Leg1Index,
PP.Leg1ResetFrequency,
PP.CapFloorRate,
PP.EquityUnderlyingIdentifier,
PP.EquityIdentifierType,
PP.SecondLegRate,
PP.LegType,
PRI.Leg2Type,
PRI.Leg2Currency,
PRI.Leg2Index,
PRI.Leg2ResetFrequency,
PP.Leg1Payer,
PRI.Leg2Payer,
PRI.Leg2Spread,
PP.AttachmentPoint,
PP.ExhaustionPoint
,PP.PortfolioHistId,
IA2.AccountId as ClientAccount,
 'Y' subscribeFlag,
PP.CurrencyCode as LocalCcy, 1 as LocalCcyCount,  PP.SEDOL, PP.ClearingMtmLcl, PP.ClearingMtmLclCcy, PP.ClearingMtmUsd, PP.ExecutingBroker, PP.ClearingHouse, PP.ClearingHouseTradeId, PP.ClearingBrokerTradeId, PP.AffirmPlatformId, PP.USIIssuer, PP.USIValue, PP.UPI, PP.LEI, PP.InitialPrice, PEI.EquityLevel, PFI.FwdFXRate, VR.Delta, PP.DtccTradeID, PP.CurrentNotional, PP.CloseField, PP.FXRate, VR.UnitPrice, VR.DiscountRate, PP.ProductSubType, PP.FundLegalName,PP.RPFinancialEntity,PP.NonRPFinancialEntity,PP.RPUSPerson,PP.NonRPUSPerson,PP.SecondAssetClass,PP.MixedSwap,PP.DualSDR,PP.Collateralized,PP.ExecutionVenue, PRI.Leg1DayCountConvention,PRI.Leg2DayCountConvention,PFI.SettlementType,POI.TotalQuantity,POI.Quantity,POI.QuantityUnit,POI.QuantityFrequency, POI.SettlementMethod,POI.PriceUnit,POI.PriceCcy,POI.BuyerPayIndex,POI.BuyerPayAveragingMethod,POI.SellerPayIndex,POI.SellerPayAveragingMethod, POI.Grade,POI.HoursFromThrough,POI.HoursFromThroughTimeZone,POI.LoadType,PP.ContractType,PRI.Leg1DayCountFraction,PRI.Leg2DayCountFraction, POTI.UnitType,PP.ReportingCptySD_MSP, PP.NonReportingCptySD_MSP, PP.CcyCodeMtmLCL, PS.Frequency2, PP.FeeCcy, PP.FeePmtDate, PP.ReportingPartyLEI,PP.NonReportingPartyLEI,PP.DaysOfWeek,PP.PrimaryAssetClass,PP.PremiumCurrency,PP.PremiumPaymentDate,PP.PremiumAmt,PP.DeliveryBand, VR.Bid, VR.Ask, VR.Mid ,VR.ValuationDate ,
VR.isValid ,
VR.isSubmitted ,
max(VR.ValuationReceivedDate) ValuationReceivedDate,
VR.ValuationCcy,
(VR.MtmUSD*-1) as MtmUSD ,
(VR.MtmLCL*-1) as MtmLCL ,
(VR.AccuredUSD*-1) as AccuredUSD ,
(VR.AccuredLCL*-1) as AccuredLCL ,
(VR.MtmUSDPrior*-1) as MtmUSDPrior ,
(VR.MtmLCLPrior*-1) as MtmLCLPrior ,
(VR.AccuredUSDPrior*-1) as AccuredUSDPrior ,
(VR.AccuredLCLPrior*-1) as AccuredLCLPrior ,
VR.IsValUpdated ,
VR.IsPosUpdated ,
VR.LastUpdDate ,
VR.Recovery ,
VR.Spot ,
SUM(VR.PresentValue) as PresentValue,
SUM(VR.PVLocal) as PVLocal,
SUM(VR.Accrued) as Accrued,
SUM(VR.AccruedValCcy) as AccruedValCcy,
SUM(VR.PresentValuePrior) as PresentValuePrior,
SUM(VR.PVLocalPrior) as PVLocalPrior,
SUM(VR.AccruedValCcyPrior) as AccruedValCcyPrior,
SUM(VR.AccruedPrior) as AccruedPrior,
SUM(VR.CleanPVLocal) as CleanPVLocal,
SUM(VR.CleanPV) as CleanPV,
SUM(VR.CleanPrice) as CleanPrice,
SUM(VR.DirtyPrice) as DirtyPrice,
SUM(VR.PV01) as PV01,
SUM(VR.PV01Local) as PV01Local,
SUM(VR.ParSpread) as ParSpread,
SUM(VR.PriceAccrued) as PriceAccrued,
SUM(VR.Credit01) as Credit01,
SUM(VR.CleanPV) as CleanPV,
MAX(VR.RealizedVol) RealizedVol,
MAX(VR.ImpliedVol) ImpliedVol,
isNull(VR.ValueDate,VR.ValuationDate) ValueDate,
SUM(VRC.Cash) as Cash ,
 SUM(VRC.CPRAssumption) as CPRAssumption ,
 SUM(VRC.Portfolio01) as Portfolio01 ,
 SUM(VRC.PortfolioSpread) as PortfolioSpread ,
 SUM(VRC.IndexSpread) as IndexSpread ,
 SUM(VRC.TheoreticalSpread) as TheoreticalSpread ,
 SUM(VRC.ModelDelta) as ModelDelta ,
 SUM(VRC.RealisedDelta) as RealisedDelta ,
 SUM(VRC.Correlation01) as Correlation01 ,
 SUM(VRC.WeightedPortfolioSpread) as WeightedPortfolioSpread ,
 SUM(VRC.DirtyPriceBid) as DirtyPriceBid ,
 SUM(VRC.DirtyPriceAsk) as DirtyPriceAsk ,
 SUM(VRC.CleanPriceBid) as CleanPriceBid ,
 SUM(VRC.CleanPriceAsk) as CleanPriceAsk ,
 SUM(VRC.RealisedVol) as RealisedVol ,
 SUM(VRC.FairVol) as FairVol ,
 SUM(VRC.BEVol) as BEVol ,
 SUM(VRC.DataRating) as DataRating ,
 SUM(VRC.CashPosition) as CashPosition 
 FROM PortfolioPositionHistory PP 
 inner join ValuationResult VR  on PP.PORTFOLIOID = VR.PortfolioId
 left outer join PortfolioDispute PD on PP.PORTFOLIOID = PD.PortfolioId
 AND PD.ReconType = 'DAILY'
 AND isNull(PD.Stage,'Resolved') != 'Resolved'
 left outer join PortfolioRatesInfoHistory PRI  on PP.PortfolioHistId = PRI.PortfolioHistId
 left outer join PortfolioEquityInfoHistory PEI  on PP.PortfolioHistId = PEI.PortfolioHistId
 left outer join PortfolioFxInfoHistory PFI  on PP.PortfolioHistId = PFI.PortfolioHistId
 left outer join PortfolioOptionInfoHistory  POI on PP.PortfolioHistId = POI.PortfolioHistId
 left outer join PortfolioOtherInfoHistory POTI on PP.PortfolioHistId = POTI.PortfolioHistId
 left outer join PortfolioSchedulesHistory PS on PP.PortfolioHistId = PS.PortfolioHistId
 left outer join ValuationResultCash VRC  on VR.ValuationId = VRC.ValuationId
 inner join BuySideCounterPartyMap CM on PP.CounterpartyMapId = CM.BSCPMID  
 inner join CounterParties CP on CP.COUNTERPARTYID = CM.CounterPartyId  
 left outer join ISDAAccounts IA on PP.ClientId = IA.SourceCompanyId and PP.CompanyId = IA.CompanyId and substring(PP.Account, 1, 150) = IA.AccountId  left outer join ISDAAccountsMap map on IA.ISDAAccountId = map.ISDAAccountId1
 left outer join ISDAAccounts IA2 on  IA2.ISDAAccountId = map.ISDAAccountId2
 left outer join PVAccountConfig PA on PA.AccountId = PP.PVAccount  WHERE 1 = 1
AND PP.CompanyId = 13575
AND PP.Status = 'O'
 AND ((PP.CompanyId = 13575))
  AND PP.HistoryEffDate <= '2013-08-11'
AND (PP.HistoryExpDate is NULL OR PP.HistoryExpDate >= '2013-08-11' )
AND ( '2013-08-11' >= VR.ValuationDate AND  '2013-08-11' < VR.ValExpDate
 AND VR.ValuationDate between '2013-05-11' and '2013-08-11' )
GROUP BY  PP.PORTFOLIOID ,
PP.CompanyId ,
PP.SourceType ,
PP.ProductType ,
PP.ProductSubType ,
PP.CurrencyCode ,
CM.BrokerMnemonic ,
CP.CounterPartyName,
PP.SecRef ,
PP.ReferenceObligation ,
PP.NotionalTotal ,
PP.SecondNotionalTotal ,
PP.Account ,
PP.BrokerTradeNumber ,
PP.Status ,
PP.EntityName ,
PP.CptyEntityName ,
PP.UnderlyingSecurityRefId ,
PP.LastCommentDate ,
PP.Rate ,
PP.ClientId ,
PP.ProductCategory,
PP.IsValuationable ,
PP.IsValuationReq ,
PP.IsPositionInErr ,
PP.LastValuationDate ,
PD.LastDisputedDate ,
datediff(dd,isNull(PD.LastDisputedDate,getDate()),getDate()),
PP.TradeDate ,
PP.EffectiveDate ,
PP.SettlementDate ,
PP.ExpirationDate ,
PP.InitialPmtAmt ,
PP.InitialPayment ,
PP.StrikePrice ,
PP.DirectionFlag ,
PP.IssuerName ,
PP.OptionStyle ,
PP.Spread ,
PP.PutCall ,
PP.OptionBuySellIndicator ,
PP.NoOfOptions ,
PP.OptionExpirationDate ,
PP.ModifyDate ,
PP.Tier ,
PP.IndependentAmountInCurrency ,
PP.PVTradeId,
PP.LastValuationSubmissionTime,
PD.Type,
PD.DisputedTo,
PD.Stage,
PD.Action,
PD.ActionDate,
PP.AlternateSwapId,
PP.GroupID,
PP.PayerReceiver,
VR.ValuationDate ,
VR.isValid ,
VR.isSubmitted ,
VR.MtmUSD ,
VR.MtmLCL ,
VR.AccuredUSD ,
VR.AccuredLCL ,
VR.MtmUSDPrior ,
VR.MtmLCLPrior ,
VR.AccuredUSDPrior ,
VR.AccuredLCLPrior,
VR.IsValUpdated ,
VR.IsPosUpdated ,
VR.LastUpdDate,
VR.Spot,
VR.Recovery,
VR.ValueDate,
VR.ValuationCcy,
PP.AttachmentPoint,
PP.ExhaustionPoint,
PD.ID
,PP.IndependentAmount ,
PP.IndependentAmountCCY ,
PP.IndependentAmountType,
PP.IndependentAmountPercentage,
  PP.SEDOL, PP.ClearingMtmLcl, PP.ClearingMtmLclCcy, PP.ClearingMtmUsd, PP.ExecutingBroker, PP.ClearingHouse, PP.ClearingHouseTradeId, PP.ClearingBrokerTradeId, PP.AffirmPlatformId, PP.USIIssuer, PP.USIValue, PP.UPI, PP.LEI, PP.InitialPrice, PEI.EquityLevel, PFI.FwdFXRate, VR.Delta, PP.DtccTradeID, PP.CurrentNotional, PP.CloseField, PP.FXRate, VR.UnitPrice, VR.DiscountRate, PP.ProductSubType, PP.FundLegalName,PP.RPFinancialEntity,PP.NonRPFinancialEntity,PP.RPUSPerson,PP.NonRPUSPerson,PP.SecondAssetClass,PP.MixedSwap,PP.DualSDR,PP.Collateralized,PP.ExecutionVenue, PRI.Leg1DayCountConvention,PRI.Leg2DayCountConvention,PFI.SettlementType,POI.TotalQuantity,POI.Quantity,POI.QuantityUnit,POI.QuantityFrequency, POI.SettlementMethod,POI.PriceUnit,POI.PriceCcy,POI.BuyerPayIndex,POI.BuyerPayAveragingMethod,POI.SellerPayIndex,POI.SellerPayAveragingMethod, POI.Grade,POI.HoursFromThrough,POI.HoursFromThroughTimeZone,POI.LoadType,PP.ContractType,PRI.Leg1DayCountFraction,PRI.Leg2DayCountFraction, POTI.UnitType,PP.ReportingCptySD_MSP, PP.NonReportingCptySD_MSP, PP.CcyCodeMtmLCL, PS.Frequency2, PP.FeeCcy, PP.FeePmtDate, PP.ReportingPartyLEI,PP.NonReportingPartyLEI,PP.DaysOfWeek,PP.PrimaryAssetClass,PP.PremiumCurrency,PP.PremiumPaymentDate,PP.PremiumAmt,PP.DeliveryBand, VR.Bid, VR.Ask, VR.Mid , PP.Cusip,
PP.ISIN,
PP.PaymentFrequency,
PP.TicketNo,
PP.Leg1Index,
PP.Leg1ResetFrequency,
PP.CapFloorRate,
PP.EquityUnderlyingIdentifier,
PP.EquityIdentifierType,
PP.SecondLegRate,
PP.LegType,
PRI.Leg2Type,
PRI.Leg2Currency,
PRI.Leg2Index,
PRI.Leg2ResetFrequency,
PP.Leg1Payer,
PRI.Leg2Payer,
PRI.Leg2Spread
,PP.PortfolioHistId
,IA2.AccountId
ORDER BY VR.IsValUpdated Desc, PP.SecRef Asc


Mohd Sufian www.sqlship.wordpress.com Please mark the post as Answered if it helped.



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>