I have a view, vMyView which is doing a complex pivot select.If I run SELECT * FROM vMyView WHERE id = 7
the result returns in under a second.If however I runSELECT * FROM vMyView WHERE id = 7 ORDER BY PersonName
the result returns in about 5 minutes!Then if I runSELECT * INTO #temp FROM FROM vMyView WHERE id = 7SELECT * FROM#tempORDER BY PersonNameDROP TABLE #temp
the result will again return in under a second.It would appear that the order by is being applied to the whole view rather than just the subset I am selecting. I want to be able to do this in one select statement without using a temporary table. How can I force sql server to only apply the order by clause after it has selected the subset of information?I've triedSELECT * FROM (SELECT * FROM vMyView WHERE id = 7) AS InnerSelect ORDER BY PersonName
but that didn't work - it still took 5 minutes.