pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedDecrease time needed to CREATE INDEX and FOREIGN KEY on new table column which has all values NULL
3+ messages / 3 participants
[nested] [flat]
* Decrease time needed to CREATE INDEX and FOREIGN KEY on new table column which has all values NULL
@ 2019-01-22 16:29 Denisa Cirstescu <Denisa.Cirstescu@tangoe.com>
0 siblings, 2 replies; 3+ messages in thread
From: Denisa Cirstescu @ 2019-01-22 16:29 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Hello all,
I am trying to add a new column to a really big table and to define an INDEX and a FOREIGN KEY on that new column using the following instructions:
ALTER TABLE Employee ADD COLUMN DepartmentId INTEGER;
CREATE INDEX IDX_Employee_DepartmentId ON Employee(DepartmentId);
ALTER TABLE Employee ADD CONSTRAINT FK_Employee_Department FOREIGN KEY(DepartmentId) REFERENCES Department(DepartmentId);
The table is huge and it takes a lot of time to add the INDEX and the FOREIGN KEY although all values are NULL.
Considering that the new DepartmentId column is NULL for all rows at this point, is there a way to make the INDEX and FOREIGN KEY creation run faster?
See below information about the size of the table and also the size of its associated Primary Key and Indexes:
Employee 339 GB
Employee_PKEY 46 GB
IDX_Employee_JobId 46 GB
IDX_Employee_IsWFH 46 GB
Thank you,
Denisa Cîrstescu
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Decrease time needed to CREATE INDEX and FOREIGN KEY on new table column which has all values NULL
@ 2019-01-22 16:38 Ravi Krishna <srkrishna@fastmail.com>
parent: Denisa Cirstescu <Denisa.Cirstescu@tangoe.com>
1 sibling, 0 replies; 3+ messages in thread
From: Ravi Krishna @ 2019-01-22 16:38 UTC (permalink / raw)
To: Denisa Cirstescu <Denisa.Cirstescu@tangoe.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
> The table is huge and it takes a lot of time to add the INDEX and the FOREIGN KEY although all values are NULL.
> Considering that the new DepartmentId column is NULL for all rows at this point, is there a way to make the INDEX and FOREIGN KEY creation run faster?
In your script to create index add this:
set maintenance_work_mem = '1GB'
or any other figure you feel comfortable.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Decrease time needed to CREATE INDEX and FOREIGN KEY on new table column which has all values NULL
@ 2019-01-22 16:47 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Denisa Cirstescu <Denisa.Cirstescu@tangoe.com>
1 sibling, 0 replies; 3+ messages in thread
From: Alvaro Herrera @ 2019-01-22 16:47 UTC (permalink / raw)
To: Denisa Cirstescu <Denisa.Cirstescu@tangoe.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On 2019-Jan-22, Denisa Cirstescu wrote:
> I am trying to add a new column to a really big table and to define an INDEX and a FOREIGN KEY on that new column using the following instructions:
>
> ALTER TABLE Employee ADD COLUMN DepartmentId INTEGER;
> CREATE INDEX IDX_Employee_DepartmentId ON Employee(DepartmentId);
> ALTER TABLE Employee ADD CONSTRAINT FK_Employee_Department FOREIGN KEY(DepartmentId) REFERENCES Department(DepartmentId);
>
> The table is huge and it takes a lot of time to add the INDEX and the FOREIGN KEY although all values are NULL.
> Considering that the new DepartmentId column is NULL for all rows at this point, is there a way to make the INDEX and FOREIGN KEY creation run faster?
The typical advice is to run CREATE INDEX CONCURRENTLY instead of plain
CREATE INDEX. Also, use "ADD CONSTRAINT ... NOT VALID;" instead, and
later do another ALTER TABLE ... VALIDATE CONSTRAINT. That decreases
the impact considerably in production scenarios.
--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2019-01-22 16:47 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-01-22 16:29 Decrease time needed to CREATE INDEX and FOREIGN KEY on new table column which has all values NULL Denisa Cirstescu <Denisa.Cirstescu@tangoe.com>
2019-01-22 16:38 ` Ravi Krishna <srkrishna@fastmail.com>
2019-01-22 16:47 ` Alvaro Herrera <alvherre@2ndquadrant.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox