Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ sql-performance/
│ ├── index_usage.py # B-tree index impact
│ ├── join_vs_subquery.py # JOIN vs IN vs EXISTS
│ └── pagination.py # OFFSET vs keyset
├── queries/ # SQL files (loaded by benchmarks)
├── sql/
│ ├── init.sql # Schema only
│ └── seed.py # Parametrized seeder (small/medium/large)
Expand Down
4 changes: 0 additions & 4 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent
QUERIES_DIR = BASE_DIR / 'queries'

QUERY_1_NAME = 'SELECT *'
QUERY_2_NAME = 'SELECT id, name, email'


class Config:
Expand Down
21 changes: 10 additions & 11 deletions benchmarks/select_star.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure

from app.config import QUERIES_DIR, QUERY_1_NAME, QUERY_2_NAME
from benchmarks.base import (
BenchmarkBase,
BenchmarkNotApplicable,
Expand All @@ -20,6 +19,12 @@

DATA_POINTS = 10

QUERY_ALL_COLUMNS = "SELECT * FROM users LIMIT %s"
QUERY_THREE_COLUMNS = "SELECT id, name, email FROM users LIMIT %s"

LABEL_ALL_COLUMNS = "SELECT *"
LABEL_THREE_COLUMNS = "SELECT id, name, email"


@register
class SelectStarBenchmark(BenchmarkBase):
Expand All @@ -28,10 +33,6 @@ class SelectStarBenchmark(BenchmarkBase):
description = "Demonstrates why SELECT * is slow and selecting specific columns is fast"
required_tables = ["users"]

def _get_query(self, filename: str) -> str:
with open(QUERIES_DIR / filename) as f:
return f.read()

def _limits(self, total: int) -> list[int]:
"""LIMITs must stay inside the table: a LIMIT above the row count
returns the whole table every time and flattens the curve into noise."""
Expand All @@ -47,27 +48,25 @@ def setup(self, conn) -> None:

def run(self, conn) -> BenchmarkResult:
limits = self._limits(table_row_count(conn))
query_1 = self._get_query("query_1.sql")
query_2 = self._get_query("query_2.sql")

q1_times, q2_times = [], []
q1_rows, q2_rows = [], []

with conn.cursor() as cursor:
for limit in limits:
rows_1, t1 = measure(cursor, query_1, (limit,))
rows_2, t2 = measure(cursor, query_2, (limit,))
rows_1, t1 = measure(cursor, QUERY_ALL_COLUMNS, (limit,))
rows_2, t2 = measure(cursor, QUERY_THREE_COLUMNS, (limit,))
q1_times.append(t1)
q2_times.append(t2)
q1_rows.append(rows_1)
q2_rows.append(rows_2)

q1 = QueryResult(
name=QUERY_1_NAME, query=query_1,
name=LABEL_ALL_COLUMNS, query=QUERY_ALL_COLUMNS,
times=q1_times, limits=limits, rows_fetched=q1_rows,
)
q2 = QueryResult(
name=QUERY_2_NAME, query=query_2,
name=LABEL_THREE_COLUMNS, query=QUERY_THREE_COLUMNS,
times=q2_times, limits=limits, rows_fetched=q2_rows,
)

Expand Down
1 change: 0 additions & 1 deletion queries/query_1.sql

This file was deleted.

1 change: 0 additions & 1 deletion queries/query_2.sql

This file was deleted.

2 changes: 1 addition & 1 deletion sql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CREATE TABLE IF NOT EXISTS users (
name VARCHAR(100),
surname VARCHAR(100),
email VARCHAR(100),
direction VARCHAR(200),
address VARCHAR(200),
city VARCHAR(100),
country VARCHAR(100),
postal_code VARCHAR(20),
Expand Down
2 changes: 1 addition & 1 deletion sql/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
%(cities)s::TEXT[] AS cities,
%(countries)s::TEXT[] AS countries
)
INSERT INTO users (name, surname, email, direction, city, country,
INSERT INTO users (name, surname, email, address, city, country,
postal_code, phone, age, bio)
SELECT
a.first_names[1 + floor(random() * array_length(a.first_names, 1))::INT],
Expand Down
8 changes: 1 addition & 7 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from app.config import BASE_DIR, QUERIES_DIR, Config
from app.config import BASE_DIR, Config


def test_config_defaults():
Expand Down Expand Up @@ -35,9 +35,3 @@ def test_config_port_cast(monkeypatch):
def test_base_dir():
assert BASE_DIR.exists()
assert (BASE_DIR / 'app').exists()


def test_queries_dir():
assert QUERIES_DIR.exists()
assert (QUERIES_DIR / 'query_1.sql').exists()
assert (QUERIES_DIR / 'query_2.sql').exists()
2 changes: 1 addition & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_seed_leaves_no_null_values_in_generated_columns(conn):
with conn.cursor() as cur:
cur.execute(
"SELECT count(*) FROM users "
"WHERE name IS NULL OR surname IS NULL "
"WHERE name IS NULL OR surname IS NULL OR address IS NULL "
"OR city IS NULL OR country IS NULL"
)
assert cur.fetchone()[0] == 0
Expand Down
Loading