sql-optimization-patterns
Optimize SQL queries and database performance
Auch verfügbar von: wshobson
Slow database queries frustrate users and increase infrastructure costs. This skill provides systematic patterns to analyze query plans, design effective indexes, and transform sluggish SQL into high-performance operations.
Die Skill-ZIP herunterladen
In Claude hochladen
Gehe zu Einstellungen → Fähigkeiten → Skills → Skill hochladen
Einschalten und loslegen
Teste es
Verwendung von "sql-optimization-patterns". Query with sequential scans on a 10M row table
Erwartetes Ergebnis:
Analysis: The EXPLAIN shows a Sequential Scan because no index exists on the 'created_at' column used in the WHERE clause. Recommendation: CREATE INDEX idx_orders_created ON orders(created_at DESC). Expected improvement: Query time should drop from 2.3s to under 50ms for recent date ranges.
Verwendung von "sql-optimization-patterns". N+1 pattern loading user orders individually
Erwartetes Ergebnis:
Problem: 101 queries executed (1 for users + 100 for orders). Solution: Replace with a single JOIN query or use batch loading with WHERE user_id IN (...). The refactored approach reduces database round-trips by 99% and total execution time from 5.2s to 120ms.
Sicherheitsaudit
SicherStatic analysis flagged 103 potential issues that are all false positives. The 'external_commands' findings are markdown code fence backticks in documentation, not shell execution. The 'weak cryptographic algorithm' findings reference SQL examples, not actual crypto code. The 'filesystem' finding is a PostgreSQL COPY command syntax example in documentation. This skill contains only markdown documentation with SQL optimization patterns and examples - no executable code or security risks.
Qualitätsbewertung
Was du bauen kannst
E-commerce Platform Optimization
A development team struggles with slow product search and order history queries during peak traffic. The skill helps them analyze slow query logs, identify missing indexes on frequently filtered columns, and refactor N+1 patterns in order loading to reduce API response times from seconds to milliseconds.
Analytics Dashboard Performance
A data engineer needs to speed up aggregation queries for real-time dashboards displaying sales metrics. The skill guides implementation of materialized views for expensive calculations, covering indexes for index-only scans, and efficient GROUP BY patterns to achieve sub-second dashboard refresh rates.
Legacy Application Modernization
A senior developer inherits an application with dozens of slow queries causing timeout errors. The skill provides a systematic approach: capture slow queries with pg_stat_statements, analyze execution plans, prioritize high-impact optimizations, and implement proper indexing to eliminate 90% of performance issues without application rewrites.
Probiere diese Prompts
I have a slow SQL query that takes over 5 seconds to run. Here is the query and table structure: [paste query and schema]. Can you analyze what might be causing the slowness and suggest specific indexes to create?
I ran EXPLAIN ANALYZE on my query and got this output: [paste EXPLAIN output]. Please explain what each operation means, identify the performance bottlenecks, and recommend specific optimizations.
I have a table with columns [list columns] and my most common queries are: [list queries]. What indexing strategy should I use? Consider composite indexes, partial indexes, and covering indexes where appropriate.
My application executes hundreds of similar queries in a loop: [describe pattern or show code]. This is causing performance issues. Help me refactor this using JOINs, batch loading, or other techniques to reduce the number of database round-trips.
Bewährte Verfahren
- Always use EXPLAIN ANALYZE to understand actual query execution before optimizing
- Create indexes on columns used in WHERE, JOIN, and ORDER BY clauses - but avoid over-indexing as each index slows write operations
- Fetch only required columns instead of SELECT *, and filter data as early as possible in the query
Vermeiden
- Using functions on indexed columns in WHERE clauses without corresponding functional indexes
- Pagination with large OFFSET values on big tables instead of cursor-based approaches
- Executing queries in application loops (N+1 pattern) instead of batch loading or JOINs
Häufig gestellte Fragen
How do I identify which queries to optimize first?
Will adding more indexes always improve performance?
What is the difference between EXPLAIN and EXPLAIN ANALYZE?
When should I use a composite index versus separate indexes?
How do I know if my index is being used?
What is cursor-based pagination and when should I use it?
Entwicklerdetails
Autor
sickn33Lizenz
MIT
Repository
https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/sql-optimization-patternsRef
main
Dateistruktur