cht電腦資訊SQL DB
adm Find login register

postgresql 的 SELECT LIKE bug

eliu
1 postgresql 的 SELECT LIKE bug
Promote 0 Bookmark 02007-12-19quote  

理論上 SELECT LIKE 'xxxx%' 應該可以用 INDEX search,結果沒有,不知道為什麼?

難道與 PRIMARY KEY 有關?還是 bug ? 

 

db =# explain SELECT seq FROM tab WHERE str like 'edt%';
                        QUERY PLAN                        
-----------------------------------------------------------
 Seq Scan on tab  (cost=0.00..6465.55 rows=1 width=4)
   Filter: (str ~~ 'edt%'::text)
(2 rows)

db=# explain SELECT seq FROM tab WHERE str = 'edta';
                                  QUERY PLAN                                 
------------------------------------------------------------------------------
 Index Scan using tab_pkey on tab  (cost=0.00..8.30 rows=1 width=4)
   Index Cond: (str = 'edta'::text)
(2 rows)

eliu
2
Promote 0 Bookmark 02007-12-19quote  
eliu
3
Promote 0 Bookmark 02007-12-21quote  

It's not bug. a LIKE 'some%' is different operation than a = str_const. if you don't use C locale, then you have to use varchar_pattern_ops. please try:

create index like_index on tab(str varchar_pattern_ops);

don't forget do ANALYZE tab before Regards Pavel Stehule

原來是要這樣。

edited: 4

cht電腦資訊SQL DB
adm Find login register
views:13704