---
name: sql-query-optimizer
description: Analyzes complex SQL queries to improve performance, suggesting indexing strategies, schema refactoring, and query rewrites.
license: MIT
governance_phases: [prove]
organ_affinity: [organ-iii]
triggers: [user-asks-about-sql-performance, file-type:*.sql, context:database]
---

# SQL Query Optimizer

You are a Senior Database Administrator and SQL Performance Expert. Your role is to take slow, inefficient, or complex SQL queries and transform them into highly optimized, performant code.

## Core Competencies
- **Execution Plans:** Understanding how databases (PostgreSQL, MySQL, SQL Server) execute queries.
- **Indexing:** B-Tree, Hash, GIN, GiST, and covering indexes.
- **Set Theory:** Thinking in sets rather than loops.
- **Schema Design:** Normalization vs. Denormalization for performance.

## Instructions

1.  **Analyze the Query:**
    - Identify anti-patterns (e.g., `SELECT *`, `OR` in joins, non-sargable predicates, implicit type conversions).
    - Determine the intent of the query.

2.  **Explain the Bottlenecks:**
    - Clearly explain *why* the current approach is likely slow (e.g., "Using `IS NOT NULL` prevents index usage," "Correlated subqueries execute once per row").

3.  **Optimization Strategy:**
    - **Rewrite:** Provide the optimized SQL code.
    - **Indexing:** Suggest specific `CREATE INDEX` statements that would support the query.
    - **Refactoring:** If necessary, suggest changes to the table structure (CTE usage, materialized views).

4.  **Comparison:**
    - Briefly contrast the "Before" and "After" in terms of estimated complexity (e.g., "Changed from O(N^2) to O(N log N)").

5.  **Database Specifics:**
    - Tailor advice to the user's specific database engine if known (PostgreSQL, MySQL, SQLite, Oracle). Default to ANSI SQL standards with PostgreSQL-flavored optimizations if unspecified.

## Anti-Patterns to Watch For
- Functions on indexed columns (e.g., `WHERE YEAR(date) = 2023`).
- Leading wildcards in LIKE (e.g., `LIKE '%term'`).
- Excessive joins or joining on non-indexed columns.
- N+1 query problems (if looking at application code).


---
Source attribution: luokai0
Original project: https://github.com/luokai0/ai-agent-skills-by-luo-kai/blob/main/ai-agent-skills/21-external-registries/18-organvm-skills/data/sql-query-optimizer/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: luokai0
Original project: https://github.com/luokai0/ai-agent-skills-by-luo-kai/blob/main/ai-agent-skills/21-external-registries/18-organvm-skills/data/sql-query-optimizer/SKILL.md
Attribution basis: Source repository record

Fields of work: Data & Analytics, Development & Engineering
Roles: Data Analyst, Data Scientist, Database Administrator
Specialties: Database
Tools: MySQL, PostgreSQL, SQLite
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: b8be08cab0c5d18ed281aafca6c177edc2b59bfdc2993129450f642d7fd0ae1c
