Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2004
    Location
    Long Island
    Posts
    696

    Unanswered: Optimizer not using index

    I have a 33 million row table that for some reason goes into a table scan for a query (fine in 2000, started happening in 2005), but when I force an index hint, it uses the index and completes the query in 4 secs vs. 10minutes. Stats are up to date, the only thing is that it is a heap table.

    I cannot use the index hint since the query will run from a server link, any ideas ?

    Thanks

  2. #2
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    You will have to give us more info. For me, - I'd like to replicate your situation to see if I can actually get the scan when an index is present. Post the DDL, and the query.
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

  3. #3
    Join Date
    Jun 2004
    Location
    Long Island
    Posts
    696
    The indexes may be fragmented, I try a defrag first to see if that remedies the problem, if not, I'll post query.

    Thanks,

  4. #4
    Join Date
    Jun 2004
    Location
    Long Island
    Posts
    696
    If I have a value in the where clause for the date, works fine, function or variable, causes table scan. Did not behave like this on 2000

    The developer's a bullshit artist, the query has the same problem on 2000.
    Last edited by PMASchmed; 07-17-08 at 15:07.

  5. #5
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    Quote Originally Posted by PMASchmed
    The developer's a bullshit artist, the query has the same problem on 2000.
    we, uhhh I mean they all are.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  6. #6
    Join Date
    Jun 2004
    Location
    Long Island
    Posts
    696
    Quote Originally Posted by Thrasymachus
    we, uhhh I mean they all are.
    I found that if I create a dynamic sql query with the dates populated for the date range, it uses the index, otherwise I need to hint the index.

    Stupid Optimizer - Homer Simpson.

  7. #7
    Join Date
    Sep 2005
    Posts
    161
    Quote Originally Posted by PMASchmed
    If I have a value in the where clause for the date, works fine, function or variable, causes table scan. Did not behave like this on 2000
    Do you have a BETWEEN (or other kind of range) in your where clause? Often, when you have a range using variables, the optimizer will choose an index scan instead of an index seek because it can't guess how many rows will be returned by the query. One way to fix this is to use the optimize hint. For Example:
    OPTION (OPTIMIZE for (@start='1/1/2008', @end='1/2/2008'))
    The problem is that if you use this, and the actual values include a large date range, the query is still going to be optimized for a small date range.

  8. #8
    Join Date
    Jun 2004
    Location
    Long Island
    Posts
    696
    Quote Originally Posted by cascred
    Do you have a BETWEEN (or other kind of range) in your where clause? Often, when you have a range using variables, the optimizer will choose an index scan instead of an index seek because it can't guess how many rows will be returned by the query. One way to fix this is to use the optimize hint. For Example:
    OPTION (OPTIMIZE for (@start='1/1/2008', @end='1/2/2008'))
    The problem is that if you use this, and the actual values include a large date range, the query is still going to be optimized for a small date range.

    yes, thanks, just went this way:
    http://www.eggheadcafe.com/software/...-using-va.aspx

    Yes, but it seems the small data range will always use the index, which is what I want (at least for the moment)
    Last edited by PMASchmed; 07-17-08 at 17:01.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •