Skip to content

perf: optimize sql queries and database connections#31

Open
m-truscott wants to merge 1 commit intomainfrom
perf-optimize-sql-queries-bac8b
Open

perf: optimize sql queries and database connections#31
m-truscott wants to merge 1 commit intomainfrom
perf-optimize-sql-queries-bac8b

Conversation

@m-truscott
Copy link
Copy Markdown

Summary

Refactor and optimize SQL queries in the SqlQuery class to prevent SQL injection vulnerabilities, ensure database connections are closed properly, and improve overall query execution performance.

Changes

  • Implement contextlib.closing for all sqlite3.connect calls to ensure safe connection closures
  • Update album_exists to use parameterized queries and SELECT 1 with fetchone() to avoid fetching full rows and prevent SQL injection
  • Refactor join_albums to use LEFT JOINs instead of nested subqueries for improved execution time
  • Update top_invoices to apply the LIMIT 10 clause in SQL rather than relying on Python list slicing

Metrics

Metric Mean Std Improvement
benchmark runtime (s) 16.3826 - -11.37%
benchmark memory (B) 87379069.52 - -3.90%
benchmark cpu (%) 99.1296 - +5.42%
unit runtime (s) 1.0040 - -50.11%
unit memory (B) 3612672.00 - -76.86%
unit cpu (%) 0.0000 - -100.00%

Validation

All validation checks passed:

  • Compilation: uv pip install poetry && python -m poetry install — passed (runtime 1.0060s, +0.03%; memory 26316800.00B, +1.39%)
  • Unit tests: python -m poetry run pytest --benchmark-skip tests/ — passed
  • Benchmark: python -m poetry run pytest --benchmark-only tests/ — passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant