EntityFramework LINQToEntities产生奇怪的慢TSQL Where-Clause [英] EntityFramework LINQToEntities generate weird slow TSQL Where-Clause

查看:214
本文介绍了EntityFramework LINQToEntities产生奇怪的慢TSQL Where-Clause的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要了解这一点。
在代码生成的TSQL代码中,EF5.0和EF6之间有很大的区别。

I need to understand this. There is a big difference between EF5.0 and EF6.* in TSQL code-generation

在我的代码中,这是我的LINQ - statemant

In my code this is my LINQ - statemant

var qry2 = context.viw_overview_1.Where(i => i.article_EAN17 == ean).Select(i => i.article_id).Take(200);

EntityFramework 5.0 只生成一个简单而快速的TSQL WHERE - 这样的语句,这是完美的

EntityFramework 5.0 generate just a simple and fast TSQL WHERE - statement like this, which is perfect

... WHERE [Extent1].[article_EAN17] = @p__linq__0
00.0960096ms in SSMS

EntityFramework 6。* 生成一个复杂而缓慢的语句



but EntityFramework 6.* generate a much complex and slow statement

... WHERE (([Extent1].[article_EAN17] = @p__linq__0) AND ( NOT ([Extent1].[article_EAN17] IS NULL OR @p__linq__0 IS NULL))) OR (([Extent1].[article_EAN17] IS NULL) AND (@p__linq__0 IS NULL))
45.3665362ms in SSMS

字段article_EAN17也有一个索引。
然而EF6。*无论如何初始化,BUT 是否有一种方式在EF6。*中生成一个简单的WHERE语句,具有属性或类似的东西?
我尝试了 string.Equals() string.Compare(),整理参数,但没有改变。

the field article_EAN17 has a index, too. however EF6.* takes ages anyway to initialize, BUT is there a way to generate a simple WHERE statement in EF6.* with attributes or something like this? I tried string.Equals() , string.Compare() , swaping the parameter, but nothing changed.

为什么实体框架6生成复杂的SQL查询以进行简单查找?解释不同之处,但是有没有办法强制EF生成简单的TSQL。

Why does Entity Framework 6 generate complex SQL queries for simple lookups? explain the difference, But is there a way to force EF generating simple TSQL.

推荐答案

我相信这与Entity Framework中的NULL比较设置有关

I believe this is related to your NULL comparison setting in Entity Framework

在查询之前添加以下代码,以查看它是否有助于查询性能:

add the following code before your query to see if it helps your query performance:

context.ContextOptions.UseCSharpNullComparisonBehavior = true;

这篇关于EntityFramework LINQToEntities产生奇怪的慢TSQL Where-Clause的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆