Answer the question
In order to leave comments, you need to log in
How to do a prefix lookup in PostgeSQL so that an index is used?
I need to search by prefix, by exact match.
The result can be obtained by such queries.SELECT * FROM regions WHERE name ILIKE 'екат%';
SELECT * FROM regions WHERE LOWER(name) LIKE 'екат%';
SELECT * FROM geobase_region WHERE fts @@ to_tsquery('simple', 'заречный:*');
Answer the question
In order to leave comments, you need to log in
My index works in this case:
CREATE INDEX addr_lower_idx
ON public.addr
USING btree
(lower(factaddr) COLLATE pg_catalog."default" text_pattern_ops);
explain analyze
select * from addr
where lower(factaddr) like lower('Моск%');
Bitmap Heap Scan on addr (cost=22.40..490.53 rows=194 width=150) (actual time=0.110..0.110 rows=0 loops=1)
Filter: (lower(factaddr) ~~ 'моск%'::text)
-> Bitmap Index Scan on addr_lower_idx (cost=0.00..22.36 rows=194 width=0) (actual time=0.108..0.108 rows=0 loops=1)
Index Cond: ((lower(factaddr) ~>=~ 'моск'::text) AND (lower(factaddr) ~<~ 'мосл'::text))
Planning time: 0.174 ms
Execution time: 0.173 ms
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question