Skip to content

COALESCE in array.Contains() prevents index usage in 3.1.1 #1254

Closed

Description

@KinoshitaShimizu

The following C# code:

        var ids = new int [] { 11, 22, 33, 44, 55 };
        await context.Orders.Where(x => ids.Contains(x.OrderID)).ToListAsync();

Is producing the following SQL:

      SELECT o."OrderID", o."CustomerID", o."EmployeeID", o."OrderDate"
      FROM "Orders" AS o
      WHERE COALESCE(o."OrderID" = ANY (@__ids_0), FALSE)

This results in queries not being optimized/indexes not being used.

Code to reproduce:

  1. Create project.
dotnet new webapi ExampleIssueNpgsql
cd ExampleIssueNpgsql
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
dotnet add package Microsoft.EntityFrameworkCore.Design 
dotnet add package Microsoft.Extensions.Logging.Console
rm Startup.cs
  1. Place this into Program.cs and set proper database credentials:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;

namespace ExampleIssueNpgsql
{
  public class Program
  {
    public class OrderDetail
    {
      public int OrderDetailID { get; set; }
      public int OrderID { get; set; }
      public int ProductID { get; set; }
      public int Quantity { get; set; }
      public Order Order { get; set; }
    }

    public class Order
    {
      public int OrderID { get; set; }
      public int CustomerID { get; set; }
      public int EmployeeID { get; set; }
      public DateTime OrderDate { get; set; }
      public List<OrderDetail> OrderDetails { get; set; }
    }
    public class MyContext : DbContext
    {
      public DbSet<OrderDetail> OrderDetails { get; set; }
      public DbSet<Order> Orders { get; set; }

      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
        optionsBuilder
          .UseNpgsql(@"Host=localhost;Database=issue-db;Username=postgres;Password=password")
          .UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()));
      }
    }
    public static async Task Main(string[] args)
    {
      using (var context = new MyContext()) {
        var ids = new int [] { 11, 22, 33, 44, 55 };
        await context.Orders.Where(x => ids.Contains(x.OrderID)).ToListAsync();
      }
    }
  }
}
  1. Run the following to create test schema:
dotnet ef migrations add InitialCreate 
# NOTE: Might require fixing generated migration before running it
dotnet ef database update
  1. Run the program
dotnet run

Output will be the following:

info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 3.1.1 initialized 'MyContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL' with options: None
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (10ms) [Parameters=[@__ids_0='?' (DbType = Object)], CommandType='Text', CommandTimeout='30']
      SELECT o."OrderID", o."CustomerID", o."EmployeeID", o."OrderDate"
      FROM "Orders" AS o
      WHERE COALESCE(o."OrderID" = ANY (@__ids_0), FALSE)

COALESCE shouldn't have been generated.

Activity

  1. added this to the 3.1.2 milestone on Feb 4, 2020
  2. self-assigned this
    on Feb 4, 2020
  3. roji commented on Feb 6, 2020

    @roji
    Member

    Duplicate of #1152

  4. removed this from the 3.1.2 milestone on Feb 6, 2020
  5. removed their assignment
    on Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions