Skip to content

Instantly share code, notes, and snippets.

@manishkpr
Last active February 21, 2019 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manishkpr/d2138caf46435cf37e7713f155137162 to your computer and use it in GitHub Desktop.
Save manishkpr/d2138caf46435cf37e7713f155137162 to your computer and use it in GitHub Desktop.
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