用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/Arete-Consortium/ai-skills --skill sql命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
| name | sql |
| description | SQL Query Optimization |
| lifecycle | experimental |
Analyze and optimize SQL queries.
/sql "SELECT * FROM users..." # Analyze query
/sql --explain # Generate EXPLAIN plan
/sql --index # Suggest indexes
/sql --rewrite # Rewrite for performance
# SQL Analysis
## Original Query
```sql
SELECT u.*, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.created_at > '2024-01-01'
GROUP BY u.id
ORDER BY order_count DESC
LIMIT 100;
Problem: Selects all columns, may fetch unnecessary data Impact: More memory, slower network transfer Fix: Specify only needed columns
Problem: No index on users.created_at
Impact: Full table scan for date filter
Fix: CREATE INDEX idx_users_created_at ON users(created_at);
Problem: No index on orders.user_id
Impact: Slow JOIN operation
Fix: CREATE INDEX idx_orders_user_id ON orders(user_id);
SELECT
u.id,
u.username,
u.email,
COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.created_at > '2024-01-01'
GROUP BY u.id, u.username, u.email
ORDER BY order_count DESC
LIMIT 100;
-- For WHERE clause filtering
CREATE INDEX idx_users_created_at ON users(created_at);
-- For JOIN performance
CREATE INDEX idx_orders_user_id ON orders(user_id);
-- Composite index for common query pattern
CREATE INDEX idx_users_created_at_id ON users(created_at, id);
## Common Anti-Patterns
### SELECT *
```sql
-- Bad
SELECT * FROM users WHERE id = 1;
-- Good
SELECT id, username, email FROM users WHERE id = 1;
-- Bad: N+1 (1 query + N queries)
SELECT * FROM users;
-- Then for each user:
SELECT * FROM orders WHERE user_id = ?;
-- Good: Single query with JOIN
SELECT u.*, o.*
FROM users u
LEFT JOIN orders o ON o.user_id = u.id;
-- Bad: May return millions of rows
SELECT * FROM logs WHERE level = 'ERROR';
-- Good: Bounded result set
SELECT * FROM logs WHERE level = 'ERROR' LIMIT 1000;
-- Bad: Can't use indexes efficiently
SELECT * FROM users WHERE email = 'x' OR username = 'y';
-- Good: Use UNION
SELECT * FROM users WHERE email = 'x'
UNION
SELECT * FROM users WHERE username = 'y';
-- Bad: Can't use index
SELECT * FROM products WHERE name LIKE '%phone%';
-- Good: Use full-text search
SELECT * FROM products WHERE MATCH(name) AGAINST('phone');
-- Index on (a, b, c) can be used for:
WHERE a = ?
WHERE a = ? AND b = ?
WHERE a = ? AND b = ? AND c = ?
-- But NOT for:
WHERE b = ?
WHERE c = ?
WHERE b = ? AND c = ?
EXPLAIN ANALYZE SELECT ...;
| Metric | Good | Bad |
|---|---|---|
| Seq Scan | Small tables | Large tables |
| Index Scan | Large tables | - |
| Rows | Low estimate | High estimate |
| Cost | Low | High |
When /sql is invoked: