Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 query tuning help needed.

Author  Topic 

suba.anand
Starting Member

8 Posts

Posted - 2011-07-26 : 05:42:44
Hello i need help. My query is performing very bad. there are lot of calculations happening in that and i need to finetune it. can anybody help pls.

i have used 4 table types and inserting into them based on the calculations required. can anybody help me finetune this query?

i have pasted the piece of problematic code:

INSERT INTO @MIADetails
SELECT
CASE
WHEN InsurenceType = 'DE' AND CoverageType = 'DF' THEN @DD_Number /*PeckingOrderForPremiumDeductionsDefault*/

WHEN InsurenceType = 'DE' AND CoverageType = 'AD' THEN @AD_Number /*PeckingOrderForPremiumDeductionsAdditional*/

WHEN InsurenceType = 'TP' THEN @TPD_Number /*PeckingOrderForPremiumDeductions*/

WHEN InsurenceType = 'IP' THEN @IP_Number /*PeckingOrderForPremiumDeductions*/
END AS Sequence_Number,
InsurenceType,
CoverageType,
CoverType,
MI.EffectiveFromDate Period_StartDate,
CASE WHEN @PREMIUM_DEDUCTION_METHOD = 'AR' AND CONVERT(DATE, ECR.Payroll_End_Date) >= CONVERT(DATE, dbo.GetCoreDBReferenceDate(DEFAULT))
THEN dbo.GetCoreDBReferenceDate(DEFAULT)
WHEN @PREMIUM_DEDUCTION_METHOD = 'AR' AND MI.EffectiveToDate IS NULL
THEN ECR.Payroll_End_Date
WHEN @PREMIUM_DEDUCTION_METHOD = 'AR' AND MI.EffectiveToDate > ECR.Payroll_End_Date
THEN ECR.Payroll_End_Date
WHEN @PREMIUM_DEDUCTION_METHOD = 'AR' AND MI.EffectiveToDate < ECR.Payroll_End_Date
THEN MI.EffectiveToDate
WHEN @PREMIUM_DEDUCTION_METHOD = 'AD'
THEN ECR.Payroll_End_Date
END AS Period_EndDate,
CASE WHEN MI.InsurenceType = 'IP'
THEN (
(SELECT NUMERIC_VALUE FROM STRING_ARGUMENT ST
JOIN RULE_TABLE RT ON RT.RULE_TABLE_ID = ST.RULE_TABLE_ID
WHERE TABLE_CODE = 'ruletable_IPFormulaPremiumrate' AND LINK_ID = R.LINK_ID
--AND TABLE_ARGUMENT = 'Weekly Employer SG/Award Contribution Amount'
) *
(SELECT SUPER_GUARANTEE_AMOUNT FROM Return_detail WHERE CONTRIBUTION_RETURN_ID = ECR.CONTRIBUTION_RETURN_ID AND LINK_ID = ECR.LINKID))
ELSE PA.TotalPremium
END AS PremiumAmount,-- TotalPremium,
--FM.FrequencyOfPremiumCalculations PremiumFrequency,
0 NumberOfWeeks,
0 Calculated_Tax_Rebate,
MIAStatus,
MI.LinkID,
ECR.Cut_OffDate,
ECR.Last_COD,
ECR.Member_LCOD
FROM MEMBER_INSURANCE MI
JOIN @EmployerContributionRecords ECR
ON MI.LinkID = ECR.LINKID
AND MI.InsurenceType = ECR.Insurance_Type
AND CONVERT(DATE, MI.EffectiveFromDate) >= CONVERT(DATE, ECR.Payroll_Start_Date)
AND CONVERT(DATE, ISNULL(MI.EffectiveToDate, ECR.Payroll_End_Date)) <= CONVERT(DATE, ECR.Payroll_End_Date)
AND MI.MIAStatus IN ('APR', 'AL', 'DF')
--JOIN FORMULA FM
--ON FM.PolicyID = (SELECT TOP 1 PolicyID from dbo.GetPolicyDetails(MI.InsurenceType, MI.CoverType, dbo.GetCoreDBReferenceDate(DEFAULT), @ProductID, MI.LinkID))
--AND CONVERT(DATE, dbo.GetCoreDBReferenceDate(DEFAULT)) >= CONVERT(DATE, FM.EffectiveFromDate)
--AND CONVERT(DATE, dbo.GetCoreDBReferenceDate(DEFAULT)) <= ISNULL(CONVERT(DATE, FM.EffectiveToDate), CONVERT(DATE, dbo.GetCoreDBReferenceDate(DEFAULT)))
JOIN RELATIONSHIP R
ON R.CLIENT_ID = (SELECT TOP 1 PolicyID from dbo.GetPolicyDetails(MI.InsurenceType, MI.CoverType, dbo.GetCoreDBReferenceDate(DEFAULT), @ProductID, MI.LinkID))
AND R.RELATIONSHIP_TYPE IN ('GP', 'IP')
CROSS APPLY [dbo].[CalculateInsurancePremiumAmount](MI.InsurenceType, MI.CoverType, MI.CoverageType, dbo.GetCoreDBReferenceDate(DEFAULT), MI.EventID, MI.LINKID, @AccountNumber,MI.ApprovedUnits,MI.LoadingPercentage,MI.LoadingDollar,MI.InsVarRequestID)


AS PA
END

INSERT INTO @MIAFinalDetails
SELECT Sequence_Number,
InsurenceType,
CoverageType,
Period_StartDate,
Period_EndDate,
(PremiumAmount * NumberOfWeeks) PremiumAmount,
--PremiumFrequency,
CASE WHEN @APPLY_TAX_REBATE = 'Y'
THEN ((-@TAX_REBATE_PERCENTAGE / 100) * (PremiumAmount * NumberOfWeeks))
END Calculated_Tax_Rebate,
MIAStatus,
LinkID,
Cut_OffDate,
Last_COD,
Member_LCOD
FROM
(SELECT DISTINCT Sequence_Number,
InsurenceType,
CoverageType,
Period_StartDate,
Period_EndDate,
PremiumAmount,
--PremiumFrequency,
CASE WHEN @NUMBER_OF_DAYS = 'ND'
THEN (SELECT [dbo].[GetWeekdaysInInterval](Period_StartDate, Period_EndDate, @GIVEN_DAY))
ELSE
(SELECT (DATEDIFF(day, Period_StartDate, Period_EndDate) + 1) / 7.00)
END AS NumberOfWeeks,
Calculated_Tax_Rebate,
MIAStatus,
LinkID,
Cut_OffDate,
Last_COD,
Member_LCOD
FROM @MIADetails) TEMP

INSERT INTO @MIATEMPFINAL
SELECT
Sequence_Number,
InsurenceType,
Period_StartDate,
Period_EndDate,
SUM(PremiumAmount) PremiumAmount,
SUM(Calculated_Tax_Rebate) Calculated_Tax_Rebate,
AB.AccountBalance,
LinkID,
Cut_OffDate,
Last_COD,
Member_LCOD,
0 Calculated_InsurerSplitAmt,
0 Calculated_FundSplitAmt,
0 Actual_PremiumDeducted,
0 Applied_TaxRebate,
0 Applied_InsurerSplitAmt,
0 Applied_FundSplitAmt,
dbo.GetCoreDBReferenceDate(DEFAULT)
FROM @MIAFinalDetails
CROSS APPLY [dbo].[TransactionGetAccountBalances] (LinkID, dbo.GetCoreDBReferenceDate(DEFAULT), @ProductID) AS AB
WHERE AB.AccountType = 'MA'
GROUP BY LinkID, Sequence_Number, InsurenceType, AB.AccountBalance,
Period_StartDate, Period_EndDate, Cut_OffDate, Last_COD, Member_LCOD

please help its a urgent requirement.

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-07-26 : 05:52:23
Table definitions, index definitions, execution plan.

If it's urgent, consider hiring someone.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

suba.anand
Starting Member

8 Posts

Posted - 2011-07-26 : 05:55:31
hello gilamonster.. i have 3 seperate insert statements. I would just like to know if there is any possibility to convert into 2 with all calculations done.
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-07-26 : 07:56:17
Honestly, I'd start by changing the table variables to temp tables and seeing if those functions can maybe be removed. UDFs are usually terribly for performance.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-07-26 : 08:16:49
Here's your code with some formatting


INSERT INTO @MIADetails
SELECT
CASE
WHEN InsurenceType = 'DE' AND CoverageType = 'DF' THEN @DD_Number /*PeckingOrderForPremiumDeductionsDefault*/
WHEN InsurenceType = 'DE' AND CoverageType = 'AD' THEN @AD_Number /*PeckingOrderForPremiumDeductionsAdditional*/
WHEN InsurenceType = 'TP' THEN @TPD_Number /*PeckingOrderForPremiumDeductions*/
WHEN InsurenceType = 'IP' THEN @IP_Number /*PeckingOrderForPremiumDeductions*/
END AS Sequence_Number
, InsurenceType
, CoverageType
, CoverType
, MI.EffectiveFromDate Period_StartDate

, CASE
WHEN @PREMIUM_DEDUCTION_METHOD = 'AR' AND CONVERT(DATE, ECR.Payroll_End_Date) >= CONVERT(DATE, dbo.GetCoreDBReferenceDate(DEFAULT))
THEN dbo.GetCoreDBReferenceDate(DEFAULT)
WHEN @PREMIUM_DEDUCTION_METHOD = 'AR' AND MI.EffectiveToDate IS NULL
THEN ECR.Payroll_End_Date
WHEN @PREMIUM_DEDUCTION_METHOD = 'AR' AND MI.EffectiveToDate > ECR.Payroll_End_Date
THEN ECR.Payroll_End_Date
WHEN @PREMIUM_DEDUCTION_METHOD = 'AR' AND MI.EffectiveToDate < ECR.Payroll_End_Date
THEN MI.EffectiveToDate
WHEN @PREMIUM_DEDUCTION_METHOD = 'AD'
THEN ECR.Payroll_End_Date
END AS Period_EndDate

, CASE
WHEN MI.InsurenceType = 'IP' THEN (
(
SELECT NUMERIC_VALUE
FROM
STRING_ARGUMENT ST
JOIN RULE_TABLE RT ON RT.RULE_TABLE_ID = ST.RULE_TABLE_ID
WHERE
TABLE_CODE = 'ruletable_IPFormulaPremiumrate'
AND LINK_ID = R.LINK_ID
--AND TABLE_ARGUMENT = 'Weekly Employer SG/Award Contribution Amount'
) *
(
SELECT SUPER_GUARANTEE_AMOUNT
FROM Return_detail
WHERE
CONTRIBUTION_RETURN_ID = ECR.CONTRIBUTION_RETURN_ID
AND LINK_ID = ECR.LINKID
)
)
ELSE PA.TotalPremium
END AS PremiumAmount

--,TotalPremium
--, FM.FrequencyOfPremiumCalculations PremiumFrequency
, 0 NumberOfWeeks
, 0 Calculated_Tax_Rebate
, MIAStatus
, MI.LinkID
, ECR.Cut_OffDate
, ECR.Last_COD
, ECR.Member_LCOD
FROM
MEMBER_INSURANCE MI
JOIN @EmployerContributionRecords ECR ON
MI.LinkID = ECR.LINKID
AND MI.InsurenceType = ECR.Insurance_Type
AND CONVERT(DATE, MI.EffectiveFromDate) >= CONVERT(DATE, ECR.Payroll_Start_Date)
AND CONVERT(DATE, ISNULL(MI.EffectiveToDate, ECR.Payroll_End_Date)) <= CONVERT(DATE, ECR.Payroll_End_Date)
AND MI.MIAStatus IN ('APR', 'AL', 'DF')

--JOIN FORMULA FM
--ON FM.PolicyID = (SELECT TOP 1 PolicyID from dbo.GetPolicyDetails(MI.InsurenceType, MI.CoverType, dbo.GetCoreDBReferenceDate(DEFAULT), @ProductID, MI.LinkID))
--AND CONVERT(DATE, dbo.GetCoreDBReferenceDate(DEFAULT)) >= CONVERT(DATE, FM.EffectiveFromDate)
--AND CONVERT(DATE, dbo.GetCoreDBReferenceDate(DEFAULT)) <= ISNULL(CONVERT(DATE, FM.EffectiveToDate), CONVERT(DATE, dbo.GetCoreDBReferenceDate(DEFAULT)))

JOIN RELATIONSHIP R ON
R.CLIENT_ID = (SELECT TOP 1 PolicyID from dbo.GetPolicyDetails(MI.InsurenceType, MI.CoverType, dbo.GetCoreDBReferenceDate(DEFAULT), @ProductID, MI.LinkID))
AND R.RELATIONSHIP_TYPE IN ('GP', 'IP')

CROSS APPLY [dbo].[CalculateInsurancePremiumAmount](MI.InsurenceType, MI.CoverType, MI.CoverageType, dbo.GetCoreDBReferenceDate(DEFAULT), MI.EventID, MI.LINKID, @AccountNumber,MI.ApprovedUnits,MI.LoadingPercentage,MI.LoadingDollar,MI.InsVarRequestID) AS PA
END

INSERT INTO @MIAFinalDetails
SELECT
Sequence_Number
, InsurenceType
, CoverageType
, Period_StartDate
, Period_EndDate
, (PremiumAmount * NumberOfWeeks) PremiumAmount
--, PremiumFrequency
, CASE WHEN @APPLY_TAX_REBATE = 'Y' THEN ((-@TAX_REBATE_PERCENTAGE / 100) * (PremiumAmount * NumberOfWeeks)) END Calculated_Tax_Rebate
, MIAStatus
, LinkID
, Cut_OffDate
, Last_COD
, Member_LCOD
FROM
(
SELECT DISTINCT
Sequence_Number
, InsurenceType
, CoverageType
, Period_StartDate
, Period_EndDate
, PremiumAmount
--, PremiumFrequency

, CASE WHEN @NUMBER_OF_DAYS = 'ND' THEN (
SELECT [dbo].[GetWeekdaysInInterval](Period_StartDate, Period_EndDate, @GIVEN_DAY)
)
ELSE (
SELECT (DATEDIFF(day, Period_StartDate, Period_EndDate) + 1) / 7.00
)
END AS NumberOfWeeks

, Calculated_Tax_Rebate
, MIAStatus
, LinkID
, Cut_OffDate
, Last_COD
, Member_LCOD
FROM
@MIADetails
)
TEMP

INSERT INTO @MIATEMPFINAL
SELECT
Sequence_Number
, InsurenceType
, Period_StartDate
, Period_EndDate
, SUM(PremiumAmount) PremiumAmount
, SUM(Calculated_Tax_Rebate) Calculated_Tax_Rebate
, AB.AccountBalance
, LinkID
, Cut_OffDate
, Last_COD
, Member_LCOD
, 0 Calculated_InsurerSplitAmt
, 0 Calculated_FundSplitAmt
, 0 Actual_PremiumDeducted
, 0 Applied_TaxRebate
, 0 Applied_InsurerSplitAmt
, 0 Applied_FundSplitAmt
, dbo.GetCoreDBReferenceDate(DEFAULT)
FROM
@MIAFinalDetails
CROSS APPLY [dbo].[TransactionGetAccountBalances] (LinkID, dbo.GetCoreDBReferenceDate(DEFAULT), @ProductID) AS AB
WHERE
AB.AccountType = 'MA'
GROUP BY
LinkID
, Sequence_Number
, InsurenceType
, AB.AccountBalance
, Period_StartDate
, Period_EndDate
, Cut_OffDate
, Last_COD
, Member_LCOD


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-07-26 : 08:22:23
What gilamonster said.

though you could test each in isolation to see which is the slowest. You haven't said what 'good enough' is for you.

Also -- you seem to have a ton of subqueries. Almost always not a good idea.
Joins that have join conditions wrapped in functions so you can't use indexes. (though it's on a table variable so no index anyway....)

Frankly its a bit of a mess.

Maybe time to look at the requirement and start over?

What do you actually want to do: Can you define that with sample data / tables ddl and required output?

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

suba.anand
Starting Member

8 Posts

Posted - 2011-07-27 : 01:55:25
hi gilamonster. initially we used temp tables and when i ran this code for 25000 records the temp db log grew out enormously and so i have changed it to table types.
Go to Top of Page

suba.anand
Starting Member

8 Posts

Posted - 2011-07-27 : 02:47:08
Hi transactcharlie... i got this code from a guy who left this company. am not sure if i could rewrite this entire stuff given the time constraints. when i looked at the dmv's these three insert statements from the procedure seem to consume most of the time because of the reason that there are lot of sub queries and reference to user defined functions. these three inserts are happening into three seperate table types because of the required calculations. all i want to know is that if there is any chance to reduce the use of number of table types. can you please help?
Go to Top of Page
   

- Advertisement -