query-optimizer
Analyze and optimize SQL queries for better performance and efficiency.
What this skill does
# Query Optimizer Skill Analyze and optimize SQL queries for better performance and efficiency. ## Instructions You are a database performance optimization expert. When invoked: 1. **Analyze Query Performance**: - Use EXPLAIN/EXPLAIN ANALYZE to understand execution plan - Identify slow queries from logs - Measure query execution time - Detect full table scans and missing indexes 2. **Identify Bottlenecks**: - Find N+1 query problems - Detect inefficient JOINs - Identify missing or unused indexes - Spot suboptimal WHERE clauses 3. **Optimize Queries**: - Add appropriate indexes - Rewrite queries for better performance - Suggest caching strategies - Recommend query restructuring 4. **Provide Recommendations**: - Index creation suggestions - Query rewriting alternatives - Database configuration tuning - Monitoring and alerting setup ## Supported Databases - **SQL**: PostgreSQL, MySQL, MariaDB, SQL Server, SQLite - **Analysis Tools**: EXPLAIN, EXPLAIN ANALYZE, Query Profiler - **Monitoring**: pg_stat_statements, slow query log, performance schema ## Usage Examples ``` @query-optimizer @query-optimizer --analyze-slow-queries @query-optimizer --suggest-indexes @query-optimizer --explain SELECT * FROM users WHERE email = '[email protected]' @query-optimizer --fix-n-plus-one ``` ## Query Analysis Tools ### PostgreSQL - EXPLAIN ANALYZE ```sql -- Basic EXPLAIN EXPLAIN SELECT u.id, u.username, COUNT(o.id) as order_count FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.active = true GROUP BY u.id, u.username; -- EXPLAIN ANALYZE - actually runs the query EXPLAIN ANALYZE SELECT u.id, u.username, COUNT(o.id) as order_count FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.active = true GROUP BY u.id, u.username; -- EXPLAIN with all options (PostgreSQL) EXPLAIN (ANALYZE, BUFFERS, VERBOSE, FORMAT JSON) SELECT * FROM orders WHERE user_id = 123 AND created_at >= '2024-01-01'; ``` **Reading EXPLAIN Output:** ``` Seq Scan on users (cost=0.00..1234.56 rows=10000 width=32) Filter: (active = true) -- Seq Scan = Sequential Scan (full table scan) - BAD for large tables -- cost=0.00..1234.56 = startup cost..total cost -- rows=10000 = estimated rows -- width=32 = average row size in bytes ``` ``` Index Scan using idx_users_email on users (cost=0.29..8.30 rows=1 width=32) Index Cond: (email = '[email protected]'::text) -- Index Scan = Using index - GOOD -- Much lower cost than Seq Scan -- rows=1 = accurate estimate ``` ### MySQL - EXPLAIN ```sql -- MySQL EXPLAIN EXPLAIN SELECT u.id, u.username, COUNT(o.id) as order_count FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE u.active = true GROUP BY u.id, u.username; -- EXPLAIN with execution stats (MySQL 8.0+) EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 123; -- Show warnings for optimization info EXPLAIN SELECT * FROM users WHERE email = '[email protected]'; SHOW WARNINGS; ``` **MySQL EXPLAIN Output:** ``` +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+ | 1 | SIMPLE | users | ALL | NULL | NULL | NULL | NULL | 1000 | Using where | +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+ -- type=ALL means full table scan - BAD -- key=NULL means no index used +----+-------------+-------+------+---------------+----------------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+----------------+---------+-------+------+-------+ | 1 | SIMPLE | users | ref | idx_users_email| idx_users_email| 767 | const | 1 | NULL | +----+-------------+-------+------+---------------+----------------+---------+-------+------+-------+ -- type=ref means index lookup - GOOD -- key shows index being used ``` ## Common Performance Issues ### 1. Missing Indexes **Problem:** ```sql -- Slow query - full table scan SELECT * FROM users WHERE email = '[email protected]'; -- EXPLAIN shows: -- Seq Scan on users (cost=0.00..1500.00 rows=1 width=100) -- Filter: (email = '[email protected]') ``` **Solution:** ```sql -- Add index on email column CREATE INDEX idx_users_email ON users(email); -- Now EXPLAIN shows: -- Index Scan using idx_users_email on users (cost=0.29..8.30 rows=1 width=100) -- Index Cond: (email = '[email protected]') -- Query becomes 100x faster ``` ### 2. N+1 Query Problem **Problem:** ```javascript // ORM code causing N+1 queries const users = await User.findAll(); // 1 query for (const user of users) { const orders = await Order.findAll({ where: { userId: user.id } // N queries (one per user) }); console.log(`${user.name}: ${orders.length} orders`); } // Total: 1 + N queries for N users // For 100 users = 101 queries! ``` **Solution:** ```javascript // Use eager loading - single query with JOIN const users = await User.findAll({ include: [{ model: Order, attributes: ['id', 'total_amount'] }] }); for (const user of users) { console.log(`${user.name}: ${user.orders.length} orders`); } // Total: 1 query regardless of user count ``` **SQL Equivalent:** ```sql -- Instead of multiple queries: SELECT * FROM users; SELECT * FROM orders WHERE user_id = 1; SELECT * FROM orders WHERE user_id = 2; -- ... (N more queries) -- Use single JOIN query: SELECT u.id, u.name, COUNT(o.id) as order_count FROM users u LEFT JOIN orders o ON u.id = o.user_id GROUP BY u.id, u.name; ``` ### 3. SELECT * Inefficiency **Problem:** ```sql -- Fetching all columns when only need few SELECT * FROM products WHERE category_id = 5; -- Fetches: id, name, description (large text), image_url, specs (json), -- price, stock, created_at, updated_at, etc. ``` **Solution:** ```sql -- Only select needed columns SELECT id, name, price, stock FROM products WHERE category_id = 5; -- Benefits: -- - Less data transferred -- - Faster query execution -- - Lower memory usage -- - Can use covering indexes ``` ### 4. Inefficient Pagination **Problem:** ```sql -- OFFSET becomes slow with large offsets SELECT * FROM users ORDER BY created_at DESC LIMIT 20 OFFSET 10000; -- Database must: -- 1. Sort all rows -- 2. Skip 10,000 rows -- 3. Return next 20 -- Gets slower as offset increases ``` **Solution:** ```sql -- Use cursor-based (keyset) pagination SELECT * FROM users WHERE created_at < '2024-01-01 12:00:00' AND (created_at < '2024-01-01 12:00:00' OR id < 12345) ORDER BY created_at DESC, id DESC LIMIT 20; -- Or with indexed column: SELECT * FROM users WHERE id < 10000 ORDER BY id DESC LIMIT 20; -- Benefits: -- - Consistent performance regardless of page -- - Uses index efficiently -- - No need to skip rows ``` ### 5. Function on Indexed Column **Problem:** ```sql -- Function prevents index usage SELECT * FROM users WHERE LOWER(email) = '[email protected]'; -- EXPLAIN shows Seq Scan (index not used) ``` **Solution 1 - Store lowercase:** ```sql -- Add computed column ALTER TABLE users ADD COLUMN email_lower VARCHAR(255) GENERATED ALWAYS AS (LOWER(email)) STORED; CREATE INDEX idx_users_email_lower ON users(email_lower); -- Query: SELECT * FROM users WHERE email_lower = '[email protected]'; ``` **Solution 2 - Functional index (PostgreSQL):** ```sql -- Create index on function result CREATE INDEX idx_users_email_lower ON users(LOWER(email)); -- Now original query uses index SELECT * FROM users WHERE LOWER(email) = '[email protected]'; ``` **Solution 3 - Case-insensitive collation:** ```sql -- PostgreSQL - use citext type ALTER TABLE users ALTER COLUMN email TYPE citext; -- Query without LOWER: SELECT * FROM users WHERE email = '[email protected]'; -- Automatic
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.