I am experiencing some negative performance on queries that are sorted.
This is how the query is being generated and rendered by NHibernate:
SELECT TOP 50 y0_,
y1_
FROM **(SELECT distinct r1_.Id as y0_,
this_.Id as y1_,
ROW_NUMBER()
OVER(ORDER BY this_.Id DESC) as __hibernate_sort_row
FROM Event this_
inner join Registration r1_
on this_.Id = r1_.EventId
WHERE this_.IsDeleted = 0 /* @p2 */
and this_.SportId in (22 /* @p3 */)) as query**
WHERE query.__hibernate_sort_row > 20
ORDER BY query.__hibernate_sort_row;
The bold part(within the **-symbols) is fetching all the results and puts them in order - this is what takes time. Is there any way i can make this query more efficient? I just want to make sorting and paging possible without having to create too much overhead.
I am using NHibernate 2.1. Any improvements in future releases that relates to my problem?
Best regards, Mattias
Run the query through the database tuning advisor, most likely there is a missing (or bad) index.