postgresql - PostgresSQL full text search not using index after importing data with COPY -


i trying make postgresql use index prefix searching using full text search. works fine in general, if create index after importing data. maybe kind of intended behavior, not understand it.

first create index , import data using copy command:

create index account_fts_idx on account     using gin(to_tsvector('german', remote_id || ' ' || name || ' ' || street || ' ' || zip || ' ' || city )); copy account (id, remote_id, name, street, zip, city ...) '/path/account.csv' delimiter ',' csv; 

then run prefix (maybe that's important) search using following select statement:

explain analyze select a.id, a.remote_id, a.name, a.street, a.zip, a.city, al.latitude, al.longitude  account  left join account_location al on al.id = a.id  (to_tsvector('german', a.remote_id || ' ' || a.name || ' ' || a.street || ' ' || a.zip || ' ' || a.city)  @@ (to_tsquery('german', 'hambu:*'))) 

which results in poor performance, because index not being used:

hash left join  (cost=28.00..3389.97 rows=319 width=94) (actual time=1.685..1237.674 rows=1336 loops=1)   hash cond: (a.id = al.id)   ->  seq scan on account  (cost=0.00..3360.73 rows=319 width=78) (actual time=1.665..1236.589 rows=1336 loops=1)         filter: (to_tsvector('german'::regconfig, (((((((((remote_id)::text || ' '::text) || (name)::text) || ' '::text) || (street)::text) || ' '::text) || (zip)::text) || ' '::text) || (city)::text)) @@ '''hambu'':*'::tsquery)   ->  hash  (cost=18.00..18.00 rows=800 width=24) (actual time=0.001..0.001 rows=0 loops=1)         buckets: 1024  batches: 1  memory usage: 0kb         ->  seq scan on account_location al  (cost=0.00..18.00 rows=800 width=24) (actual time=0.001..0.001 rows=0 loops=1) total runtime: 1237.928 ms 

now comes strange part: if drop index , recreate using same create index command, same select query uses index , fast.

hash left join  (cost=61.92..1290.73 rows=1278 width=94) (actual time=0.561..1.918 rows=1336 loops=1)   hash cond: (a.id = al.id)   ->  bitmap heap scan on account  (cost=33.92..1257.78 rows=1278 width=78) (actual time=0.551..1.442 rows=1336 loops=1)         recheck cond: (to_tsvector('german'::regconfig, (((((((((remote_id)::text || ' '::text) || (name)::text) || ' '::text) || (street)::text) || ' '::text) || (zip)::text) || ' '::text) || (city)::text)) @@ '''hambu'':*'::tsquery)         ->  bitmap index scan on account_fts_idx  (cost=0.00..33.60 rows=1278 width=0) (actual time=0.490..0.490 rows=1336 loops=1)               index cond: (to_tsvector('german'::regconfig, (((((((((remote_id)::text || ' '::text) || (name)::text) || ' '::text) || (street)::text) || ' '::text) || (zip)::text) || ' '::text) || (city)::text)) @@ '''hambu'':*'::tsquery)   ->  hash  (cost=18.00..18.00 rows=800 width=24) (actual time=0.001..0.001 rows=0 loops=1)         buckets: 1024  batches: 1  memory usage: 0kb         ->  seq scan on account_location al  (cost=0.00..18.00 rows=800 width=24) (actual time=0.001..0.001 rows=0 loops=1) total runtime: 2.054 ms 

so why index has created after import?

and more important me: new rows (added via insert into) added index?

@denis pointed me right direction. looked @ vacuum, analyze command , found solution:

for tables gin indexes, vacuum (in form) completes pending index insertions, moving pending index entries appropriate places in main gin index structure. (postgresql documentation: vacuum)

after running vacuum account select query uses index excpected.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -