Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlServer Error 8623 The query processor ran out of internal resources and could not produce a query plan #11006

Closed
xaviergxf opened this issue Feb 19, 2018 · 18 comments

Comments

@xaviergxf
Copy link

When doing the following sql with ids containing more than 16000 ids:

db.Table.Where(p=>p.ids.Contains(p.ID)).ToList();

I'm receiving the following exception:

The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query. If you believe you have received this message in error, contact Customer Support Services for more information.

I was expecting Sql Server to be able to filter the results correctly.

Is it possible this query into multiple times to avoid this error?

Further technical details

EF Core version: 2.01
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 7
IDE: Visual Studio 2017 15.5
Sql Server: 2008 SP2

@ErikEJ
Copy link
Contributor

ErikEJ commented Feb 19, 2018

@xaviergxf
Copy link
Author

Hi @ErikEJ Can EF Core split the queries every 2100 parameters?

@ErikEJ
Copy link
Contributor

ErikEJ commented Feb 19, 2018

Not currently, but you could!

@smitpatel
Copy link
Contributor

@ErikEJ - It does not look like query has any closure variable which would be parametrized. It should be fully converted to SQL without any parameters.
@xaviergxf - What is the generated SQL? The error is coming from SqlServer itself. I believe there is nothing EF Core can do about it if the SQL we sent to server runs out of resources as we would have no way of knowing if a generated query could cause it.

@xaviergxf
Copy link
Author

@smitpatel unfortunately i couldn't trace the query with sql server profiler, aparently it is too big, too many parameters. What is funny is that i could do the same query logic by doing a select without any where's and filtering in memory my poco list.

@smitpatel
Copy link
Contributor

@xaviergxf - Can you share the linq query then? I am not able to see how the query in first post could generate a large SQL

@xaviergxf
Copy link
Author

@smitpatel i cannot post the linq query. The query does a left join as well, that is perhaps the reason why it cannot be split into multiple queries.

@ErikEJ
Copy link
Contributor

ErikEJ commented Feb 20, 2018

Where is the left join in the query in your original question?

@xaviergxf
Copy link
Author

It wasn't indeed. I removed the left join and i'm still receiving this error. The contains is done on a list of int.

@ralmsdeveloper
Copy link
Contributor

@xaviergxf.
Is the list of "ids" sequential?
If so, you can solve this temporarily like this:

db.Table.Where(p=> p.ID>=p.ids.First() && p.ID<=p.ids.Last()).ToList();

@ralmsdeveloper
Copy link
Contributor

ralmsdeveloper commented Feb 20, 2018

@smitpatel & @ErikEJ
For a better understanding of what is happening:
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/in-transact-sql#remarks

updated the link to specific section

@smitpatel
Copy link
Contributor

Work-around, split the ids list manually on client side. Since documentation does not provide a upper bound of values which can be included, it could be difficult to solve this in EF Core. (i.e. if the upper bound is based on size? int/string/guid could have different sizes). Even if EF Core breaks query in multiple parts based on some number and combine results on client, it would work only simple cases. If the query is being composed further then it could cause a huge client evaluation. And the logic of splitting can get really complex.

The SQL work-around to use temporary table could be useful in this scenario but we cannot apply that for all cases.

@xaviergxf
Copy link
Author

@ralmsdeveloper unfortunately the ids are not sequential... i have managed to solve this with an workaround. @smitpatel i agree with you that this should be handled somehow on sql server.

@ajcvickers
Copy link
Contributor

Triage: Agreed that this is a SQL Server issue. Assigning to @divega to suggest some other ways to get the same data back with different kinds of queries.

@divega
Copy link
Contributor

divega commented Feb 23, 2018

Here are a couple of ideas for workarounds:

@divega divega removed this from the 2.1.0 milestone Feb 23, 2018
@divega
Copy link
Contributor

divega commented Feb 23, 2018

Clearing up milestone in case we want to do something more with this issue before we close it.

@ajcvickers
Copy link
Contributor

Closing as nothing to do on the EF side. If any of Diego's ideas are to be productized, that would happen as part of TVP support.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
@GertArnold
Copy link

EF 8.0 has native support for this now: #13617.

For lower version, see this Stack Overflow question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants