Skip to content

Instantly share code, notes, and snippets.

View bharath-kotha's full-sized avatar

Bharath Kotha bharath-kotha

View GitHub Profile
@bharath-kotha
bharath-kotha / query_plan_2
Created August 7, 2022 06:39
How SELECT statement can cause disk write IO - Query Plan 2
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=1038449.15..1038460.82 rows=100 width=206) (actual time=23225.822..26199.674 rows=100 loops=1)
Buffers: shared hit=9749 read=201159, temp read=182633 written=195896
-> Gather Merge (cost=1038449.15..2983029.28 rows=16666666 width=206) (actual time=23225.820..26199.659 rows=100 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=9749 read=201159, temp read=182633 written=195896
-> Sort (cost=1037449.13..1058282.46 rows=8333333 width=206) (actual time=23219.185..23219.219 rows=75 loops=3)
Sort Key: book.title, book.id
@bharath-kotha
bharath-kotha / query_plan_1
Created August 7, 2022 06:18
How SELECT statement can cause disk write IO - Query Plan 1
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.87..51.28 rows=100 width=206) (actual time=0.023..24.739 rows=100 loops=1)
Buffers: shared hit=255 read=149
-> Nested Loop (cost=0.87..10080882.37 rows=20000000 width=206) (actual time=0.023..24.714 rows=100 loops=1)
Buffers: shared hit=255 read=149
-> Index Scan using book_pkey on book (cost=0.44..666418.44 rows=20000000 width=30) (actual time=0.012..0.069 rows=100 loops=1)
Buffers: shared hit=4
-> Index Scan using author_pkey on author (cost=0.43..0.47 rows=1 width=168) (actual time=0.245..0.245 rows=1 loops=100)
Index Cond: (id = book.author_id)
@bharath-kotha
bharath-kotha / generate_data.py
Created August 7, 2022 04:20
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')
@bharath-kotha
bharath-kotha / create_tables.sql
Last active August 7, 2022 04:18
How SELECT statement can cause disk write IO - create tables
CREATE TABLE author (
id bigserial primary key,
name varchar(64) not null, country varchar(64)
);
CREATE TABLE book (
id bigserial primary key,
title varchar(128) not null,
author_id bigint NOT NULL,
CONSTRAINT fk_books_author_id FOREIGN KEY (author_id) REFERENCES author(id)
@bharath-kotha
bharath-kotha / tasks.py
Last active June 13, 2021 16:23
celer_mutable_dictionary_issue
from celery import Celery
from datetime import datetime
app = Celery('tasks')
# Setting the task_always_eager to to true for demonstrating the issue
# You don't want to set it to true in production environments
app.conf.task_always_eager = True
def convert_to_iso(timestamp):
''' Convert Unix timestamp to an ISO string