16

Ive been trying to get sqlite to use an index with a like to no avail. Ive tried collate nocase and still no luck. Anyone have any ideas on how to get sqlite to do a like hitting an index. Thanks in advance

DROP TABLE IF EXISTS "test";
DROP TABLE IF EXISTS "test2";
DROP TABLE IF EXISTS "test3";
create table test(name TEXT COLLATE NOCASE);
create table test2(name TEXT);
create table test3(name TEXT);
create index idx_test_name on test(name);
create index idx_test2_name on test2(name);
create index idx_test3_name on test3(name COLLATE NOCASE);
insert into test(name) values('dan');
insert into test2(name) values('dan');
insert into test3(name) values('dan');
--explain query plan select * from test where name like 'test%' 
-- explain query plan select * from test2 where name like 'test%' 
-- explain query plan select * from test3 where name like 'test%' 
1
  • 1
    Having an index doesn't ensure it will be used.
    – OMG Ponies
    Commented Oct 21, 2012 at 4:03

5 Answers 5

23

Quote from sqlite mail list (http://www.mail-archive.com/[email protected]/msg27760.html)

LIKE is case-insensitive by default. To have it use your index, you need to either make the index case-insensitive:

CREATE INDEX test_name ON test (name COLLATE NOCASE);

or make LIKE case-sensitive:

PRAGMA case_sensitive_like = 1;

2
  • Hi there,thanks for everyones replies, however after testing the collate nocase on the index, the index is still not being used. Ive updated my example code with a test3 to test the scenario if anyone wants to give it a try. Any other ideas?
    – aboutme
    Commented Oct 21, 2012 at 5:59
  • 1
    @aboutme, it looks as you're not inserting much data. The index will be of very low quality and the engine will probably disregard it. Please see separate post where like works on IC index.
    – Teson
    Commented Sep 14, 2022 at 11:25
4

In SQLite 3.6.23.1, the index on test is used:

> explain query plan select * from test where name like 'test%';
TABLE test WITH INDEX idx_test_name

> explain query plan select * from test2 where name like 'test%';
TABLE test2

> explain query plan select * from test3 where name like 'test%';
TABLE test3

With a development version of SQLite 3.7.15, both test's and test3's indexes are used (the index on test2 is used for scanning, not searching):

> explain query plan select * from test where name like 'test%';
SEARCH TABLE test USING COVERING INDEX idx_test_name (name>? AND name<?) (~31250 rows)

> explain query plan select * from test2 where name like 'test%';
SCAN TABLE test2 USING COVERING INDEX idx_test2_name (~500000 rows)

> explain query plan select * from test3 where name like 'test%';
SEARCH TABLE test3 USING COVERING INDEX idx_test3_name (name>? AND name<?) (~31250 rows)

So the answer is to update SQLite.

0
2

From the docs:

Terms that are composed of the LIKE or GLOB operator can sometimes be used to constrain indices. There are many conditions on this use:

  • The left-hand side of the LIKE or GLOB operator must be the name of an indexed column with TEXT affinity.
  • The right-hand side of the LIKE or GLOB must be either a string literal or a parameter bound to a string literal that does not begin with a wildcard character.
  • The ESCAPE clause cannot appear on the LIKE operator.
  • The built-in functions used to implement LIKE and GLOB must not have been overloaded using the sqlite3_create_function() API.
  • For the GLOB operator, the column must be indexed using the built-in BINARY collating sequence.
  • For the LIKE operator, if case_sensitive_like mode is enabled then the column must indexed using BINARY collating sequence, or if case_sensitive_like mode is disabled then the column must indexed using built-in NOCASE collating sequence.
0

I also have the same issue with org.xerial/sqlite-jdbc "3.25.2". A simple TEXT based index is not getting used with LIKE operator. But, if I put =, the index gets used. My table schema :

 create table if not exists test (ID INTEGER PRIMARY KEY, VALUE TEXT NOT NULL)
 create index test_idx on test(value)
 insert into test (ID,  VALUE) values (1, 'gold bangles')
 insert into test (ID,  VALUE) values (2, 'gold bandana')
 insert into test (ID,  VALUE) values (2, 'gold bracelet')

 explain query plan select * from test where value='gold bandana'

 Output:
 0|0|0|SEARCH TABLE test USING COVERING INDEX test_idx(VALUE=?)

 explain query plan select * from test where value like 'gold%'

 Output:
 0|0|0|SCAN TABLE test
0

ilike indeed works, based on sqlite database included in dbeaver (chinook.db),

> sqlite3 chinook.db
> create index idx_artist_name_ic on Artist (Name collate nocase);
> .eqp on
> select Name from Artist where Name like = 'd%';
`--SEARCH TABLE Artist USING COVERING INDEX idx_artist_name_ic (Name>? AND Name<?)

Which indicates that sqlite is clever enough to convert an icase like to regular AND. Unicode-characters could be problematic..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.