Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
POSTGRESQL: INDEXES AND FOREIGN KEYS

Step 1:

CREATE TABLE a (
    a_id int PRIMARY KEY
);

Step 2:

CREATE TABLE b (
  b_id    int,
  a_id    int  REFERENCES a(a_id)
                ON UPDATE CASCADE
                ON DELETE CASCADE
);

Step 3:

INSERT INTO a
  SELECT  x
  FROM    generate_series(1, 5000000) AS x;

Step 4:

INSERT INTO b
  SELECT  x, x
  FROM    generate_series(1, 5000000) AS x;

Step 5:

explain analyze DELETE FROM a WHERE a_id = 10;

Step 6:

CREATE INDEX idx_b ON b (a_id);

Step 7:

explain analyze DELETE FROM a WHERE a_id = 11;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.