From d612e8e7fbd1dcb3d426bb71028ee505eadd09db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20S=C3=A1nchez?= <100514206+jonaas-dev@users.noreply.github.com> Date: Sun, 6 Sep 2026 20:17:35 +0200 Subject: [PATCH] refactor: rename direction to address, inline the last SQL files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three leftovers from when the project had a single benchmark, all of them things a reader notices before they read any logic. - `direction VARCHAR(200)` was a literal translation of "direccion". In English `direction` means heading, not domicile. The schema is the first file most people open, so the column is now `address`. - `queries/query_1.sql` and `query_2.sql` were generically named and loaded only by select_star, while the other three benchmarks declared their SQL inline. The inconsistency had no reason behind it — for one-line queries the file indirection buys nothing, so all four now read the same way and the directory is gone along with QUERIES_DIR and the _get_query helper. - `QUERY_1_NAME` and `QUERY_2_NAME` put one benchmark's display labels in global config. They now live in select_star.py next to the queries they name, as LABEL_ALL_COLUMNS and LABEL_THREE_COLUMNS. No behaviour change. Verified end to end against a fresh compose stack: all four benchmarks return 200 with a chart, and the seeded schema reports the renamed column. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_014abw4B6YUf54giaEyPQpbo --- README.md | 1 - app/config.py | 4 ---- benchmarks/select_star.py | 21 ++++++++++----------- queries/query_1.sql | 1 - queries/query_2.sql | 1 - sql/init.sql | 2 +- sql/seed.py | 2 +- tests/test_config.py | 8 +------- tests/test_integration.py | 2 +- 9 files changed, 14 insertions(+), 28 deletions(-) delete mode 100644 queries/query_1.sql delete mode 100644 queries/query_2.sql diff --git a/README.md b/README.md index 610c5e6..83eea35 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/app/config.py b/app/config.py index b846a47..e77ddc1 100644 --- a/app/config.py +++ b/app/config.py @@ -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: diff --git a/benchmarks/select_star.py b/benchmarks/select_star.py index 484cd30..134f509 100644 --- a/benchmarks/select_star.py +++ b/benchmarks/select_star.py @@ -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, @@ -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): @@ -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.""" @@ -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, ) diff --git a/queries/query_1.sql b/queries/query_1.sql deleted file mode 100644 index 997d0b9..0000000 --- a/queries/query_1.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT * FROM users LIMIT %s; \ No newline at end of file diff --git a/queries/query_2.sql b/queries/query_2.sql deleted file mode 100644 index 5ce503e..0000000 --- a/queries/query_2.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT id, name, email FROM users LIMIT %s; diff --git a/sql/init.sql b/sql/init.sql index 7c4491a..044724c 100644 --- a/sql/init.sql +++ b/sql/init.sql @@ -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), diff --git a/sql/seed.py b/sql/seed.py index c77e3b0..69f2015 100644 --- a/sql/seed.py +++ b/sql/seed.py @@ -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], diff --git a/tests/test_config.py b/tests/test_config.py index d8c505e..d0ec790 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,4 +1,4 @@ -from app.config import BASE_DIR, QUERIES_DIR, Config +from app.config import BASE_DIR, Config def test_config_defaults(): @@ -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() diff --git a/tests/test_integration.py b/tests/test_integration.py index 274fee6..9b05525 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -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