Query: further optimize translation of StartsWith
See original GitHub issueCurrently we translate foobar.StartsWith(foo)
into:
(foobar like foo +'%' and left(foobar, len(foo)) = foo) or foo = '' -- last term is removed if foo is non-null constant
however @rmacfadyen pointed out that for some scenarios the last term actually makes the query non-sargable.
If the term is removed completely, we return (arguably) incorrect data for case: null.StartsWith(“”).
We could however use the following translation:
(foobar like foo +'%' and left(foobar, len(foo)) = foo) or (foo = '' and foobar is null)
(and if we know that foobar can’t be null we could drop the term as well.
We need to do some more investigation on which scenarios are getting better with this translation (i.e. if its worth complicating the sql)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:24 (11 by maintainers)
Top Results From Across the Web
StartsWith() doesn't translate to Like('abc%') in LINQ
The idea is with LIKE condition to allow query optimizer to use the index, and then do the slow filtering with the second...
Read more >Efficient Querying - EF Core
Performance guide for efficient querying using Entity Framework Core. ... In some cases, more optimized SQL exists for your query, which EF ...
Read more >startsWith: Does String Start or End With Another String? - rdrr.io
The code has an optimized branch for the most common usage in which prefix or suffix is of length one, and is further...
Read more >Performance Best Practice for Efficient Queries
Caching data to improve performance Improving Slow OR and JOIN Queries Overview This guide is written by the ServiceNow Technical Support Performance.
Read more >%STARTSWITH | InterSystems IRIS Data Platform 2023.2
STARTSWITH - Matches a value with a substring specifying initial ... You can use %STARTSWITH in any predicate condition of an InterSystems SQL...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
My observation/ 2 cents:
For me:
[Foo] LIKE @foo + '%' AND (LEFT([Foo], LEN(@foo)) = @foo)) OR (@foo = '')
is 100X slower than[Foo] LIKE @foo + '%'
This seems to be because the the query needs to perform a index scan rather than an index seek on
IX_TableName_Foo
It’s not even using
LIKE
for MySQL:Why is it doing this? I don’t think that this can use an index. This is such a simple case (
startsWith
).