Wednesday, 11 September 2013

PostgreSQL NULLable foreign key index optimization

PostgreSQL NULLable foreign key index optimization

PostgreSQL 9.2. Two variations of records can be stored in the following
table. alarmConfigurations entries where ac_p_id is NOT NULL can exist
more than once, but those where ac_p_id is NULL must only exist once
(hence the ac_unique_row_no_port constraint).
As you can see, regular indexes are created on all other foreign keys,
these get scanned a lot. But for ac_p_id...
Is there any particular advantage to creating the non-unique constraint
ac_index_p_id with IS NOT NULL (as opposed to not specifying, like the
others)?
In fact, if I only create ac_unique_row_no_port, will that also be used
for scans where ac_p_id is NOT NULL?
Thanks in advance.
CREATE TABLE alarmConfigurations (
ac_id SERIAL PRIMARY KEY,
ac_ad_code TEXT NOT NULL,
ac_ad_et_code TEXT NOT NULL,
ac_e_id INTEGER NOT NULL,
ac_as_code TEXT,
ac_p_id INTEGER,
ac_details HSTORE, -- extra configuration
CONSTRAINT ac_ad_fkey FOREIGN KEY (ac_ad_code, ac_ad_et_code)
REFERENCES alarmDefinitions (ad_code, ad_et_code)
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT ac_as_code_fkey FOREIGN KEY (ac_as_code)
REFERENCES alarmSeverities (as_code)
ON UPDATE CASCADE ON DELETE RESTRICT,
CONSTRAINT ac_e_id_fkey FOREIGN KEY (ac_e_id)
REFERENCES entities (e_id) ON DELETE CASCADE,
CONSTRAINT ac_p_id_fkey FOREIGN KEY (ac_p_id)
REFERENCES ports (p_id) ON DELETE CASCADE
);
CREATE INDEX ac_index_e_id ON alarmConfigurations(ac_e_id);
CREATE INDEX ac_index_ad_code ON alarmConfigurations(ac_ad_code);
CREATE INDEX ac_index_ad_et_code ON alarmConfigurations(ac_ad_et_code);
CREATE INDEX ac_index_p_id ON alarmConfigurations(ac_p_id)
WHERE ac_p_id IS NOT NULL;
CREATE UNIQUE INDEX ac_unique_row_no_port
ON alarmConfigurations(ac_ad_code, ac_ad_et_code, ac_e_id)
WHERE ac_p_id IS NULL;

No comments:

Post a Comment