optimizing-sql
Optimize SQL query performance through EXPLAIN analysis, indexing strategies, and query rewriting for PostgreSQL, MySQL, and SQL Server. Use when debugging slow queries, analyzing execution plans, or improving database performance.
What this skill does
# SQL Optimization Provide tactical guidance for optimizing SQL query performance across PostgreSQL, MySQL, and SQL Server through execution plan analysis, strategic indexing, and query rewriting. ## When to Use This Skill Trigger this skill when encountering: - Slow query performance or database timeouts - Analyzing EXPLAIN plans or execution plans - Determining index requirements - Rewriting inefficient queries - Identifying query anti-patterns (N+1, SELECT *, correlated subqueries) - Database-specific optimization needs (PostgreSQL, MySQL, SQL Server) ## Core Optimization Workflow ### Step 1: Analyze Query Performance Run execution plan analysis to identify bottlenecks: **PostgreSQL:** ```sql EXPLAIN ANALYZE SELECT * FROM users WHERE email = '[email protected]'; ``` **MySQL:** ```sql EXPLAIN FORMAT=JSON SELECT * FROM products WHERE category_id = 5; ``` **SQL Server:** Use SQL Server Management Studio: Display Estimated Execution Plan (Ctrl+L) **Key Metrics to Monitor:** - **Cost**: Estimated resource consumption - **Rows**: Number of rows processed (estimated vs actual) - **Scan Type**: Sequential scan vs index scan - **Execution Time**: Actual time spent on operation For detailed execution plan interpretation, see `references/explain-guide.md`. ### Step 2: Identify Optimization Opportunities **Common Red Flags:** | Indicator | Problem | Solution | |-----------|---------|----------| | Seq Scan / Table Scan | Full table scan on large table | Add index on filter columns | | High row count | Processing excessive rows | Add WHERE filter or index | | Nested Loop with large outer table | Inefficient join algorithm | Index join columns | | Correlated subquery | Subquery executes per row | Rewrite as JOIN or EXISTS | | Sort operation on large result set | Expensive sorting | Add index matching ORDER BY | For scan type interpretation, see `references/scan-types.md`. ### Step 3: Apply Indexing Strategies **Index Decision Framework:** ``` Is column used in WHERE, JOIN, ORDER BY, or GROUP BY? ├─ YES → Is column selective (many unique values)? │ ├─ YES → Is table frequently queried? │ │ ├─ YES → ADD INDEX │ │ └─ NO → Consider based on query frequency │ └─ NO (low selectivity) → Skip index └─ NO → Skip index ``` **Index Types by Use Case:** **PostgreSQL:** - **B-tree** (default): General-purpose, supports <, ≤, =, ≥, >, BETWEEN, IN - **Hash**: Equality comparisons only (=) - **GIN**: Full-text search, JSONB, arrays - **GiST**: Spatial data, geometric types - **BRIN**: Very large tables with naturally ordered data **MySQL:** - **B-tree** (default): General-purpose index - **Full-text**: Text search on VARCHAR/TEXT columns - **Spatial**: Spatial data types **SQL Server:** - **Clustered**: Table data sorted by index (one per table) - **Non-clustered**: Separate index structure (multiple allowed) For comprehensive indexing guidance, see `references/indexing-decisions.md` and `references/index-types.md`. ### Step 4: Design Composite Indexes For queries filtering on multiple columns, use composite indexes: **Column Order Matters:** 1. **Equality filters first** (most selective) 2. **Additional equality filters** (by selectivity) 3. **Range filters or ORDER BY** (last) **Example:** ```sql -- Query pattern SELECT * FROM orders WHERE customer_id = 123 AND status = 'shipped' ORDER BY created_at DESC LIMIT 10; -- Optimal composite index CREATE INDEX idx_orders_customer_status_created ON orders (customer_id, status, created_at DESC); ``` For composite index design patterns, see `references/composite-indexes.md`. ### Step 5: Rewrite Inefficient Queries **Common Anti-Patterns to Avoid:** **1. SELECT * (Over-fetching)** ```sql -- ❌ Bad: Fetches all columns SELECT * FROM users WHERE id = 1; -- ✅ Good: Fetch only needed columns SELECT id, name, email FROM users WHERE id = 1; ``` **2. N+1 Queries** ```sql -- ❌ Bad: 1 + N queries SELECT * FROM users LIMIT 100; -- Then in loop: SELECT * FROM posts WHERE user_id = ?; -- ✅ Good: Single JOIN SELECT users.*, posts.id AS post_id, posts.title FROM users LEFT JOIN posts ON users.id = posts.user_id; ``` **3. Non-Sargable Queries** (functions on indexed columns) ```sql -- ❌ Bad: Function prevents index usage SELECT * FROM orders WHERE YEAR(created_at) = 2025; -- ✅ Good: Sargable range condition SELECT * FROM orders WHERE created_at >= '2025-01-01' AND created_at < '2026-01-01'; ``` **4. Correlated Subqueries** ```sql -- ❌ Bad: Subquery executes per row SELECT name, (SELECT COUNT(*) FROM orders WHERE orders.user_id = users.id) FROM users; -- ✅ Good: JOIN with GROUP BY SELECT users.name, COUNT(orders.id) AS order_count FROM users LEFT JOIN orders ON users.id = orders.user_id GROUP BY users.id, users.name; ``` For complete anti-pattern reference, see `references/anti-patterns.md`. For efficient query patterns, see `references/efficient-patterns.md`. ## Quick Reference Tables ### Index Selection Guide | Query Pattern | Index Type | Example | |--------------|------------|---------| | `WHERE column = value` | Single-column B-tree | `CREATE INDEX ON table (column)` | | `WHERE col1 = ? AND col2 = ?` | Composite B-tree | `CREATE INDEX ON table (col1, col2)` | | `WHERE text_col LIKE '%word%'` | Full-text (GIN/Full-text) | `CREATE INDEX ON table USING GIN (to_tsvector('english', text_col))` | | `WHERE geom && box` | Spatial (GiST) | `CREATE INDEX ON table USING GIST (geom)` | | `WHERE json_col @> '{"key":"value"}'` | JSONB (GIN) | `CREATE INDEX ON table USING GIN (json_col)` | ### Join Optimization Checklist - [ ] Index foreign key columns on both sides of JOIN - [ ] Order joins starting with table returning fewest rows - [ ] Use INNER JOIN when possible (more efficient than OUTER JOIN) - [ ] Avoid joining more than 5 tables (break into CTEs or subqueries) - [ ] Consider denormalization for frequently joined tables in read-heavy systems ### Execution Plan Performance Targets | Scan Type | Performance | When Acceptable | |-----------|-------------|-----------------| | Index-Only Scan | Best | Always preferred | | Index Scan | Excellent | Small-medium result sets | | Bitmap Heap Scan | Good | Medium result sets (PostgreSQL) | | Sequential Scan | Poor | Only for small tables (<1000 rows) or full table queries | | Table Scan | Poor | Only for small tables or unavoidable full scans | ## Database-Specific Optimizations ### PostgreSQL-Specific Features **Partial Indexes** (index subset of rows): ```sql CREATE INDEX idx_active_users_login ON users (last_login) WHERE status = 'active'; ``` **Expression Indexes** (index computed values): ```sql CREATE INDEX idx_users_email_lower ON users (LOWER(email)); ``` **Covering Indexes** (avoid heap access): ```sql CREATE INDEX idx_users_email_covering ON users (email) INCLUDE (id, name); ``` For comprehensive PostgreSQL optimization, see `references/postgresql.md`. ### MySQL-Specific Features **Index Hints** (override optimizer): ```sql SELECT * FROM orders USE INDEX (idx_orders_customer) WHERE customer_id = 123; ``` **Storage Engine Selection:** - **InnoDB** (default): Transactional, row-level locks, clustered primary key - **MyISAM**: Faster reads, no transactions, table-level locks For comprehensive MySQL optimization, see `references/mysql.md`. ### SQL Server-Specific Features **Query Store** (track query performance over time): ```sql ALTER DATABASE YourDatabase SET QUERY_STORE = ON; ``` **Execution Plan Warnings:** - Look for yellow exclamation marks in graphical execution plans - Thick arrows indicate high row counts For comprehensive SQL Server optimization, see `references/sqlserver.md`. ## Advanced Optimization Techniques ### Common Table Expressions (CTEs) Break complex queries into readable, maintainable parts: ```sql WITH active_customers AS ( SELECT id, name FROM customers WHERE status = 'active' ), recent_orders AS ( SELECT customer_id, COUNT(*) as order_count FROM orders WHERE created_at > NOW() - INTERVAL '30 days' GRO
Related in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.