Query optimization question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mathew Relick

    Query optimization question...

    I'm trying to optimize some queries on an existing system, and I'm
    noticing some odd behavior. I'm performing a join between several
    tables, the final table being joined by the optimizer has a clustered
    index on the field that it is using to join to the rest of the query,
    but for some reason SQL Server doesn't seem to actually use this index
    (it's doing an index scan instead of an index seek). Is there some
    reason why SQL Server would not use a valid Clustered Index? I've
    dropped and readded the index, but that doesn't seem to help. I don't
    know if it would be relevant, but the tables I'm working on are fairly
    fat (2 to 7K bytes/row).

    This is happening for several tables. I've been able to get around it
    for some of the tables by creating a non-clustered index on all the
    fields that are being queried so that the leaf pages don't need to be
    loaded, but this isn't a valid solution for all of the tables I'm
    struggling with.


    Any ideas? (and no, they aren't willing to redesign any of the
    tables).
  • Dave Hau

    #2
    Re: Query optimization question...

    "Mathew Relick" <ticars@yahoo.c om> wrote in message
    news:dd84d8a7.0 311181017.39d1c 69@posting.goog le.com...[color=blue]
    > I'm trying to optimize some queries on an existing system, and I'm
    > noticing some odd behavior. I'm performing a join between several
    > tables, the final table being joined by the optimizer has a clustered
    > index on the field that it is using to join to the rest of the query,
    > but for some reason SQL Server doesn't seem to actually use this index
    > (it's doing an index scan instead of an index seek). Is there some
    > reason why SQL Server would not use a valid Clustered Index? I've
    > dropped and readded the index, but that doesn't seem to help. I don't[/color]

    This can happen if your table statistics are out of date. If SQL Server
    determines that the join will require more than x % of the table to be
    retrieved, then doing a clustered index scan may be faster than doing a
    clustered index seek (faster because an index scan can read the rows in a
    page sequentially, whereas an index seek has to traverse the B-tree
    structure. Sequential read is faster because you cut down on seek time, and
    also because you may be able to read more than one page in a single I/O
    operation, since the data is sequential.) Try updating statistics and see
    if it helps:

    UPDATE STATISTICS <table_name> WITH FULLSCAN
    GO
    [color=blue]
    > know if it would be relevant, but the tables I'm working on are fairly
    > fat (2 to 7K bytes/row).
    >
    > This is happening for several tables. I've been able to get around it
    > for some of the tables by creating a non-clustered index on all the
    > fields that are being queried so that the leaf pages don't need to be
    > loaded, but this isn't a valid solution for all of the tables I'm
    > struggling with.[/color]

    This is because when you create a new index, new statistics are generated
    for that index, so you have the most up-to-date statistics with your new
    index. Same if you rebuild your existing indexes.

    HTH,
    Dave
    [color=blue]
    >
    >
    > Any ideas? (and no, they aren't willing to redesign any of the
    > tables).[/color]


    Comment

    • Greg D. Moore \(Strider\)

      #3
      Re: Query optimization question...


      "Dave Hau" <nospam_dave_no spam_123@nospam _netscape_nospa m.net_nospam> wrote
      in message news:gCvub.3378 8$yj4.5497@news svr27.news.prod igy.com...[color=blue]
      > "Mathew Relick" <ticars@yahoo.c om> wrote in message
      > news:dd84d8a7.0 311181017.39d1c 69@posting.goog le.com...[color=green]
      > > I'm trying to optimize some queries on an existing system, and I'm
      > > noticing some odd behavior. I'm performing a join between several
      > > tables, the final table being joined by the optimizer has a clustered
      > > index on the field that it is using to join to the rest of the query,
      > > but for some reason SQL Server doesn't seem to actually use this index
      > > (it's doing an index scan instead of an index seek). Is there some
      > > reason why SQL Server would not use a valid Clustered Index? I've
      > > dropped and readded the index, but that doesn't seem to help. I don't[/color]
      >
      > This can happen if your table statistics are out of date. If SQL Server
      > determines that the join will require more than x % of the table to be
      > retrieved, then doing a clustered index scan may be faster than doing a
      > clustered index seek (faster because an index scan can read the rows in a
      > page sequentially, whereas an index seek has to traverse the B-tree
      > structure. Sequential read is faster because you cut down on seek time,[/color]
      and[color=blue]
      > also because you may be able to read more than one page in a single I/O
      > operation, since the data is sequential.) Try updating statistics and see
      > if it helps:
      >
      > UPDATE STATISTICS <table_name> WITH FULLSCAN
      > GO
      >[color=green]
      > > know if it would be relevant, but the tables I'm working on are fairly
      > > fat (2 to 7K bytes/row).
      > >
      > > This is happening for several tables. I've been able to get around it
      > > for some of the tables by creating a non-clustered index on all the
      > > fields that are being queried so that the leaf pages don't need to be
      > > loaded, but this isn't a valid solution for all of the tables I'm
      > > struggling with.[/color]
      >
      > This is because when you create a new index, new statistics are generated
      > for that index, so you have the most up-to-date statistics with your new
      > index. Same if you rebuild your existing indexes.
      >[/color]

      I'm going to chime in because I think Dave has some good points here.
      However, as I understand it, the original poster did rebuild the clustered
      index, so there may be more to this problem than meets the eye.

      One other thing that can happen is that the optimizer decides it's faster to
      do a scan instead of a seek. This is particularly true if the result it
      expects to return is a large percentage of the index. (i.e. if you have 100
      rows and will return 80).

      I'll be honest, I'm not sure exactly how this applies with a clustered
      index.

      [color=blue]
      > HTH,
      > Dave
      >[color=green]
      > >
      > >
      > > Any ideas? (and no, they aren't willing to redesign any of the
      > > tables).[/color]
      >
      >[/color]


      Comment

      Working...