Skip to content

Instantly share code, notes, and snippets.

@ketanbhatt
Last active May 14, 2017 09:39
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 ketanbhatt/9bfe77cfd2470bf2541f4b987dc482bf to your computer and use it in GitHub Desktop.
Save ketanbhatt/9bfe77cfd2470bf2541f4b987dc482bf to your computer and use it in GitHub Desktop.
Avoid Joins: Creating Tables and Inserting Data
-- User Table
CREATE TABLE user (
id serial PRIMARY KEY,
account_id int not NULL,
name varchar(10)
);
-- Purchase Table
CREATE TABLE purchase (
id SERIAL PRIMARY KEY,
data text not NULL,
user_id int not NULL
);
-- Populate our tables (might not be the most efficient way)
INSERT INTO USER (account_id)
SELECT generate_series(1,50000000) AS random_id;
INSERT INTO purchase (data, user_id)
SELECT 'Cereals' AS data,
generate_series(1,50000000);
INSERT INTO purchase (data, user_id)
SELECT 'Milk' AS data,
generate_series(1,50000000);
-- To mock a more real world example, we will add necessary indices
CREATE index ON "user" ("account_id");
CREATE index ON "purchase" ("user_id");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment