Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Sql Server doesn't use the right index Expand / Collapse
Author
Message
Posted Tuesday, June 25, 2013 8:13 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, October 31, 2013 8:21 AM
Points: 4, 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
Post #1467179
Posted Tuesday, June 25, 2013 8:24 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:17 AM
Points: 5,230, Visits: 9,457
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
Post #1467190
Posted Tuesday, June 25, 2013 9:35 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, October 31, 2013 8:21 AM
Points: 4, 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
Post #1467241
Posted Tuesday, June 25, 2013 9:58 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:17 AM
Points: 5,230, Visits: 9,457
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
Post #1467256
Posted Tuesday, June 25, 2013 10:02 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:35 PM
Points: 12,887, Visits: 31,832
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1467261
Posted Tuesday, June 25, 2013 10:10 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:17 AM
Points: 5,230, Visits: 9,457
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
Post #1467265
Posted Tuesday, June 25, 2013 10:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:35 PM
Points: 12,887, Visits: 31,832
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1467269
Posted Tuesday, June 25, 2013 10:27 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:11 PM
Points: 6,067, Visits: 5,283
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
Post #1467274
Posted Tuesday, June 25, 2013 10:36 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:17 AM
Points: 5,230, Visits: 9,457
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
Post #1467281
Posted Wednesday, June 26, 2013 2:11 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, October 31, 2013 8:21 AM
Points: 4, 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
Post #1467501
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse