Last active
May 14, 2017 09:39
-
-
Save ketanbhatt/9bfe77cfd2470bf2541f4b987dc482bf to your computer and use it in GitHub Desktop.
Avoid Joins: Creating Tables and Inserting Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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