Я использую NHibernate 2.0, и когда я отправляю запрос с просьбой вернуть две верхние записи, я получаю несколько предложений ORDER BY в моем SQL. Когда я вынимаю результаты Max, запрос выглядит нормально (без операторов ORDER BY). Почему NHibernate автоматически добавляет это, когда я ищу подмножество записей? Заранее спасибо
См. Операторы SQL ниже:
Неожиданный ЗАКАЗ ПО
exec sp_executesql
N'<b>SELECT TOP 2</b> Person1_36_0_, LastReco2_36_0_, SSN3_36_0_,
FirstName4_36_0_, LastName5_36_0_, MiddleIn6_36_0_, Title7_36_0_, Suffix8_36_0_,
DateOfBi9_36_0_, IsDeceased10_36_0_, Decease11_36_0_, Contact12_36_0_, MailHol13_36_0_,
MailHol14_36_0_, MailHol15_36_0_, Preferr16_36_0_, CreatedBy17_36_0_, Created18_36_0_,
ModifiedBy19_36_0_, Modifie20_36_0_
FROM <b>(SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_0__) as row</b>,
query.Person1_36_0_, query.LastReco2_36_0_, query.SSN3_36_0_, query.FirstName4_36_0_,
query.LastName5_36_0_, query.MiddleIn6_36_0_, query.Title7_36_0_, query.Suffix8_36_0_,
query.DateOfBi9_36_0_, query.IsDeceased10_36_0_, query.Decease11_36_0_,
query.Contact12_36_0_, query.MailHol13_36_0_, query.MailHol14_36_0_, query.MailHol15_36_0_,
query.Preferr16_36_0_, query.CreatedBy17_36_0_, query.Created18_36_0_,
query.ModifiedBy19_36_0_, query.Modifie20_36_0_, query.__hibernate_sort_expr_0__
FROM
(SELECT this_.Person_id as Person1_36_0_, this_.[LastRecordVersion] as LastReco2_36_0_,
this_.[SSN] as SSN3_36_0_, this_.[FirstName] as FirstName4_36_0_, this_.[LastName] as
LastName5_36_0_, this_.[MiddleInitial] as MiddleIn6_36_0_, this_.[Title] as Title7_36_0_,
this_.[Suffix] as Suffix8_36_0_, this_.[DateOfBirth] as DateOfBi9_36_0_, this_.[IsDeceased]
as IsDeceased10_36_0_, this_.[DeceasedDate] as Decease11_36_0_, this_.[ContactMethod_id] as
Contact12_36_0_, this_.[MailHoldReason_id] as MailHol13_36_0_, this_.[MailHoldStartDate] as
MailHol14_36_0_, this_.[MailHoldEndDate] as MailHol15_36_0_, this_.[PreferredName] as
Preferr16_36_0_, this_.[CreatedBy] as CreatedBy17_36_0_, this_.[CreatedDate] as
Created18_36_0_, this_.[ModifiedBy] as ModifiedBy19_36_0_, this_.[ModifiedDate] as
Modifie20_36_0_, CURRENT_TIMESTAMP as __hibernate_sort_expr_0__
FROM MC_Person this_
WHERE this_.[SSN] = @p0) query) page
WHERE **page.row > 0 ORDER BY __hibernate_sort_expr_0__**',
N'@p0 nvarchar(9)',@p0=N'123456789'
Правильный SQL (без получения Топ 2 записей)
exec sp_executesql
N'SELECT this_.Person_id as Person1_36_0_, this_.[LastRecordVersion] as LastReco2_36_0_,
this_.[SSN] as SSN3_36_0_, this_.[FirstName] as FirstName4_36_0_, this_.[LastName] as
LastName5_36_0_, this_.[MiddleInitial] as MiddleIn6_36_0_, this_.[Title] as Title7_36_0_,
this_.[Suffix] as Suffix8_36_0_, this_.[DateOfBirth] as DateOfBi9_36_0_, this_.[IsDeceased]
as IsDeceased10_36_0_, this_.[DeceasedDate] as Decease11_36_0_, this_.[ContactMethod_id] as
Contact12_36_0_, this_.[MailHoldReason_id] as MailHol13_36_0_, this_.[MailHoldStartDate] as
MailHol14_36_0_, this_.[MailHoldEndDate] as MailHol15_36_0_, this_.[PreferredName] as
Preferr16_36_0_, this_.[CreatedBy] as CreatedBy17_36_0_, this_.[CreatedDate] as
Created18_36_0_, this_.[ModifiedBy] as ModifiedBy19_36_0_, this_.[ModifiedDate] as
Modifie20_36_0_
FROM MC_Person this_
WHERE this_.[SSN] = @p0',
N'@p0 nvarchar(9)',@p0=N'123456789'