sql - PostgreSQL text range scan -
i have written query aim 10 results including current one, padding 9 entries on either side alphabetical list can sorted reciever. query using, issue not result, but because neither of queries using index.
(   select     uid,     title       books     lower(title) < lower('frankenstein')   order title desc   limit 9 ) union (   select     uid,     title       books     lower(title) >= lower('frankenstein')   order title   limit 10 ) order title;   the index trying utilize simple btree, no text_pattern_ops etc below:
create index books_title_idx on books using btree (lower(title));
if run explain on first part of unioin, in spite of limit , order, performs full table scan
explain analyze  select   uid,   title   books lower(title) < lower('frankenstein') order title desc limit 9   limit  (cost=69.04..69.06 rows=9 width=152) (actual time=6.276..6.292 rows=9 loops=1)   ->  sort  (cost=69.04..69.67 rows=251 width=152) (actual time=6.273..6.277 rows=9 loops=1)         sort key: ((title))         sort method: top-n heapsort  memory: 25kb         ->  seq scan on books  (cost=0.00..63.80 rows=251 width=152) (actual time=0.056..5.227 rows=267 loops=1)               filter: (lower((title)) < 'frankenstein'::text)               rows removed filter: 486 total runtime: 6.359 ms
when equality check on same query - the index used
explain analyze select   uid,   title   books lower(title) = lower('frankenstein') order title desc   sort  (cost=17.04..17.05 rows=4 width=152) (actual time=0.054..0.054 rows=0 loops=1)   sort key: ((title))   sort method: quicksort  memory: 25kb   ->  bitmap heap scan on books  (cost=4.31..17.00 rows=4 width=152) (actual time=0.041..0.041 rows=0 loops=1)         recheck cond: (lower((title)) = 'frankenstein'::text)         ->  bitmap index scan on books_title_idx  (cost=0.00..4.31 rows=4 width=0) (actual time=0.036..0.036 rows=0 loops=1)               index cond: (lower((title)) = 'frankenstein'::text) total runtime: 0.129 ms
and same applies when between query
explain analyze select   uid,   title   books lower(title) > lower('frankenstein') , lower(title) < lower('gulliver''s travels') order title   sort  (cost=17.08..17.09 rows=4 width=152) (actual time=0.511..0.529 rows=25 loops=1)   sort key: (title)   sort method: quicksort  memory: 27kb   ->  bitmap heap scan on books  (cost=4.33..17.04 rows=4 width=152) (actual time=0.118..0.213 rows=25 loops=1)         recheck cond: ((lower(title) > 'frankenstein'::text) , (lower(title) < 'gulliver''s travels'::text))         ->  bitmap index scan on books_title_idx  (cost=0.00..4.33 rows=4 width=0) (actual time=0.087..0.087 rows=25 loops=1)               index cond: ((lower(title) > 'frankenstein'::text) , (lower(title) < 'gulliver''s travels'::text)) total runtime: 0.621 ms
what looking here not between search because beginning , end unknown. postgresql limitation or there other manipulating cost of table scan silly can use convince query planner use index?
i using postgresql 9.3
use:
order lower(title) desc  or
order lower(title)  to match functional index, can utilized.
 order by irrelevant selection of rows in other 2 queries. that's why index can used in cases. 
Comments
Post a Comment