Skip to content

Queries really slow due to null checks #17543

@jamesgurung

Description

@jamesgurung

I'm getting a lot of slowdowns on the EF Core 3 preview. Here's an example.

var firstResult = await Db.Results.FirstOrDefaultAsync(o => o.TestId == 1);

This generates the following SQL, which runs really slowly on my SQL Azure database (>3 seconds):

exec sp_executesql N'SELECT TOP(1) [r].[Id]
FROM [Results] AS [r]
WHERE (([r].[TestId] = @__testId_0) AND ([r].[TestId] IS NOT NULL AND @__testId_0 IS NOT NULL))
OR ([r].[TestId] IS NULL AND @__testId_0 IS NULL)',N'@__testId_0 int',@__testId_0=1

The SQL I would expect is (<0.1 seconds):

exec sp_executesql N'SELECT TOP(1) [r].[Id]
FROM [Results] AS [r]
WHERE ([r].[TestId] = @__testId_0)',N'@__testId_0 int',@__testId_0=1

Further technical details

EF Core version: 3.0.0-preview8.19405.11
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2019 Version 16.3 Preview 2

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions