A stored procedure that runs a SELECT on a large table is timing out. The where clause is causing the timeout, because I am selecting only cities that are in another table.
AND city IN (SELECT DISTINCT city from tOH where clientId = @clientId)
AND state IN (SELECT DISTINCT state from tOH where clientId = @clientId)
*note almost always only one state will be returned
I am trying to put the cities into a table and then use the table to populate the cities, but I am getting an error that @cities is not declared.
DECLARE @cities TABLE
(
city varchar(200)
);
INSERT INTO @cities (city) SELECT city FROM tOH WHERE clientId = @clientId GROUP BY city
Then my where clause changes to
AND city IN (SELECT city from @cities)
Can anyone figure out a good way of optimizing this stored procedure?
---------------------------- UPDATE ------------------------------------
The joins are all too slow. I think a solution with a temp table or table variable will work.