SQLite Forum

SELECT optimization for constant expression
Login
there was recently (the last few days) another thread about query plan in conjunction with LIKE.

If a pattern does not start with a constant (or literal?) then SCAN needs to be done instead of being able to use the index.

Maybe a splitting into an ugly looking sequence could help to make the query plan look as expected.

Example of what I mean

~~~
...
case substr(?1 COLLATE NOCASE,1,1) 
  when 'A' then name like 'A'||substr(?1,2)
  when 'B' then name like 'B'||substr(?1,2)
  when 'C' then name like 'C'||substr(?1,2)
...
  when 'Y' then name like 'Y'||substr(?1,2)
  when 'Z' then name like 'Z'||substr(?1,2)
else 
  name like ?1  -- uses SCAN query plan
end
~~~

EDIT: copy-paste-error correction