I think that your predicate in the derived table is just quite expensive. Hard to say without an execution plan. Here are a couple of measures you can take:
- Try making
LEFT(FACETS_MEMBER.ZIP_CODE, 5)
a computed column and index it - Be sure that
R_ZIP_CODE.SHORT_ZIP_CODE
has an index on it - Be sure that
R_ZIP_CODE.SHORT_ZIP_CODE
has aNOT NULL
constraint on it.
Apart from that, I think that your DISTINCT
keyword in the outer query is useless, as you're adding the RowNum
, which is distinct for every record anyway. So you should move DISTINCT
into your nested query, which should use DENSE_RANK()
instead of ROW_NUMBER()
, as explained here: