-
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
Query: simplify comparisons which are result of null semantics in predicates #16221
Comments
What is the issue here? 😕 |
This is trivial, the first instruction overrides the second. |
Description is nullable column. |
Yes, it is, but I believe we should approach it differently, if you were to write this manually, it certainly would not write like that. |
So we have another problem. var @events = db
.Events
.Where(p => string.IsNullOrEmpty(p.Description))
.ToArray(); SELECT [e].[Id], [e].[DateOfEvent], [e].[Description], [e].[Key], [e].[Place], [e].[Price]
FROM [Events] AS [e]
WHERE [e].[Description] IS NULL OR (([e].[Description] = N'') AND [e].[Description] IS NOT NULL) |
If you do not add that term then |
@smitpatel, you guys did a great job rewriting the query pipeline, I wanted to do this provocation because many will question this. This is more process on the server, and if it is not an index field, this doubles the processing value. Thanks for the answers. |
@maumar to de-dupe |
turns out we didn't have a tracking issue for this improvement (thought I filed one) - will use this issue to track instead |
May I suggest the following translation for sql server? This has the added benefit that you can generate cleaner sql for complicated clauses. where (t.[a] = @a or (t.[a] is null and and @a is null))
and (t.[b] = @b or (t.[b] is null and @b is null))
and (t.[c] = @c or (t.[c] is null and @c is null)) You can generate the much simpler
These two statements both get the same exact same plan, which when you inspect the xml will show the following. <ScalarOperator ScalarString="[db].[dbo].[t].[a] = [@a]
AND [db].[dbo].[t].[b] = [@b]
AND [db].[dbo].[t].[c] = [@c]">
<Logical Operation="AND">
<ScalarOperator>
<Compare CompareOp="IS">
... https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=388214ea04487d42dcab3ff8e25b3245 Now, this is admittedly a query very few would actually write, but it does generate the exact same plan (That is safe) and is less verbose and ugly and is the proper solution to coalesce issues that few want to deal with. |
this has been fixed in 3.1, closing as dupe of #17543 |
I would like to know if there is any particular reason to produce this SQL statement, or it is something that happened when the Query Pipeline was rewritten.
cc/ @smitpatel @maumar
Repro
The text was updated successfully, but these errors were encountered: