PostgreSQL & PHP Tutorials - Adding Full Text Indexing to our table

PostgreSQL »  Introduction to Full Text Indexing

Posted By Chris Smith Posted on 26 Mar 2006, 07:00 AM
Viewing page 2 of 3
« | Back | Next | »

If our table looks like our news table:

CREATE TABLE newsitem (
    newsid INT PRIMARY KEY,
    newstitle VARCHAR(255),
    newscontent TEXT,
    authorid INT,
    newsdate TIMESTAMP
);

First we need to add a new column to store the full text index:

ALTER TABLE newsitem ADD COLUMN newscontent_fti tsvector;

If we want to index just the content field, we run this command:

UPDATE newsitem SET newscontent_fti=to_tsvector('default', newscontent);

When that finishes, we can index it:

CREATE INDEX newscontent_fti_idx ON newsitem USING gist(newscontent_fti);

When that's finished, we need to update postgresql statistics:

ANALYZE;

This helps postgresql decide when to use an index and when it's better to ignore it, so it's a very important step to remember.

This will go through each table and update statistics for it, if you want to do just a specific table you can do:

ANALYZE tablename;
Viewing page 2 of 3
« | Back | Next | »



Avg Rating: 4
Vote Count: 14


              

  New Reply


Very nice intro guide. Good work. Rodrigo 12 Apr 2006 Reply


Want to post a comment? Fill in the details below.

Your Name  : 
Your Email  : 
Your Website  : 
Spam Check! Please answer this question  : 8 + 7 =
Comment