I have a table with many columns and several million rows like
CREATE TABLE foo (
id integer,
thing1 text,
thing2 text,
...
stuff text);
How can I manage the relevance of dictionary of unique values of stuff
column that originally populates like this:
INSERT INTO stuff_dict SELECT DISTINCT stuff from foo;
Should I manually synchronize (check if new stuff
value already in stuff_dict
before every insert/update) or use triggers for each insert/update/delete from foo
table. In latter case, what is the best design for such a trigger(s)?
UPDATE: view does not suit here, because the SELECT * FROM stuff_dict
should run as fast as possible (even CREATE INDEX ON foo(stuff)
does not help much when foo has tens of millions of records).
A materialized view seems to be the simplest option for a large table.
In the trigger function just refresh the view. You can use
concurrently
option (see the pozs's comment below).While the solution with a materialized view is straightforward it may be suboptimal when the table
foo
is modified frequently. In this case use a table for the dictionary. An index will be helpful.The trigger function is more complicated and should be fired for each row after insert/update/delete: