Why is doing a top(1) on an indexed column in SQL Server slow?

Posted by reinier on Stack Overflow See other posts from Stack Overflow or by reinier
Published on 2010-03-16T11:04:17Z Indexed on 2010/03/16 11:56 UTC
Read the original article Hit count: 268

I'm puzzled by the following. I have a DB with around 10 million rows, and (among other indices) on 1 column (campaignid_int) is an index.

Now I have 700k rows where the campaignid is indeed 3835

For all these rows, the connectionid is the same.

I just want to find out this connectionid.

 use messaging_db;
 SELECT     TOP (1) connectionid
 FROM         outgoing_messages WITH (NOLOCK)
 WHERE     (campaignid_int = 3835)

Now this query takes approx 30 seconds to perform!

I (with my small db knowledge) would expect that it would take any of the rows, and return me that connectionid

If I test this same query for a campaign which only has 1 entry, it goes really fast. So the index works.

How would I tackle this and why does this not work?

edit:

estimated execution plan:

select (0%) - top (0%) - clustered index scan (100%)

© Stack Overflow or respective owner

Related posts about tsql

Related posts about sql-server