gpt4 book ai didi

c# - Nhibernate 查询超时而 sql 查询没有

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:25 25 4
gpt4 key购买 nike

我正在运行这段代码来获取数据:

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

它应该获取所有带附件的文档。我添加了分页来提升它,而不是在一个步骤中获取所有文档(大约有 10k 个匹配的文档和 600k 个文档)。

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

如果我以这种方式运行它,在 SSMS 中执行需要 1.5 分钟,当然它会在我的应用程序中抛出超时异常。

如果我把它改成:

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 

(唯一的变化是我不在 sp_executesql 中运行它,而是内联参数)执行大约需要 2 秒。

有谁知道如何修改我的 Nhibernate 查询以使其工作得更快?我尝试更改页面大小和页面,但没有任何改变。我阅读了有关 sp_executesql 的内容,但我发现的唯一内容是关于 varchar 参数:https://stackoverflow.com/a/4540108/1714342

为什么 SSMS 执行起来这么慢?

最佳答案

你可以尝试:

  • 在您的 NHibernate 查询中设置显式 OrderBy(比如主键)(因为我猜默认生成的 ORDER BY CURRENT_TIMESTAMP 对优化器没有帮助)
  • 替换

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

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

    (我猜这会导致生成不同的 SQL 查询)

关于c# - Nhibernate 查询超时而 sql 查询没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18231025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com