SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Sql Server doesn't use the right index


Sql Server doesn't use the right index

Author
Message
dazzim72
dazzim72
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 9
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
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35049 Visits: 16670
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
dazzim72
dazzim72
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 9
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
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35049 Visits: 16670
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
Lowell
Lowell
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72815 Visits: 40956
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!
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35049 Visits: 16670
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
Lowell
Lowell
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72815 Visits: 40956
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!
Elliott Whitlow
Elliott Whitlow
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24344 Visits: 5314
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
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35049 Visits: 16670
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
dazzim72
dazzim72
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 9
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search