我收到以下NHibernate超时异常:
could not execute query
内部异常消息为:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
这是失败的NHibernate代码:
shareClassReturns = _session.CreateCriteria<ShareClassTrailingReturn>()
.Add(LambdaSubquery.Property<ShareClassTrailingReturn>(x => x.ShareClass.Id).In(GetAvailableShareClassIds()))
.Add(LambdaSubquery.Property<ShareClassTrailingReturn>(x => x.ShareClass.Id).In(GetShareclassIdsInCalculationGroup(peerGroups, classificationTypes)))
.Add<ShareClassTrailingReturn>(c => c.CurrencyId == "GBP")
.AddShareClassReturnOrder(order)
.CreateCriteria<ShareClassTrailingReturn>(scr => scr.ShareClass, JoinType.InnerJoin)
.Add(ApplicableIdentifiers(searchExpression))
.AddShareClassOrder(order)
.SetMaxResults(pageSize)
.List<ShareClassTrailingReturn>();
这是我通过NHibernate Profiler看到的SQL(虽然我已经整理了一下,并用select top 25 *
替换了我选择的所有项目以使其更具可读性:
SELECT top 25 *
FROM offline.ShareClassTrailingReturn this_
inner join ShareManager.ShareClass shareclass1_
on this_.SCTR_ShareClassId = shareclass1_.ShareClass_Id
left outer join DCS.ShareClassInfo shareclass1_1_
on shareclass1_.ShareClass_Id = shareclass1_1_.[ShareClassInfo_MSShareClassId]
WHERE
this_.SCTR_ShareClassId in
(
SELECT this_0_.[Fund_ID] as y0_
FROM dbo.Fund this_0_
inner join CAP.DataUniverse datauniver1_
on this_0_.[Fund_TypeID] = datauniver1_.[DataUniverse_TypeId]
and this_0_.[Fund_CountryID] = datauniver1_.[DataUniverse_CountryID]
WHERE datauniver1_.[DataUniverse_SiteId] = 100 /* @p0 */
)
and this_.SCTR_ShareClassId in
(
SELECT this_0_.ShareClass_Id as y0_
FROM dbo.vCalculationGroup this_0_
WHERE
this_0_.PeerGroupId in (1,8)
and this_0_.ClassificationId in (7,1)
)
and this_.SCTR_CurrencyId = 'GBP'
and
(
shareclass1_.ShareClass_MEX like '%axa%'
or shareclass1_.ShareClass_SEDOL like '%axa%'
or shareclass1_1_.ShareClassInfo_RIC like '%axa%'
or shareclass1_.ShareClass_ISIN like '%axa%'
or shareclass1_.ShareClass_CUSIP like '%axa%'
)
ORDER BY shareclass1_.ShareClass_Name asc
如果我更改NHibernate标准,以便它不对vCalculationGroup执行选择,即如果我删除此行:
.Add(LambdaSubquery.Property<ShareClassTrailingReturn>(x => x.ShareClass.Id).In(GetShareclassIdsInCalculationGroup(peerGroups, classificationTypes)))
或来自SQL
and this_.SCTR_ShareClassId in
(
SELECT this_0_.ShareClass_Id as y0_
FROM dbo.vCalculationGroup this_0_
WHERE
this_0_.PeerGroupId in (1,8)
and this_0_.ClassificationId in (7,1)
)
查询不再超时。这是否意味着我需要对该视图执行某些操作?像索引还是什么?
有人可以建议我如何解决这个问题吗?
编辑:我应该补充说,查询在SQL Server Management Studio中运行良好,并在大约6秒后返回。虽然如此,当我查看执行计划时,它会报告在offline.ShareClassTrailingReturn上缺少索引。这可能是问题吗?
答案 0 :(得分:0)
如果删除该部分使其更好地工作,我肯定会看一下视图的索引。在您的应用程序中检查连接超时也是值得的。如果你说它在6秒内在Management Studio中工作正常,则可能是你的应用程序连接超时少于6秒(或默认值较小)。那些将是很好的起点。
值得尝试一般避免大量嵌套子查询。您在生成的SQL中有很多。有时,自己编写SQL或HQL更有效,而不是在Criteria或Linq NHibernate API中使用大量复杂的连接。你更有可能为这份工作生成高效的SQL!