question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unparameterized .Contains query using incorrect values after periods of high traffic

See original GitHub issue

Steps to reproduce

Unable to reproduce at this time

The issue

I have an API that switches DbContext from request to request based on the customer accessing it (this is because we use single tenant databases). We are seeing on occasion, after periods of high traffic, that the expressions compiled after using the Contains() operator use old values, as though a cache is being exhausted.

To give you some context:

Users might call GET /myEndpoint?ownerId=ABC&ownerId=DEF

The query parameters get translated into SQL via LINQ using something similar to:

if (query.OwnerIds!= null && query.OwnerIds.Any())
{
    predicate = predicate.And(p => query.OwnerIds.Contains(p.OwnerId) || query.OwnerIds.Contains(p.OwnerId2) || query.OwnerIds.Contains(p.OwnerId3));
}

The predicate then gets used when calling the database context ie: dbContext.Data.Where(predicate)

This correctly gets converted to a MySQL statement using IN () as I’d expect. If we take the URL example above (GET /myEndpoint?ownerId=ABC&ownerId=DEF) however, the query that gets executed against the database actually uses values from a previous query, so it might run WHERE ownerId IN ('GHI', 'JKL') OR ownerId2 IN ('GHI', 'JKL') OR ownerId3 IN ('GHI', 'JKL').

This only seems to occur after a period of high traffic has hit the API. Restarting the container that the API runs in resolves the issue, until it eventually happens again. The services run on docker containers on AWS ECS Fargate

I can rewrite the LINQ to chain OR conditions together, which then results in a parameterized query but rather horrible SQL. Can anybody suggest anything that might be causing the underlying problem? I’m not bothered if the IN () is not parameterized IF I can explain and resolve what’s going on with old values being used.

Further technical details

MySQL version: 5.7.12 Operating system: Windows 10 Pomelo.EntityFrameworkCore.MySql version: 5.0.0 5.0.0-alpha.2 Microsoft.AspNetCore.App version: 3.1 EntityFrameworkCore version: 5.0.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:18

github_iconTop GitHub Comments

1reaction
Ahmedrz89commented, Feb 4, 2022

I can confirm that this issue is a provider issue and not efcore itself and it seems to happen only in version : 5.0.0-alpha.2 and I was able to reproduce this in a simple console application not sure if it helps

efcore_mysql_bug.zip

1reaction
plittlewood-rptcommented, Dec 6, 2021

Item also logged on main efcore project at https://github.com/dotnet/efcore/issues/26905

Read more comments on GitHub >

github_iconTop Results From Across the Web

Avoiding SQL Server plan cache pollution due to ...
Avoiding SQL Server plan cache pollution due to unparameterized Contains queries generated by Entity Framework 6. In this post I will describe ...
Read more >
Avoiding SQL Server plan cache pollution due to ...
In this post I will describe an approach to solving an issue with EF Core and queries using Contains I have previously blogged...
Read more >
UG Knowledge Fusion Functions
The body tag list contains bodies with face-face intersection. ... no error Otherwise, Error - One or more of the titles and values...
Read more >
ı MoudE E
(specified in Maude with the keyword eq and ended with a period) are used from ... This list is typically low-traffic, and contains...
Read more >
Untitled
Chapter 7, LINQ Performance, explains how to perform LINQ queries with ... the array position of 100 is out of bounds, so an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found