Skip to content

Instantly share code, notes, and snippets.

@bharath-kotha
Created August 7, 2022 04:20
Show Gist options
  • Save bharath-kotha/80f1bc9a7e3b022103cab9fdba271a4e to your computer and use it in GitHub Desktop.
Save bharath-kotha/80f1bc9a7e3b022103cab9fdba271a4e to your computer and use it in GitHub Desktop.
How SELECT statement can cause disk write IO - generate data
import random
NUM_AUTHORS = 10000000
NUM_BOOKS = 20000000
# Any author can wite any book
author_sql = """INSERT INTO author (id, name) values ({i}, 'author_{i}');\n"""
book_sql = """INSERT INTO book (title, author_id) values ('title_{i}', {author_id});\n"""
data_file = open('data.sql', 'w')
data_file.write('BEGIN;\n')
for i in range(NUM_AUTHORS):
data_file.write(author_sql.format(i=i+1))
data_file.write('COMMIT;\n')
data_file.write('BEGIN;\n')
for i in range(NUM_BOOKS):
author_id = random.randint(1, NUM_AUTHORS)
data_file.write(book_sql.format(i=i, author_id=author_id))
data_file.write('COMMIT;\n')
data_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment