1

I created an index to help a really long 28 minute qry run faster and it doesn't seem to have helped to much.

This is the Index I created

USE [NLTR201212_test]
GO
CREATE NONCLUSTERED INDEX [Billys Index, sysname,]
ON [dbo].[tblInsurance] ([TERM_REASON])
INCLUDE ([POLICY_NO],[IssueYear],[ISSUE_DATE],[LM_PLAN_CODE],[AMOUNT_INFORCE],[StatReserve],[StatReserveX],[DefPremReserve],[ExcessCashValue],[ExcessCashValueX],[STAT2_PUA_RES],[STAT2_OYT_RES],[StatOYTRes2X],[COMPANY_CODE],[PHASE_CODE],[SUB_PHASE_CODE],[ProdType])
GO

This is the first index I've ever created so I wouldn't be surprised if I did it wrong somehow. TblInsurance does have a primary key made up from five columns (COMPANY_CODE, LINE_OFBUSINESS, POLICY_NO, PHASE_CODE and SUB_PHASE_CODE) already.

Not sure what else to do to help the situation.

This is the qry,

SELECT 
    qry_tempCashValue2.IssueYear, 
    qry_tempCashValue2.LM_PLAN_CODE, 
    Count(qry_tempCashValue2.POLICY_NO) AS CountOfPOLICY_NO, 
    qry_tempCashValue2.[Interest Rate Code]
FROM
(
    SELECT 
        qry_tempCashValue.POLICY_NO, 
        qry_tempCashValue.IssueYear, 
        qry_tempCashValue.ISSUE_DATE, 
        qry_tempCashValue.LM_PLAN_CODE, 
        qry_tempCashValue.AMOUNT_INFORCE, 
        qry_tempCashValue.StatReserve, 
        qry_tempCashValue.StatReserveX, 
        qry_tempCashValue.DefPremReserve, 
        qry_tempCashValue.ExcessCashValue, 
        qry_tempCashValue.ExcessCashValueX, 
        qry_tempCashValue.STAT2_PUA_RES, 
        qry_tempCashValue.STAT2_OYT_RES, 
        qry_tempCashValue.StatOYTRes2X, 
        qry_tempCashValue.[Calc Parameters Code], 
        Max(qry_tempCashValue.[Low Issue Date]) AS [MaxOfLow Issue Date], 
        qry_tempCashValue.[Interest Rate Code]
    FROM
    (
        SELECT 
            tblInsurance.POLICY_NO, 
            tblInsurance.IssueYear, 
            tblInsurance.ISSUE_DATE, 
            tblInsurance.LM_PLAN_CODE, 
            tblInsurance.AMOUNT_INFORCE, 
            tblInsurance.StatReserve, 
            tblInsurance.StatReserveX, 
            tblInsurance.DefPremReserve, 
            tblInsurance.ExcessCashValue, 
            tblInsurance.ExcessCashValueX, 
            tblInsurance.STAT2_PUA_RES, 
            tblInsurance.STAT2_OYT_RES, 
            tblInsurance.StatOYTRes2X, 
            qryPolyCalcParameters.[Calc Parameters Code], 
            qryPolyCalcParameters.[Low Issue Date], 
            qryPolyCalcParameters.[Interest Rate Code]
        FROM tblInsurance 
        INNER JOIN qryPolyLifeMasterPlans 
            ON tblInsurance.LM_PLAN_CODE = 
                qryPolyLifeMasterPlans.[LifeMaster Plan Code] 
        INNER JOIN qryPolyNonforfeitureValues 
            ON qryPolyLifeMasterPlans.[Nonforfeiture Value Code] = 
                qryPolyNonforfeitureValues.[Nonforfeiture Value Code] 
        INNER JOIN qryPolyCalcParameters 
            ON qryPolyNonforfeitureValues.[(Cash Value) Calc Parameters Code] = 
                qryPolyCalcParameters.[Calc Parameters Code]
        WHERE 
            qryPolyCalcParameters.[Low Issue Date]<[ISSUE_DATE] 
            AND tblInsurance.COMPANY_CODE='NL' 
            AND tblInsurance.LINE_OF_BUSINESS='IT' 
            AND tblInsurance.SchedNP='PAR'
            AND tblInsurance.TERM_REASON='A' 
            AND tblInsurance.ProdType='PERM' 
            AND tblInsurance.PHASE_CODE=0 
            AND tblInsurance.SUB_PHASE_CODE=1 
        ) qry_tempCashValue
    GROUP BY 
        qry_tempCashValue.POLICY_NO, 
        qry_tempCashValue.IssueYear, 
        qry_tempCashValue.ISSUE_DATE, 
        qry_tempCashValue.LM_PLAN_CODE, 
        qry_tempCashValue.AMOUNT_INFORCE, 
        qry_tempCashValue.StatReserve, 
        qry_tempCashValue.StatReserveX, 
        qry_tempCashValue.DefPremReserve, 
        qry_tempCashValue.ExcessCashValue, 
        qry_tempCashValue.ExcessCashValueX, 
        qry_tempCashValue.STAT2_PUA_RES, 
        qry_tempCashValue.STAT2_OYT_RES, 
        qry_tempCashValue.StatOYTRes2X, 
        qry_tempCashValue.[Calc Parameters Code], 
        qry_tempCashValue.[Interest Rate Code]
    ) qry_tempCashValue2
GROUP BY 
    qry_tempCashValue2.IssueYear, 
    qry_tempCashValue2.LM_PLAN_CODE, 
    qry_tempCashValue2.[Interest Rate Code];

GO
5
  • A table can only ever have one primary key, by definition. You're probably talking about a primary key made up of 5 columns - right? And which ones would those be??
    – marc_s
    May 22, 2013 at 16:04
  • Yes Thats what I mean. The columns are COMPANY_CODE LINE_OFBUSINESS POLICY_NO PHASE_CODE and SUB_PHASE_CODE. The the table tblinsurance is around 300,000 rows May 22, 2013 at 16:09
  • All of those columns most definitely do not belong into your nonclustered index - they're already part of it anyway.
    – marc_s
    May 22, 2013 at 16:10
  • would that prevent the index from working properly? May 22, 2013 at 16:12
  • The query optimizer uses a lot of factors to decide what approach to take - no quick'n'easy answers here.....
    – marc_s
    May 22, 2013 at 16:14

3 Answers 3

2

Rather than focusing on adding indices, I think you should clean up your query. I don't know why you are nesting this, instead of just doing the group by and the select all at once.

    SELECT 
        tblInsurance.POLICY_NO, 
        tblInsurance.IssueYear, 
        tblInsurance.ISSUE_DATE, 
        tblInsurance.LM_PLAN_CODE, 
        tblInsurance.AMOUNT_INFORCE, 
        tblInsurance.StatReserve, 
        tblInsurance.StatReserveX, 
        tblInsurance.DefPremReserve, 
        tblInsurance.ExcessCashValue, 
        tblInsurance.ExcessCashValueX, 
        tblInsurance.STAT2_PUA_RES, 
        tblInsurance.STAT2_OYT_RES, 
        tblInsurance.StatOYTRes2X, 
        qryPolyCalcParameters.[Calc Parameters Code], 
        max(qryPolyCalcParameters.[Low Issue Date]) AS [MaxOfLow Issue Date], 
        qryPolyCalcParameters.[Interest Rate Code]
    FROM tblInsurance 
    INNER JOIN qryPolyLifeMasterPlans 
        ON tblInsurance.LM_PLAN_CODE = 
            qryPolyLifeMasterPlans.[LifeMaster Plan Code] 
    INNER JOIN qryPolyNonforfeitureValues 
        ON qryPolyLifeMasterPlans.[Nonforfeiture Value Code] = 
            qryPolyNonforfeitureValues.[Nonforfeiture Value Code] 
    INNER JOIN qryPolyCalcParameters 
        ON qryPolyNonforfeitureValues.[(Cash Value) Calc Parameters Code] = 
            qryPolyCalcParameters.[Calc Parameters Code]
    WHERE 
        qryPolyCalcParameters.[Low Issue Date]<[ISSUE_DATE] 
        AND tblInsurance.COMPANY_CODE='NL' 
        AND tblInsurance.LINE_OF_BUSINESS='IT' 
        AND tblInsurance.SchedNP='PAR'
        AND tblInsurance.TERM_REASON='A' 
        AND tblInsurance.ProdType='PERM' 
        AND tblInsurance.PHASE_CODE=0 
        AND tblInsurance.SUB_PHASE_CODE=1 
GROUP BY 
    tblInsurance.POLICY_NO, 
    tblInsurance.IssueYear, 
    tblInsurance.ISSUE_DATE, 
    tblInsurance.LM_PLAN_CODE, 
    tblInsurance.AMOUNT_INFORCE, 
    tblInsurance.StatReserve, 
    tblInsurance.StatReserveX, 
    tblInsurance.DefPremReserve, 
    tblInsurance.ExcessCashValue, 
    tblInsurance.ExcessCashValueX, 
    tblInsurance.STAT2_PUA_RES, 
    tblInsurance.STAT2_OYT_RES, 
    tblInsurance.StatOYTRes2X, 
    qryPolyCalcParameters.[Calc Parameters Code], 
    qryPolyCalcParameters.[Interest Rate Code]

That should help. Then we can see about getting rid of the other nesting. It's your subselects that a are killing your performance.

8
  • 1
    That chang turned it into a 2 second qry. Thank you so much. I'm going to do my best to apply this line of thought to many other qrys that take 5 minutes. May 22, 2013 at 16:21
  • Glad it helped, but there is still some improvements possible. I don't think you need any subselect. Keep trying! And please accept my answer.
    – Bill Gregg
    May 22, 2013 at 16:22
  • Hold on I made a small mistake when i edited the qry. I accidentally deleted the top 2 selects. This is what I have now and now the qry won't function because the top 2 selects are in red. I think im probably just missing like a comma or ) somewhere but I can't find it May 22, 2013 at 16:27
  • I can't post the new version of the qry here, if I edit the question and insert the new version will you help me find the problem? May 22, 2013 at 16:33
  • @wagregg . . . Your point is well-taken. However, the OP query is grouped by three columns and yours by many, many more. I don't see how they are equivalent. May 22, 2013 at 16:52
0

I think you can turn the query into this:

SELECT 
    qry_tempCashValue.IssueYear, 
    qry_tempCashValue.LM_PLAN_CODE, 
    Count(distinct qry_tempCashValue.POLICY_NO) AS CountOfPOLICY_NO, 
    qry_tempCashValue.[Interest Rate Code]
FROM
(
        SELECT 
            tblInsurance.POLICY_NO, 
            tblInsurance.IssueYear, 
            tblInsurance.ISSUE_DATE, 
            tblInsurance.LM_PLAN_CODE, 
            tblInsurance.AMOUNT_INFORCE, 
            tblInsurance.StatReserve, 
            tblInsurance.StatReserveX, 
            tblInsurance.DefPremReserve, 
            tblInsurance.ExcessCashValue, 
            tblInsurance.ExcessCashValueX, 
            tblInsurance.STAT2_PUA_RES, 
            tblInsurance.STAT2_OYT_RES, 
            tblInsurance.StatOYTRes2X, 
            qryPolyCalcParameters.[Calc Parameters Code], 
            qryPolyCalcParameters.[Low Issue Date], 
            qryPolyCalcParameters.[Interest Rate Code]
        FROM tblInsurance 
        INNER JOIN qryPolyLifeMasterPlans 
            ON tblInsurance.LM_PLAN_CODE = 
                qryPolyLifeMasterPlans.[LifeMaster Plan Code] 
        INNER JOIN qryPolyNonforfeitureValues 
            ON qryPolyLifeMasterPlans.[Nonforfeiture Value Code] = 
                qryPolyNonforfeitureValues.[Nonforfeiture Value Code] 
        INNER JOIN qryPolyCalcParameters 
            ON qryPolyNonforfeitureValues.[(Cash Value) Calc Parameters Code] = 
                qryPolyCalcParameters.[Calc Parameters Code]
        WHERE 
            qryPolyCalcParameters.[Low Issue Date]<[ISSUE_DATE] 
            AND tblInsurance.COMPANY_CODE='NL' 
            AND tblInsurance.LINE_OF_BUSINESS='IT' 
            AND tblInsurance.SchedNP='PAR'
            AND tblInsurance.TERM_REASON='A' 
            AND tblInsurance.ProdType='PERM' 
            AND tblInsurance.PHASE_CODE=0 
            AND tblInsurance.SUB_PHASE_CODE=1 
        ) qry_tempCashValue
GROUP BY 
    qry_tempCashValue.IssueYear, 
    qry_tempCashValue.LM_PLAN_CODE, 
    qry_tempCashValue.[Interest Rate Code];

This eliminates the inner aggregation and counts the policies using count(distinct). Depending on your data, this may not return the same results.

As for indexes, a good place to start is to have an index on the key for each table used in the join. And another on all the columns in the where clause on tblInsrance that use =.

8
  • I get an error highlighting the first select and the last group by May 22, 2013 at 17:08
  • I elminated the 2 from tempcashvalues and it seems to have fixed that problem and I am now testing the qry. Unsure if I will get the same results however May 22, 2013 at 17:25
  • I've heard that something called a Temp Table may also be helpful what do you think of this? May 22, 2013 at 17:33
  • @Gordan Linoff This actually helped shave 3 minutes off and it grabbed all the same information. Unfortunetly its still much too long. May 22, 2013 at 17:59
  • @user2379197 . . . This type of query does not need temporary tables. You are doing a fairly common type of query, joining together some tables and aggregating the results. May 22, 2013 at 18:21
0

Here is the whole query simplified.

SELECT 
    qry_tempCashValue2.IssueYear, 
    qry_tempCashValue2.LM_PLAN_CODE, 
    Count(qry_tempCashValue2.POLICY_NO) AS CountOfPOLICY_NO, 
    qry_tempCashValue2.[Interest Rate Code]
FROM
(
    SELECT distinct
        tblInsurance.POLICY_NO, 
        tblInsurance.IssueYear, 
        tblInsurance.ISSUE_DATE, 
        tblInsurance.LM_PLAN_CODE, 
        tblInsurance.AMOUNT_INFORCE, 
        tblInsurance.StatReserve, 
        tblInsurance.StatReserveX, 
        tblInsurance.DefPremReserve, 
        tblInsurance.ExcessCashValue, 
        tblInsurance.ExcessCashValueX, 
        tblInsurance.STAT2_PUA_RES, 
        tblInsurance.STAT2_OYT_RES, 
        tblInsurance.StatOYTRes2X, 
        qryPolyCalcParameters.[Calc Parameters Code], 
        qryPolyCalcParameters.[Interest Rate Code]
        FROM tblInsurance 
        INNER JOIN qryPolyLifeMasterPlans 
            ON tblInsurance.LM_PLAN_CODE = 
                qryPolyLifeMasterPlans.[LifeMaster Plan Code] 
        INNER JOIN qryPolyNonforfeitureValues 
            ON qryPolyLifeMasterPlans.[Nonforfeiture Value Code] = 
                qryPolyNonforfeitureValues.[Nonforfeiture Value Code] 
        INNER JOIN qryPolyCalcParameters 
            ON qryPolyNonforfeitureValues.[(Cash Value) Calc Parameters Code] = 
                qryPolyCalcParameters.[Calc Parameters Code]
        WHERE 
            qryPolyCalcParameters.[Low Issue Date]<[ISSUE_DATE] 
            AND tblInsurance.COMPANY_CODE='NL' 
            AND tblInsurance.LINE_OF_BUSINESS='IT' 
            AND tblInsurance.SchedNP='PAR'
            AND tblInsurance.TERM_REASON='A' 
            AND tblInsurance.ProdType='PERM' 
            AND tblInsurance.PHASE_CODE=0 
            AND tblInsurance.SUB_PHASE_CODE=1 
    ) qry_tempCashValue2
GROUP BY 
    qry_tempCashValue2.IssueYear, 
    qry_tempCashValue2.LM_PLAN_CODE, 
    qry_tempCashValue2.[Interest Rate Code];

GO
3
  • It has just passed the 25 minute mark so its slower then the new version I have now based off of Gordans suggestion. Thank you for trying May 22, 2013 at 19:58
  • Well that's disappointing. Is there a lot of contention on those tables? Would some NOLOCK hints help?
    – Bill Gregg
    May 22, 2013 at 20:08
  • Only tbl insurance is a table the others are views. I think this must be why it takes so long. Sql has to run each of those qry's use din the joins which themselves contain tables and other qrys. Making this qry run faster is either impossible at this point or going to take a lot of effort. May 22, 2013 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.