tags:

views:

386

answers:

1

I'm trying to run an update query that updates one table based on rows it finds in another table where a specific value exists anywhere in one of the remote table's column values. The query I'm using is:

update 
    c
set
    LastStep = @StepNumber,
    LastDate = pv.Created
from
    @Conversions c
        inner join PageViews pv on c.SessionID = pv.SessionID
where
    c.GoalName = @GoalName AND
    pv.Created > c.LastDate AND
    charindex(@MatchValue, pv.PageUrl) > 0;

On a test database with 50,000 rows in the PageViews table, this single query is causing just over 1 million reads according to SQL profiler and is taking 1 minute and 14 seconds. Any ideas why?

+1  A: 

Do the following column have indexes? pv.SessionID, pv.created

Have you looked at the resulting execution plan?

Jonas Lincoln
The resulting EP shows a clustered index scan for the where clause.
Chris
Ahahahaha I was missing a key index on session ID. I thought it was the primary key, but I was mistaken. Thanks!
Chris