Using Postgres 9.5. The Postgres documentation says that the btree
index can be used for left-anchored "like" queries (i.e. "%" only at end). But my left-anchored "like" queries on a 1 billion row table won't use a btree index, and my queries are taking forever. What am I missing? Here's a small example that demonstrates the problem:
-- generate a table of 100K integers with random texts
CREATE TABLE test(num integer, string text);
INSERT INTO test(num, string) (SELECT generate_series(1, 100000), md5(random()::text));
CREATE INDEX test_string ON test USING btree (string); -- b-tree index
EXPLAIN SELECT * FROM test WHERE string LIKE 'asdf%'; -- will use seqscan
EXPLAIN SELECT * FROM test WHERE string = 'asdf'; -- will use index
SET enable_seqscan=off; -- tell query planner to avoid seqscan if possible
EXPLAIN SELECT * FROM test WHERE string LIKE 'asdf%'; -- will still use seqscan
ANALYZE test;
? I tested your commands above (with C collation), before an ANALYZE it used a seq scan, after the ANALYZE it used the index.ANALYZE
too. Turned out the issue was my locale settings.