Index over multiuple columns question

  • Hi there,

    I have a select stateent which goes like this:

    SELECT TOP 1 *

    FROM table1

    WITH (READUNCOMMITED)

    WHERE Colx = @somevalue

    order by Colx, Coly

    The table holds millions of rows. There is an index on that table on both Colx, Coly. However, the WHERE clause only gives a value on Colx.

    I get a Clustered Index Scan in my ShowPlan. It's considered a slow query, the average WAITTIME is 130.000 ms and this query is executed1000s of time daily.

    I wonder if things would improve if I would create an extra index just on Colx. There are not many updates on that table, and sofar 10 indices already exist. I have no proper test environment to just create it and try it.

    Any help is appreciated

    Greetz,
    Hans Brouwer

  • If ColX is the leading column of that index, won't help at all. If your index is on (colX, Coly) (in that order) that query should not scan, it's perfectly suitable for a seek. Execution plan and index definitions?

    p.s. Why nolock? http://sqlblog.com/blogs/andrew_kelly/archive/2009/04/10/how-dirty-are-your-reads.aspx

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • OK, here comes:

    /****** Object: Index [$13] Script Date: 08/29/2011 14:05:06 ******/

    CREATE UNIQUE NONCLUSTERED INDEX [$13] ON [dbo].[Table1]

    (

    [Colx] ASC,

    [Coly] ASC

    )WITH

    (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF,

    ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 99)

    ON [Data Filegroup 1]

    BTW, this is a table/query generated by Navision.

    Greetz,
    Hans Brouwer

  • Please give me the actual index definition. I can't match a made-up one to the execution plan (that has the real table and column names in it).

    In fact, please post the definitions of all the indexes on [ABX_NAV_PRD].[dbo].[ABX$Item]

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • OK, the real thing:

    /****** Object: Index [$13] Script Date: 08/30/2011 08:54:40 ******/

    CREATE UNIQUE NONCLUSTERED INDEX [$13] ON [dbo].[ABX$Item]

    (

    [EAN-Code] ASC,

    [No_] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 99) ON [Data Filegroup 1]

    Greetz,
    Hans Brouwer

  • What's the clustered index definition?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Perhaps you have data skew, where say 70% of your Colx values are a single thing and the others are spread around? Or there are just 4 evenly-distributed values of Colx? Those would make it such that the optimizer would never use the index seek/bookmark lookup type of plan since a scan will be much more efficient. You can see this for yourself by forcing the index usage in your statement. use SET STATISTICS IO ON to see total reads for each plan.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • Hans,

    Are you sure the index hasn't been disabled? Since the query is a TOP 1, it really should have used the index for a seek, even if there aren't any rows that have an empty string as EAN-CODE.

    Todd Fifield

  • agree with Todd

  • Colx has about 11000 unique values on several million rows.

    On being disabled, I don't know, I'll have to check that. Never thought of that.

    Greetz,
    Hans Brouwer

  • I can't see any reason why that index wouldn't be used there. Doesn't make sense...

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

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

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