question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query: further optimize translation of StartsWith

See original GitHub issue

Currently 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:24 (11 by maintainers)

github_iconTop GitHub Comments

5reactions
darcythomascommented, Oct 5, 2018

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

2reactions
glen-84commented, May 15, 2022

It’s not even using LIKE for MySQL:

 WHERE (@__val_0 = '') OR (LEFT(`u`.`username`, CHAR_LENGTH(@__val_0)) = @__val_0))

Why is it doing this? I don’t think that this can use an index. This is such a simple case (startsWith).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found