Optimize a slow SQL query
Find why a query is slow and get a faster version, index suggestions and a way to prove the improvement.
Turn feature requirements into a normalized schema with keys, constraints, indexes and the tradeoffs explained.
Design a {{database}} schema for the following application.
What the application does: {{app}}
Core requirements and rules:
{{requirements}}
Expected scale and main query patterns: {{scale}}
Deliver:
1. Entities and relationships: a short list in plain words, including cardinality (one-to-many, many-to-many) and anything optional.
2. Schema as runnable DDL, with primary keys, foreign keys and their ON DELETE behavior, NOT NULL and CHECK constraints, unique constraints, sensible types (for money, timestamps with time zones, identifiers, enums) and default values.
3. Indexes: justified by the query patterns I gave. Do not add indexes without naming the query they serve.
4. Decisions and tradeoffs: normalization choices, where denormalization is worth it, soft delete versus hard delete, audit history, multi-tenancy handling.
5. Five example queries for the most important access patterns.
6. Evolution: which likely future requirements would be painful with this design, and how to prepare for them.
7. Questions: ambiguities in my requirements that would change the design.
Use clear snake_case names. Enforce rules in the database rather than relying on application code whenever the database can express them.Requiring every index to name the query it serves prevents speculative indexing. The final questions surface the unclear requirements that most often cause redesigns.
Licensed CC0 (public domain) Version 1 · history
Find why a query is slow and get a faster version, index suggestions and a way to prove the improvement.
Describe the question and your tables, and get a correct, readable query with an explanation and edge cases.
Describe your tables and the question you need answered and get a correct, commented SQL query with assumptions spelled out.
Set up a proper cohort retention analysis, from defining cohorts and the activity event to producing the table and reading it correctly.
Paste a slow or suspicious SQL query and get correctness checks, performance fixes, and a cleaner rewrite you can verify.
Draft a clear, tight system prompt that tells an AI coding assistant how to work in your repository.
Comments (0)
Sign in to join the discussion.
No comments yet. Be the first to share how it worked.