- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
首先,我在 SSMS 上尝试了 ARITHABORT OFF
,它仍然不到 1 秒。
我使用 EntityFrameWork:6.1.3 和 Azure Sql S1 层(我会尝试使用第 3 层,如果有变化会通知您。)
我使用 EF Profiler 从 linq 获取生成的 sql。我已经查询了我共享的所有 linq,它们在 SSMS 上都不到 1 秒。
我在 AuditLog 表上有 300 万条记录。 ID 为 3 的一位客户有 170K 条记录,另一位 ID 为 35 的客户有 125 条记录。我将最小化代码。
审计日志模型:
public class AuditLog
{
public long? CustomerId { get; set; }
[ForeignKey("CustomerId")]
public virtual CustomerSummary Customer { get; set; }
[Required]
[Index]
public DateTime CreatedDate { get; set; }
}
第一次查询:
if (customer != null)
{
var customerId = customer.Id;
var result= Dbset.Where(x => x.CustomerId == customerId).OrderByDescending(x => x.CreatedDate).Skip(0).Take(25).ToList();
}
如果我尝试与拥有 170k 行的客户一起尝试,它会给出超时异常。如果我对有 125 条记录的客户进行尝试,没问题。
第二个查询:与第一个查询相同,我只包括客户。
if (customer != null)
{
var customerId = customer.Id;
var result= Dbset.Where(x => x.CustomerId == customerId).OrderByDescending(x => x.CreatedDate).Skip(0).Take(25).Include(x => x.Customer).ToList();
}
结果与第一个查询相反。如果我尝试与拥有 170k 行的客户一起使用,那很好。如果我尝试与有 125 条记录的客户一起尝试,它会给出超时异常。
第三个查询:与第一个查询相同,但我在 customerId 的位置匹配 long?
。
if (customer != null)
{
long? customerId = customer.Id;
var result= Dbset.Where(x => x.CustomerId == customerId).OrderByDescending(x => x.CreatedDate).Skip(0).Take(25).ToList();
}
结果与第一个查询相反。如果我尝试与拥有 170k 行的客户一起使用,那很好。如果我尝试与有 125 条记录的客户一起尝试,它会给出超时异常。
第四个查询:与第二个查询相同,但我在 customerId 的位置匹配 long?
。
if (customer != null)
{
long? customerId = customer.Id;
var result= Dbset.Where(x => x.CustomerId == customerId).OrderByDescending(x => x.CreatedDate).Skip(0).Take(25).Include(x => x.Customer).ToList();
}
结果与第二个查询相反。如果我尝试与拥有 170k 行的客户一起尝试,它会给出超时异常。如果我对有 125 条记录的客户进行尝试,没问题。
我真的很困惑。为什么内部连接或将匹配参数更改为 long?
会改变结果?为什么所有这些查询在 SSMS 上运行不到 1 秒并在 ef linq 上给出错误?
错误:
{System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
更新(19/04/2016):
在 Ivan Stoev 之后意见建议。
Have you tried (just for the sake of test) using hardcoded 3 and 35 instead of
customerId
variable?
我没有收到任何错误,查询速度与在 SSMS 上一样快。
更新(2016 年 4 月 20 日):真正的问题是参数嗅探。当我将参数包含或更改为可为空时,实际上我已经创建了另一个查询和另一个查询计划。我为拥有 125 条记录的客户创建了一些计划,为拥有这 4 个查询的 170k 记录的客户创建了其他计划。这就是我得到不同结果的原因。
最佳答案
您遇到的是所谓的 Parameter Sniffing Problem 的结果。 .到目前为止,我还不知道一个简单的通用解决方案,因此通常建议通过在表达式中手动绑定(bind)常量值来消除一些 SQL 查询参数来提出解决方法,例如 EntityFramework LINQ query count fails but query returns result. How to optimize LINQ query? .
对于您的场景,我建议使用以下自定义扩展方法:
public static class QueryableExtensions
{
public static IQueryable<T> WhereEquals<T, TValue>(this IQueryable<T> source, Expression<Func<T, TValue>> selector, TValue value)
{
var predicate = Expression.Lambda<Func<T, bool>>(
Expression.Equal(selector.Body, Expression.Constant(value)),
selector.Parameters);
return source.Where(predicate);
}
}
然后像这样更新你的代码段
if (customer != null)
{
var result= Dbset.WhereEquals(x => x.CustomerId.Value, customer.Id)
.OrderByDescending(x => x.CreatedDate)
.Skip(0).Take(25)
.Include(x => x.Customer)
.ToList();
}
关于c# - Ef Linq 查询超时,但相同的查询在 SSMS 上不到 1 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36698792/
EF POCO 和 EF Code First 有什么区别? 如果我一开始只使用 POCO,我可以将 EF 与它们一起使用吗? 最佳答案 如果您首先使用 EF 代码,您将拥有 POCO 对象,并且数据
EF POCO 和 EF Code First 有什么区别? 如果我一开始只使用 POCO,我可以将 EF 与它们一起使用吗? 最佳答案 如果您首先使用 EF 代码,您将拥有 POCO 对象,并且数据
我有一个基于 .NET 4.8 和 EF 6.4.4 的项目。我们正在逐步迁移到 .Net Core,但在此过程中我可以创建一个 .NET Core 数据上下文类 EF Core 并将两者指向相同的实
我有以下 Entity Framework 5 代码第一类 public class Airplane { public int Id { get; set; } public int
我正在尝试使用 Entity Framework Core 对现有数据库进行逆向工程.我试着按照指示from Microsoft但我遇到了错误: Unable to find provider ass
当数据库不是由 EF 代码首先创建时,有没有办法检查 DbContext 是否与数据库匹配? 我正在寻找与 Database.CompatibleWithModel 类似的功能但没有元数据。 最佳答案
目前,我正在重构我的上下文方法的测试,以不再需要真正的数据库。我使用 Ef Core。 所以我通读了 Microsoft 文档如何测试上下文方法。我首先找到了 EF6 测试的文档,然后阅读了 EfCo
我正在使用 EF 6 Database.SqlQuery 语句来执行存储过程,并实现错误和事务处理,打开和关闭事务(处理多个记录,如果一个记录有错误,仅回滚此,并提交其他任何内容无错误完成)。 Lis
我首先使用 EF 数据库,因为我喜欢在 SQL Management Studio 中设计我的数据库,坦率地说,让 Visual Studio 直接从数据库创建所有实体非常容易,而无需执行任何代码。
我的项目中有几个迁移文件,由于我对上次迁移进行了手动修改,所以我不想使用“程序包管理器控制台”重新生成它。我只需要添加 1 列。所以在之前的迁移中手动添加了这个(我可以这样做,因为还没有人升级过)。
原文:https://bit.ly/2umidlb 作者:jon p smith 翻译:王亮 声明:我翻译技术文章不是逐句翻译的,而是根据我自己的理解来表述的。其中可能会去除一些本人实在不知道如何组
我们想开始一个新项目,我们决定从一开始就在一些表中使用 Columnstore 索引和聚簇索引,我们如何使用 Code First EF Core 3.1 做到这一点? 最佳答案 您应该将主键更改为非
我在 Entity Framework 6.0 上。这是一个开发问题,而不是生产问题。 我想我有一个相互矛盾的策略。 目前,我设置了 DropCreateDatabaseIfModelChanges
我在 VS 2012 RTM 上,使用 EF 5。我在做代码优先,但由于我只是在开发中,所以试图忽略代码迁移。为了避免它们,我有这一套 Database.SetInitializer(new Drop
我有复杂的审计字段类型 我的复杂类型: [ComplexType] public class AuditData { [Column("CreatorUserId")] public
我已经阅读了很多关于如何在关系中指定外键名称的帖子,没有遇到任何问题。不过,我想知道的是,有没有办法更改主键和关系的命名方式? 例如,您有一个以 UserId 作为主键的 User 表。 EF 代码第
我刚刚安装了新的Entity Framework 4.1 NuGet包,因此根据NuGet指令和this article of Scott Hanselman替换了EFCodeFirst包。 现在,想
我的应用程序基于 .NET 4.0 和 EF 4。我现在正在考虑升级到最新版本。 是否有任何可能对我的申请产生不利影响的重大变化或行为差异? 升级路径有多简单?升级到 EF 5 是否需要任何代码更改或
假设您必须使用 EF Code First 和 POCO 类开发支持多种语言的网站,您将如何对 POCO 类进行建模以支持这种情况? 通过多语言支持,我的意思不仅是拥有例如在一些资源文件中为您的 UI
我首先使用 EF 4.1 代码。鉴于以下类片段: public class Doctor { public virtual ICollection Hospitals { get; set;
我是一名优秀的程序员,十分优秀!