all groups > sql server new users > may 2006 >
You're in the

sql server new users

group:

Sql weird behavior


Sql weird behavior csmba
5/22/2006 6:04:56 PM
sql server new users:
Hi all. I got the next very simple and strange problem. When running the
following 2 slqs, one runs very fast, and one very slow. I looked at my
indexes time and time again, and cannot figure out why. The table has about
8 million rows.

Very fast:

Select top 1 keyA from tableA where fieldB = 0 and keyA=keyA order by fieldC

Very slow:

Select top 1 keyA from tableA where fieldB = 0 order by fieldC

Properties:

keyA: integer unique key of tableA (clustered index)

fieldB: integer (non-clustered index)

fieldC: integer (non-clustered index)

any ideas?

Re: Sql weird behavior Jeje
5/22/2006 10:00:47 PM
maybe SQL Server use the fieldA index in the query 1 while the query B use
the fieldB index.

take a look at your resulting index plan to see which index(es) is used.

this will not explain the result but help us to understand what's appends.

[quoted text, click to view]

Re: Sql weird behavior Erland Sommarskog
5/23/2006 10:27:22 PM
csmba (csmba@nowhere.com) writes:
[quoted text, click to view]

So the optimizer need to decide: use index B to find all rows with
field B and sort these? Or traverse the index on fieldC, and perform
a bookmark lookup until a row with field B is found?

If there are many rows with fieldB = 0, the latter strategy is better.
If there are few rows, the first is better.

Of course, with an index on (fieldB, fieldC), the optimizer gets a
free ride.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: Sql weird behavior Kay-Christian Wessel
6/18/2006 12:14:27 AM
What about putting all 3 fields in an index ?

Kay

AddThis Social Bookmark Button