简体   繁体   English

Nhibernate查询超时而SQL查询不

[英]Nhibernate query times out while sql query not

I'm running this code to fetch data: 我正在运行以下代码来获取数据:

var docs = session.Query<Content>().Where(x =>  x.Attachments.Count > 0).Skip(pageNumber * Pagesize).Take(count).ToList();

It should get all documents with attachements. 它应该带有附件的所有文件。 I added paging to boost it up, and not taking all documents at one step (there is about 10k documents matching and 600k all). 我添加了分页来增强它,而不是一步一步地获取所有文档(大约有1万个文档匹配而所有文档有60万个)。

Query executed by NHibernate: NHibernate执行的查询:

exec sp_executesql N'SELECT TOP (@p0) CastleId12_, Version12_, Abstract12_, Publishe4_12_, Title12_, Body12_, Brand12_, Source12_, SourceCo9_12_, IdInSource12_, Documen11_12_, HTML12_, Subscri13_12_, FileLoc14_12_, OtherMe15_12_, Companies12_, Keywords12_, Subscri18_12_, Author12_, Documen20_12_, SourceF21_12_, SourceB22_12_, UpdateDate12_, SourceU24_12_, Content25_12_, Interna26_12_, Workben27_12_, Checksum12_, Field29_12_ FROM (select content0_.CastleId as CastleId12_, content0_.Version as Version12_, content0_.Abstract as Abstract12_, content0_.PublishedDate as Publishe4_12_, content0_.Title as Title12_, content0_.Body as Body12_, content0_.Brand as Brand12_, content0_.Source as Source12_, content0_.SourceContentId as SourceCo9_12_, content0_.IdInSource as IdInSource12_, content0_.DocumentType as Documen11_12_, content0_.HTML as HTML12_, content0_.Subscriptions as Subscri13_12_, content0_.FileLocation as FileLoc14_12_, content0_.OtherMetadata as OtherMe15_12_, content0_.Companies as Companies12_, content0_.Keywords as Keywords12_, content0_.SubscriptionUpdateDate as Subscri18_12_, content0_.Author as Author12_, content0_.DocumentStatus as Documen20_12_, content0_.SourceFileExtension as SourceF21_12_, content0_.SourceBaseName as SourceB22_12_, content0_.UpdateDate as UpdateDate12_, content0_.SourceUpdateDate as SourceU24_12_, content0_.ContentUpdateDate as Content25_12_, content0_.InternalDocumentType as Interna26_12_, content0_.WorkbenchList as Workben27_12_, content0_.Checksum as Checksum12_, content0_.Field_id as Field29_12_, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row from [Content] content0_ where (select cast(count(*) as INT) from ContentAttachments attachment1_ where content0_.CastleId=attachment1_.DocumentId)>@p1) as query WHERE query.__hibernate_sort_row > @p2 ORDER BY query.__hibernate_sort_row',N'@p0 int,@p1 int,@p2 int',@p0=50,@p1=0,@p2=0

If I run it in that way it takes 1.5 minutes to execute in SSMS and of course it throws timeout exception in my app. 如果我以这种方式运行它,则需要花费1.5分钟才能在SSMS中执行,当然,它还会在我的应用中引发超时异常。

If I change it to: 如果我将其更改为:

SELECT TOP (50) CastleId12_, Version12_, Abstract12_, Publishe4_12_, Title12_, Body12_, Brand12_, Source12_, SourceCo9_12_, IdInSource12_, Documen11_12_, HTML12_, Subscri13_12_, FileLoc14_12_, OtherMe15_12_, Companies12_, Keywords12_, Subscri18_12_, Author12_, Documen20_12_, SourceF21_12_, SourceB22_12_, UpdateDate12_, SourceU24_12_, Content25_12_, Interna26_12_, Workben27_12_, Checksum12_, Field29_12_ FROM (select content0_.CastleId as CastleId12_, content0_.Version as Version12_, content0_.Abstract as Abstract12_, content0_.PublishedDate as Publishe4_12_, content0_.Title as Title12_, content0_.Body as Body12_, content0_.Brand as Brand12_, content0_.Source as Source12_, content0_.SourceContentId as SourceCo9_12_, content0_.IdInSource as IdInSource12_, content0_.DocumentType as Documen11_12_, content0_.HTML as HTML12_, content0_.Subscriptions as Subscri13_12_, content0_.FileLocation as FileLoc14_12_, content0_.OtherMetadata as OtherMe15_12_, content0_.Companies as Companies12_, content0_.Keywords as Keywords12_, content0_.SubscriptionUpdateDate as Subscri18_12_, content0_.Author as Author12_, content0_.DocumentStatus as Documen20_12_, content0_.SourceFileExtension as SourceF21_12_, content0_.SourceBaseName as SourceB22_12_, content0_.UpdateDate as UpdateDate12_, content0_.SourceUpdateDate as SourceU24_12_, content0_.ContentUpdateDate as Content25_12_, content0_.InternalDocumentType as Interna26_12_, content0_.WorkbenchList as Workben27_12_, content0_.Checksum as Checksum12_, content0_.Field_id as Field29_12_, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row from [Content] content0_ where (select cast(count(*) as INT) from ContentAttachments attachment1_ where content0_.CastleId=attachment1_.DocumentId)>0) as query WHERE query.__hibernate_sort_row > 0 ORDER BY query.__hibernate_sort_row 

(The only change is that I don't run it in sp_executesql and I'm inlining parameters) It takes about 2 seconds to execute. (唯一的变化是,我没有在sp_executesql中运行它,而是在插入参数),执行大约需要2秒钟。

Has anyone any idea how can I modify my Nhibernate query so it will work faster? 有谁知道如何修改我的Nhibernate查询,使其运行更快? I tried changing page size and page, but nothing changed. 我尝试更改页面大小和页面,但没有任何改变。 I read about sp_executesql but the only thing I found is about varchar arguments: https://stackoverflow.com/a/4540108/1714342 我读到有关sp_executesql但发现的唯一内容是有关varchar参数的信息: https : //stackoverflow.com/a/4540108/1714342

Why SSMS is executing it so slow? 为什么SSMS执行得这么慢?

You may try to : 您可以尝试:

  • set an explicit OrderBy (say on primary key) in your NHibernate query (as I guess the default generated ORDER BY CURRENT_TIMESTAMP does not help the optimizer) 在您的NHibernate查询中设置一个显式的OrderBy (例如主键)(因为我猜默认生成的ORDER BY CURRENT_TIMESTAMP不会对优化器有所帮助)
  • replace 更换

      .Where(x => x.Attachments.Count > 0) 

    with

      .Where(x => x.Attachments.Any()) 

    (I guess it would lead to a different generated SQL query) (我想这会导致生成不同的SQL查询)

It should get all documents with attachements. 它应该带有附件的所有文件。

I should recommend that instead of loading all documents, just load the basic information of the documents and show that to user. 我应该建议不要加载所有文档,而只是加载文档的基本信息并将其显示给用户。 And when user actually asks you to display content of data, load that specific document. 并且当用户实际要求您显示数据内容时,请加载该特定文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM