dotnet / dotnet/efcore

Extensive SQL translation in where-statement with nullable types

Open
#27,202 8 comments 4 reactions 0 assignees View on GitHub
area-query area-vb customer-reported ef6-parity
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

We encounter a heavy problem which slows down all our queries to the database.
All where statements are fully overloaded with COALESCE, CASE and CAST statements.

Let's say we have a linq query like:

```
Dim objQuery = From t In Me.DataStore.qryLedger
Where t.LdgPrdsKey = 705
```

It's getting translated to SQL as:

```
DECLARE @__$VB$Local_intPrdsKey_0 int = 705;
SELECT [q].*
FROM [qryLedger] AS [q]
WHERE COALESCE(CASE
WHEN ([q].[LdgPrdsKey] = @__$VB$Local_intPrdsKey_0) AND [q].[LdgPrdsKey] IS NOT NULL THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END, CAST(0 AS bit)) = CAST(1 AS bit)
```

This slows down the query execution in SQL Server by minimum double of time compared with:

```
DECLARE @__$VB$Local_intPrdsKey_0 int = 705;

SELECT [q].*
FROM [qryLedger] AS [q]
WHERE ([q].[LdgPrdsKey] = @__$VB$Local_intPrdsKey_0)
```

Why these extensive SQL transaltion and is there a way/switch/setting to avoid that?

We are using EF Core 6.0.1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.