实体框架定义查询为空+左连接 = 太慢

发布于 2024-11-01 06:01:39 字数 462 浏览 0 评论 0原文

有此 SQL 语句

select st.Column1, isnull(sot.SomeColumn, 0) as SomeColumn 
from SomeTable st 
left join SomeOtherTable sot 

我的 Entity Framework (4.0) .edmx 文件中的 标记内 ,并且我遇到了严重的性能问题。运行编写的 SQL 速度很快,但 EF 在运行时包装 SQL 以注入参数,这会大大减慢速度。

我可以去掉 isnull,它在包装的 EF SQL 中的速度与本机一样快,但我需要 isnull 来确保 SomeColumn 具有值。

我可以在这里使用 isnull 的替代方案吗?它可以很好地与 EF 配合使用吗?

感谢您的帮助。

I have this SQL statement

select st.Column1, isnull(sot.SomeColumn, 0) as SomeColumn 
from SomeTable st 
left join SomeOtherTable sot 

inside a <DefiningQuery> tag inside my Entity Framework (4.0) .edmx file and I'm running into a serious performance issue. Running the SQL as it's written is fast, but EF wraps the SQL during runtime to inject parameters, which slows it down tremendously.

I can take away the isnull, and it's just as fast in the wrapped EF SQL as it is natively, but I need the isnull to ensure SomeColumn has a value.

Are there any alternatives to isnull that I could use here that would play nicely with EF?

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚雾 2024-11-08 06:01:39

在 EF 设计器的属性属性中应该有一个默认值属性,但我不确定这是否能满足您的需求。作为替代方案,从 EF 模型生成的所有实体都是部分实体。我将添加另一个包装 SomeColumn 属性(不带 isnull)的属性以提供默认值。鉴于 SomeColumn 可能会映射为可为 null 的 int,您可以像这样定义新属性:

public SomeColumnWithDefaultValue {
    get { return this.SomeColumn ?? 0; }
    set { this.SomeColumn = value; }
}

您将针对 SomeColumn 编写查询,然后在需要确保它不为 null 的地方使用 SomeColumnWithDefaultValue。

In the property's properties in the EF designer there should be a default value property, I'm not sure if that will fit your needs though. As an alternative, all of the entities generated out of you EF model are partials. I would add another property that wraps the SomeColumn property (without the isnull) to provide a default value. Given that SomeColumn will likely be mapped as a nullable int, you can define the new property like this:

public SomeColumnWithDefaultValue {
    get { return this.SomeColumn ?? 0; }
    set { this.SomeColumn = value; }
}

You would write your queries against SomeColumn and then use SomeColumnWithDefaultValue where you needed to ensure that it was not null.

怪我太投入 2024-11-08 06:01:39

我的解决方案是从 SQL 中完全删除任何 isnull 的使用,而是包含一个 SELECT NEWID() AS ID (需要它与 SQL 2000+ 兼容)这些查询。一旦我将此 ID 设置为我的单个 ,我就可以允许将任何左连接属性设置为 isnullable=true 并且我不再需要应对 EF 速度变慢的问题。

希望这对某人有帮助。

My solution was to remove any use of isnull altogether from my SQL and instead include a SELECT NEWID() AS ID (needed this to be SQL 2000+ compatible) with all of these queries. Once I set this ID as my single <EntityKey>, I could allow set any of the left join properties as isnullable=true and I no longer had to deal with the EF slowdown.

Hope this helps someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文