-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
For "Contains" queries of numeric values, just use the value in an "IN" statement instead of casting, converting, or using JSON. #33423
Comments
@RobertPepkaSEL you're discussing various issues above, which it's worth untangling.
|
Thanks for responding and consideration!
DBAs I work with help us analyze the SQL that entity framework generates, either during development, or after development when evaluating SQL Server's performance when there's a measured impact to a system. Output like this makes these much more challenging to read and understand. Obviously this is a simple example, but in the cases of very large lists, multiple lists, etc, the data casting overwhelms actual the query, cueing eye-rolling, etc.
That makes a ton of sense. Being able to have a cached query plan is really important for all kinds of queries. It's one of the chief complaints we get when evaluating the generated SQL queries, and a great reason to upgrade to EF 8.0 .
My initial ask was because that goes back to reviewing, analyzing, and approving the query. From a perspective of a DBA, the table form would look more idiomatic and understandable. It would avoid a step that requires OPENJSON, meaning it could continue to operate on older SQL Server versions like 2012. I haven't tested it, but I would guess that using a table and inserts may be faster than parsing JSON to get integers. Additionally, it may also meet the same kind of needs discussed in #13617 . |
OK. I think you're possibly overstating the complexity that
SQL Server 2012 went out of support in 2022; we also have an option to force SQL Server back to the older constant-based translation in case that's needed (see docs).
Guessing is always a bad idea when it comes to performance - my own testing showed the opposite, with OPENJSON being faster; declaring a temporary table ( I'm going to go ahead and close this issue as everything seems to be working as designed, but please don't hesitate to post back if you have further questions or suggestions. |
Thank you for your feedback :) |
Given a simple entity, such as:
With a SQL Server Table declaration like:
and I want to find a set of these from a list:
Older Entity Framework Core generates a query along the lines of:
And as of EFCore 8, it generates:
Proposal:
If both the input type and the identifier types are numeric (int16, int32, int64), and the same type, just do an IN with the values directly. Don't convert, don't use OPENJSON conversion, just use the values. This will save time with conversion, string interpolation, and unnecessary inner queries. Ideally, EFCore would generate something like the following:
Alternatively, if a
WHERE <> IN (SELECT ...)
style solution is still desirable, use a table variable instead.The text was updated successfully, but these errors were encountered: