From 0c4d3ab15d50fcf39b016d25ed5ef73b35039398 Mon Sep 17 00:00:00 2001 From: Steve Dogiakos Date: Mon, 9 Mar 2026 20:17:34 -0600 Subject: [PATCH] perf: add DB indexes and cap guest queries at 100 rows - Add idx_guests_id and idx_guests_email indexes in init_db() - Cap all SELECT queries on the guests table to LIMIT 100 to prevent unbounded memory growth as the guestbook accumulates entries --- app.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index e989526..6dd7682 100644 --- a/app.py +++ b/app.py @@ -57,6 +57,8 @@ def init_db(): timestamp DATETIME DEFAULT CURRENT_TIMESTAMP ) ''') + c.execute('CREATE INDEX IF NOT EXISTS idx_guests_id ON guests (id DESC)') + c.execute('CREATE INDEX IF NOT EXISTS idx_guests_email ON guests (email)') conn.commit() conn.close() logger.info("Database initialized.") @@ -96,9 +98,8 @@ def index(): if error: conn = sqlite3.connect(DATABASE) c = conn.cursor() - # TODO: No LIMIT — returns all rows. Add LIMIT 100 or similar. # TODO: No error handling — a locked/corrupted DB returns an unhandled 500. Wrap in try/except. - c.execute('SELECT first_name, location FROM guests ORDER BY id DESC') + c.execute('SELECT first_name, location FROM guests ORDER BY id DESC LIMIT 100') guests = c.fetchall() conn.close() return render_template('index.html', error=error, guests=guests) @@ -120,9 +121,7 @@ def index(): conn = sqlite3.connect(DATABASE) c = conn.cursor() - # TODO: No LIMIT — returns all rows forever. Add LIMIT 100 or similar to avoid memory growth. - # TODO: No indexes on this table — full table scan on every page load. Add index on id/timestamp. - c.execute('SELECT first_name, location FROM guests ORDER BY id DESC') + c.execute('SELECT first_name, location FROM guests ORDER BY id DESC LIMIT 100') guests = c.fetchall() conn.close() logger.info("Rendering index with %d guests.", len(guests))