Repository navigation
Description
Most databases can take advantage of indexes (and in some cases even auto-generate them) when queries perform equality comparisons (instead of inequality).
In some places EFCore already tries to lean towards equality
efcore/src/EFCore.Relational/Query/SqlExpressionFactory.cs
Lines 657 to 666 in 20edb63
| // use equality where possible | |
| // !(a == true) -> a == false | |
| // !(a == false) -> a == true | |
| SqlBinaryExpression { OperatorType: ExpressionType.Equal, Right: SqlConstantExpression { Value: bool } } binary | |
| => Equal(binary.Left, Not(binary.Right)), | |
| // !(true == a) -> false == a | |
| // !(false == a) -> true == a | |
| SqlBinaryExpression { OperatorType: ExpressionType.Equal, Left: SqlConstantExpression { Value: bool } } binary | |
| => Equal(Not(binary.Left), binary.Right), |
In other places it disregards this and in fact can even replace equality comparisons with inequalities
efcore/src/EFCore.Relational/Query/SqlNullabilityProcessor.cs
Lines 1748 to 1757 in 20edb63
| // a == b <=> !a == !b -> a == b | |
| // !a == b <=> a == !b -> a != b | |
| // a != b <=> !a != !b -> a != b | |
| // !a != b <=> a != !b -> a == b | |
| nullable = false; | |
| return sqlBinaryExpression.OperatorType == ExpressionType.Equal ^ leftNegated == rightNegated | |
| ? _sqlExpressionFactory.NotEqual(left, right) | |
| : _sqlExpressionFactory.Equal(left, right); |
efcore/src/EFCore.Relational/Query/SqlNullabilityProcessor.cs
Lines 1803 to 1804 in 20edb63
| // a == !b and !a == b in SQL evaluate the same as a != b | |
| body = _sqlExpressionFactory.NotEqual(left, right); |
It would be better to avoid converting equalities into inequalities and, when possible, convert inequalities into equalities.
Activity
ranma42 commentedon Jul 12, 2024
On Sqlite
inequality has a much shorter plan:
but it performs a full table scan of the whole table for each row.
Equal-to-not has a more complex plan:
which automatically creates a temporary index for column
aand takes advantage of the index.ranma42 commentedon Jul 12, 2024
IIUC similar results also hold true in postgres:
ranma42 commentedon Jul 12, 2024
SqlServer 2022:
ranma42 commentedon Jul 12, 2024
An example session showing the performance difference on Sqlite:
TL;DR: about 3 times faster for a 50/50 distribution of true/false; much faster for 5/5/90 false/true/null
ranma42 commentedon Jul 12, 2024
A similar experiment on SqlServer 2022:
TL;DR: SqlServer saves about 15% of the CPU time when filtering with
=and it apparently can run it in parallel (10x faster wall-time).ErikEJ commentedon Jul 12, 2024
I see the same perf pattern on my informal test based on the script above!
ranma42 commentedon Jul 12, 2024
Ah, I mentioned it in the comment #34166 (comment) but maybe it should also be repeated here: these benchmarks are completely synthetic and do not represent a real-world workload.
They are meant to demonstrate that
a = NOT(b)is usually optimized better than<>.One might expect these optimizations to be automatically handled by strictly-typed DB engines, but apparently that is not the case.