Results 1 to 12 of 12
Nov 19, 2013, 01:21 #1
- Join Date
- Jun 2005
- Sydney, Australia
- 0 Post(s)
- 0 Thread(s)
Why is this query taking so long to execute?
I have a stored procedure that runs this query ...
SELECT * FROM tbl_owners WHERE OwnerPostcode IN (SELECT DISTINCT Postcode FROM tbl_postcodes WHERE TerritoryID = _TerritoryID) ORDER BY OwnerLastName;
The query takes a minute or more to run.
If I supply a list of postcodes e.g. ... WHERE OwnerPostcode in ("2144","2156","2142") ... the query is very fast.
This makes me think that maybe the query is running the subquery ever time. Is that the case? And if so, is there a better way to write this query so that it only does the subquery once?