deathwish wrote:
Otis wrote:
3) does the query indeed result in the results you wanted?
I knew as soon as you asked that question that I had done something wrong. I felt like I was back in junior school.
It does actually return the correct number of results.... DOH
I guess I should have debugged and checked the count before posting instead of just looking at the SQL and making an assumption. <Insert famous "Under Siege" quote here> It is part of background processing and not seen in the application that's why I just checked the SQL.
Sorry about that. Thanks for the great support.
I'm off to crawl under a rock.
haha Under Siege...
Well, it's not that dramatic, fortunately for you
. Looking at the filter, you could think why isn't there a TOP clause in the query? It would make the query more efficient (it's now reading as much rows as specified in Take and then closing the datareader)
To be able to reliably emit TOP in a query, the resultset should contain unique values only, after all, you're requesting unique values. As you're sorting on a field not in the select list, DISTINCT can't be emitted. And because you're executing a function, the DQE can't decide if the resultset contains unique values, so it can't safely emit TOP. To be able to return unique values anyway, it does limitation filtering on the client by reading a row from the datareader and checking whether this is a unique row, and if not, skip it, otherwise use it. (using hashes).
Your conclusion from looking at the query was correct, though TOP would be impossible to emit, so it took a different route, to bring you the correct answers.