Sql Server doesn't use the right index

  • Hello,

    I've a strange issue with an index. I discover the sql have not used the right index last week.

    This's my SQL code:

    SELECT

    -- type

    'C ' AS [type],

    -- item

    RTRIM([models].[mode]) + '@' + RTRIM([skus].[grid]) AS [item] ,

    -- ware

    'PFG0' AS [ware],

    -- duedate

    LEFT([dbo].[get_date_fmt]([commessa_C_excp].[duedate],'GGMMAA',NULL),6) AS [duedate],

    -- outflow

    0 AS [outflow],

    -- backorder

    [PRODSTARTS].[maxqm] AS [backorder],

    -- mingrossreq

    0 AS [mingrossreq],

    -- maxgrossreq

    [commessa_C_excp].[maxgrossreq] AS [maxgrossreq]

    INTO #COMM_C_EXCP

    FROM [commessa_C_excp] INNER JOIN [skus]

    ON([skus].[id] = [commessa_C_excp].[sku_id])

    INNER JOIN [models]

    ON([models].[id] = [skus].[mode_id])

    INNER JOIN [fitcode]

    ON([fitcode].[id] = [skus].[fitcode_id])

    INNER JOIN [prod_starts] [PRODSTARTS]

    --

    --

    --

    WITH (INDEX(MI_prod_starts_linea_id_tipo_id_cod_rel_id_fitgrp_id_year))

    --

    --

    --

    ON([PRODSTARTS].[linea_id] = [models].[linea_id] AND

    [PRODSTARTS].[tipo_art_id] = [dbo].[tipo_id_std]([models].[tipo_id]) AND

    [PRODSTARTS].[cod_rel_id] = [dbo].[get_release_id]([skus].[id]) AND

    [PRODSTARTS].[fitgrp_id] = [fitcode].[fitgrp_id] AND

    [PRODSTARTS].[year] = YEAR([dbo].[skus_min_drel]([skus].[id]))

    )

    GO

    I'm forcing the select to use the index now, the problem is I don't understand why it stopped to use it.

    I try to rebuild it but no change.

    Best Regards

    Marco

  • Marco

    Please will you post table DDL for prod_starts, along with the CREATE INDEX statement for the index you mentioned? How many rows are in the table? How do you know the index isn't being used?

    John

  • Thanks,

    Here's the description of the table:

    CREATE TABLE [dbo].[prod_starts](

    [id] [int] IDENTITY(1,1) NOT NULL,

    [linea_id] [int] NOT NULL,

    [tipo_art_id] [int] NOT NULL,

    [cod_rel_id] [int] NOT NULL,

    [fitgrp_id] [int] NOT NULL,

    [year] [int] NULL,

    [dtsamp] [datetime] NULL,

    [dtlotto] [datetime] NULL,

    [qmode] [int] NULL,

    [maxqm] [int] NULL,

    [qsamp] [int] NULL,

    [qdecsamp] [int] NULL,

    [uagg] [varchar](100) NULL,

    CONSTRAINT [PK_prod_starts] PRIMARY KEY CLUSTERED

    (

    [id] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    and here's the code for the index:

    CREATE NONCLUSTERED INDEX [MI_prod_starts_linea_id_tipo_id_cod_rel_id_fitgrp_id_year] ON [dbo].[prod_starts]

    (

    [linea_id] ASC,

    [tipo_art_id] ASC,

    [cod_rel_id] ASC,

    [fitgrp_id] ASC,

    [year] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    In the table there are 1120 rows.

    I know it's not using the index because i use the execution plan tool to check what happens. The problem is that normally this select take 12 seconds, now it take 20 minutes

    The result of the select is about 100 rows.

    Thanks Marco

  • Marco

    If you only have 1120 rows, it may be that the query optimzer decides that it's better to do a clustered index scan than to use the index. I don't know on what basis it overrules index hints, nor whether that is even documented. I'd be surprised if the failure to use the index is the reason for the drop in performance. If you post the execution plan, we can have a look and see whether there are any clues.

    John

  • also, i see a scalar function in the selection, so that will substantially slow things down...it looks like it's just formatting the data as 6 chars, maybe 201306?

    lose the function and do an inline conversion instead for something that simple would help.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell (6/25/2013)


    also, i see a scalar function in the selection, so that will substantially slow things down...it looks like it's just formatting the data as 6 chars, maybe 201306?

    lose the function and do an inline conversion instead for something that simple would help.

    Lowell, if the query only returns about 100 rows, is that likely to have a large impact? I thought that maybe the YEAR function in the final join predicate might be the culprit, especially if skus_min_drel is a large table, but without the execution plan, we're just guessing.

    John

  • John Mitchell-245523 (6/25/2013)


    Lowell (6/25/2013)


    also, i see a scalar function in the selection, so that will substantially slow things down...it looks like it's just formatting the data as 6 chars, maybe 201306?

    lose the function and do an inline conversion instead for something that simple would help.

    Lowell, if the query only returns about 100 rows, is that likely to have a large impact? I thought that maybe the YEAR function in the final join predicate might be the culprit, especially if skus_min_drel is a large table, but without the execution plan, we're just guessing.

    John

    probably not a big impact, i agree John;

    I think one of the join criterias are using scalar functions though...i think that is the performance killer here:

    isn't this three different scalar functions being used to create the joins??

    [dbo].[tipo_id_std]()

    [dbo].[get_release_id]()

    [dbo].[skus_min_drel]()

    INNER JOIN [prod_starts] [PRODSTARTS]

    --

    --

    --

    WITH (INDEX (MI_prod_starts_linea_id_tipo_id_cod_rel_id_fitgrp_id_year))

    --

    --

    --

    ON (

    [PRODSTARTS].[linea_id] = [models].[linea_id]

    AND [PRODSTARTS].[tipo_art_id] = [dbo].[tipo_id_std]([models].[tipo_id])

    AND [PRODSTARTS].[cod_rel_id] = [dbo].[get_release_id]([skus].[id])

    AND [PRODSTARTS].[fitgrp_id] = [fitcode].[fitgrp_id]

    AND [PRODSTARTS].[year] = YEAR([dbo].[skus_min_drel]([skus].[id]))

    )

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Looking at the table the max row length on any data page would be something like 165 bytes and a minimum of about 25, so the data pages for this table would be in the 4 (32KB)-23(184KB) range. A point of history in SQL 6.5 if the table contained less than 41(82KB) data pages it would NEVER use any indexes no matter what hints you gave it. I'm going out on a limb but I wouldn't be surprised in later versions of SQL had a similar rule, I don't know this as fact but for tables of a certain size I'm willing to bet an index won't be used.

    And the LEFT used with the scalar function is likely to degrade performance because that function is fun once for EVERY row. So if that function is fairly complex it can REALLY slow the query down. As far as by how much, that's hard to guess. But you can get real world numbers by replacing that line with:

    [commessa_C_excp].[duedate]

    It will still query it just not used the function, that should give you the with/without metrics.

    CEWII

  • Lowell (6/25/2013)


    I think one of the join criterias are using scalar functions though...i think that is the performance killer here:

    isn't this three different scalar functions being used to create the joins??

    [dbo].[tipo_id_std]()

    [dbo].[get_release_id]()

    [dbo].[skus_min_drel]()

    Mmm, you're right, Lowell. I didn't even notice that amongst the lack of aliases and the square brackets. I agree: this is what's hurting performance, not the index not being used.

    Marco, scalar functions are bad for performance because they have to be executed for each row, and because they make the clause non-SARGable, which means that any index on the columns in question will (probably) not be able to be used.

    John

  • John Mitchell-245523 (6/25/2013)


    Lowell (6/25/2013)


    I think one of the join criterias are using scalar functions though...i think that is the performance killer here:

    isn't this three different scalar functions being used to create the joins??

    [dbo].[tipo_id_std]()

    [dbo].[get_release_id]()

    [dbo].[skus_min_drel]()

    Mmm, you're right, Lowell. I didn't even notice that amongst the lack of aliases and the square brackets. I agree: this is what's hurting performance, not the index not being used.

    Marco, scalar functions are bad for performance because they have to be executed for each row, and because they make the clause non-SARGable, which means that any index on the columns in question will (probably) not be able to be used.

    John

    Thanks John,

    I use the function because I have a large amount of .sql files and I prefer to store specific logic in only one point. They help me to have a simpler maintenance.

    For the moment I never have had performances issue with my function.

    Bye Marco

  • Hi everybody,

    Some news......

    I have an other index on the table prod_starts:

    CREATE NONCLUSTERED INDEX [FK_prod_starts_fitgrp_id] ON [dbo].[prod_starts]

    (

    [fitgrp_id] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    GO

    This morning I discovered the select is using this. I try to disable this index and it works well, with the right index and very speed.

    I don't understand why Sql Server chooses that index.

    I'll wait for a week with the index disabled, I hope it changes its statistics and fixes the situation

    By Marco

  • dazzim72 (6/26/2013)


    I use the function because I have a large amount of .sql files and I prefer to store specific logic in only one point. They help me to have a simpler maintenance.

    For the moment I never have had performances issue with my function.

    Marco

    Yes, performance problems can come on suddenly. A slight change in data can result in a totally different execution plan, and bang! You should consider converting your functions to table-valued functions.

    John

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply