M Wells (planetquirky@planetthoughtful.org) writes:[color=blue]
> Essentially the query I'm running is nothing more complex than:
>
> SELECT TOP 1 * FROM Table1 WHERE tier=n ORDER BY member_id
>
> [tier] is a smallint column with a non-clustered, non-unique index on
> it. [member_id] is a numeric column with a clustered, unique index on
> it.
>
> When I supply a [tier] value of 1, it returns results instantaneously.
> I have no idea if this is meaningful, but the tier = 1 records were
> loaded first into the table, and comprise approximately 5 million
> records.
>
> When I supply a [tier] value of 2, the results take 30 to 40 seconds.
> tier =2 records were loaded second, and comprise approximately 3
> million records.
>
> I've tried running an execution plan, and while I'm no expert, it
> appears to me that the index on tier isn't being used, even if I use:[/color]
This is a little funny. In most situations that index on tier would
be meaningless, but the TOP 1 should change everything.
The reason the index is meaningless is because for each hit in tier, SQL
Server needs to access the data page, in what is called a bookmark lookup,
and many bookmark lookups can easily be more expensive than scanning
the table.
However, since SQL Server users the clustered key as the row pointer in
a nonclustered index, SQL Server should be able to find that single row
by seeking the index on tier and then perform a single lookup.
So, indeed, upgrade to SP3, and see if it resolves the issue. No
guarantees that it will.
If it doesn't - it would be interesting to have a repro to give to
Microsoft, but producing a repro with eight million rows could mean
some problems.
This could be an alternative way of writing the query:
SELECT t.*
FROM tbl t
WHERE t.memberid = (SELECT MIN(t2.memberid)
FROM tbl t2
WHERE t2.tier = n)
--
Erland Sommarskog, SQL Server MVP,
sommar@algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp