SQL Server search filter and order by performance issues

Posted by John Leidegren on Stack Overflow See other posts from Stack Overflow or by John Leidegren
Published on 2010-03-10T13:51:41Z Indexed on 2010/03/14 16:25 UTC
Read the original article Hit count: 308

Filed under:
|

We have a table value function that returns a list of people you may access, and we have a relation between a search and a person called search result.

What we want to do is that wan't to select all people from the search and present them.

The query looks like this

SELECT qm.PersonID, p.FullName 
FROM QueryMembership qm
INNER JOIN dbo.GetPersonAccess(1) ON GetPersonAccess.PersonID = qm.PersonID
INNER JOIN Person p ON p.PersonID = qm.PersonID
WHERE qm.QueryID = 1234

There are only 25 rows with QueryID=1234 but there are almost 5 million rows total in the QueryMembership table. The person table has about 40K people in it.

QueryID is not a PK, but it is an index. The query plan tells me 97% of the total cost is spent doing "Key Lookup" witht the seek predicate.

QueryMembershipID = Scalar Operator (QueryMembership.QueryMembershipID as QM.QueryMembershipID)

Why is the PK in there when it's not used in the query at all? and why is it taking so long time?

The number of people total 25, with the index, this should be a table scan for all the QueryMembership rows that have QueryID=1234 and then a JOIN on the 25 people that exists in the table value function. Which btw only have to be evaluated once and completes in less than 1 second.

© Stack Overflow or respective owner

Related posts about sql-server-2005

Related posts about tsql