PostgreSQL & PHP Tutorials - Adding Full Text Indexing to our table
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;
Avg Rating: 4
Vote Count: 13
