0

我正在使用 NHibernate 2.0,当我提交请求返回前 2 条记录的请求时,我的 SQL 中出现了一些 ORDER BY 子句。当我取出 Max 结果时,查询看起来很好(没有 ORDER BY 语句)。为什么 NHibernate 在我查找记录子集时会自动添加它?提前致谢

请参阅下面的 SQL 语句:

意外订购

执行 sp_executesql
N' SELECT TOP 2 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 (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_0__) 作为行,
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.DateOfBi9_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__
从
(选择 this_.Person_id 作为 Person1_36_0_,this_.[LastRecordVersion] 作为 LastReco2_36_0_,
this_.[SSN] 作为 SSN3_36_0_,this_.[FirstName] 作为 FirstName4_36_0_,this_.[LastName] 作为
LastName5_36_0_, this_.[MiddleInitial] 作为 MiddleIn6_36_0_, this_.[Title] 作为 Title7_36_0_,
this_.[Suffix] as Suffix8_36_0_, this_.[DateOfBirth] as DateOfBi9_36_0_, this_.[IsDeceased]
作为 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
修改 20_36_0_, CURRENT_TIMESTAMP 为 __hibernate_sort_expr_0__
FROM MC_Person this_
WHERE this_.[SSN] = @p0) 查询)页面
WHERE **page.row > 0 ORDER BY __hibernate_sort_expr_0__**',
N'@p0 nvarchar(9)',@p0=N'123456789'

正确的 SQL(没有获得 Top 2 记录)

执行 sp_executesql
N'SELECT this_.Person_id as Person1_36_0_, this_.[LastRecordVersion] as LastReco2_36_0_,
this_.[SSN] 作为 SSN3_36_0_,this_.[FirstName] 作为 FirstName4_36_0_,this_.[LastName] 作为
LastName5_36_0_, this_.[MiddleInitial] 作为 MiddleIn6_36_0_, this_.[Title] 作为 Title7_36_0_,
this_.[Suffix] as Suffix8_36_0_, this_.[DateOfBirth] as DateOfBi9_36_0_, this_.[IsDeceased]
作为 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
修改20_36_0_
FROM MC_Person this_
在哪里 this_.[SSN] = @p0',
N'@p0 nvarchar(9)',@p0=N'123456789'
4

2 回答 2

1

这是实现分页的方式。所以没有特殊情况只取前 n 个元素,因为例如 Oracle 不支持这种结构。

因此,任何设置了分页限制的任何事情都是以这种方式完成的。

你得到正确的结果了吗?

于 2009-03-31T15:29:27.813 回答
0

如果没有排序,顶级选择没有多大意义,因为排序可能会因数据库表的维护而改变。

难道你不以某种方式定义你想要的前两个结果吗?因此顺序?

于 2009-03-31T15:33:33.730 回答