Re: Delete performance - Mailing list pgsql-admin

From Tom Lane
Subject Re: Delete performance
Date
Msg-id 22825.1140712500@sss.pgh.pa.us
Whole thread Raw
In response to Re: Delete performance  (Arnau <arnaulist@andromeiberica.com>)
Responses Re: Delete performance  (Arnau <arnaulist@andromeiberica.com>)
List pgsql-admin
Arnau <arnaulist@andromeiberica.com> writes:
>> The referencED column is forced to have an index.  The referencING
>> column is not.  The cases where you need an index on the latter are
>> precisely updates/deletes of the referencED column.

>    Lets put an example

>    CREATE TABLE departments
>    (
>      id   INT2
>           CONSTRAINT pk_dept_id PRIMARY KEY,
>      name VARCHAR(50)
>           CONSTRAINT nn_dept_name NOT NULL
>    );

>    CREATE TABLE users
>    (
>      id            INT8
>                    CONSTRAINT pk_users_id PRIMARY KEY,
>      name          VARCHAR(50)
>                    CONSTRAINT nn_users_name NOT NULL,
>      department_id INT2
>                    CONSTRAINT fk_users_deptid REFERENCES departments(id)
>                    CONSTRAINT nn_users_deptid NOT NULL
>    )

>    Do we should create the following index?

>    CREATE INDEX idx_users_deptid ON users(department_id)

Yes, if you are concerned about the performance of updates/deletes on
the departments table.  The reason the system doesn't make such an index
automatically is that there are common scenarios where you seldom or
never update the master table, and so the index wouldn't repay the cost
it creates for updates of the slave table.

            regards, tom lane

pgsql-admin by date:

Previous
From: Andy Shellam
Date:
Subject: Re: Deinstallation
Next
From: Arnau
Date:
Subject: Re: Delete performance