用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/Objective-Arts/lens-dist --skill sql-perf命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | sql-perf |
| description | SQL performance patterns - indexing and query optimization |
Channel Markus Winand: author of "SQL Performance Explained" and use-the-index-luke.com.
"Indexes are the key to SQL performance. Understand how they work, not just how to create them."
The Indexing Mantra: The database doesn't know what you want. Help it by designing proper indexes.
[50]
/ \
[25] [75]
/ \ / \
[10,20] [30,40] [60,70] [80,90]
↓
Leaf nodes contain row pointers
-- Index on (status)
SELECT * FROM orders WHERE status = 'shipped'; -- Uses index
-- But NOT for functions on columns
SELECT * FROM orders WHERE UPPER(status) = 'SHIPPED'; -- Full scan!
-- Solution: Function-based index or fix the query
CREATE INDEX idx_orders_status_upper ON orders (UPPER(status));
-- Index on (a, b, c)
WHERE a = 1 -- Uses index
WHERE a = 1 AND b = 2 -- Uses index
WHERE a = 1 AND b = 2 AND c = 3 -- Uses index (fully)
WHERE b = 2 -- Does NOT use index
WHERE a = 1 AND c = 3 -- Uses index for 'a' only
-- Index on (date, status)
WHERE date = '2024-01-01' AND status = 'shipped' -- Full index use
WHERE date > '2024-01-01' AND status = 'shipped' -- Only date part used!
-- Solution: Reorder index
-- Index on (status, date)
WHERE status = 'shipped' AND date > '2024-01-01' -- Full index use
-- Query: WHERE tenant_id = ? AND created_at > ?
-- Best index: (tenant_id, created_at)
-- Equality columns first, then range column
-- Query: WHERE status = 'active' ORDER BY created_at DESC
-- Best index: (status, created_at DESC)
-- Avoids filesort
-- LIMIT optimization
-- Query: WHERE status = 'active' ORDER BY created_at DESC LIMIT 10
-- Same index, but DB can stop after 10 rows
-- Query: SELECT id, name FROM users WHERE email = ?
-- Covering index: (email, id, name)
-- All data from index, no table access needed
CREATE INDEX idx_users_email_covering ON users (email) INCLUDE (id, name);
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 1;
| Metric | Good | Bad |
|---|---|---|
| Type | eq_ref, ref, range | ALL (full scan) |
| Rows | Small number | Large number |
| Extra | Using index | Using filesort, Using temporary |
| Problem | Symptom | Fix |
|---|---|---|
| Full table scan | type: ALL | Add appropriate index |
| Filesort | Extra: Using filesort | Index for ORDER BY |
| Temporary table | Extra: Using temporary | Optimize GROUP BY |
| Index not used | Key: NULL | Check index, query |
-- BAD: Separate index for each column
CREATE INDEX idx_a ON t(a);
CREATE INDEX idx_b ON t(b);
CREATE INDEX idx_c ON t(c);
-- Query: WHERE a = 1 AND b = 2 AND c = 3
-- Only ONE index used! Others wasted.
-- GOOD: Composite index for common query patterns
CREATE INDEX idx_abc ON t(a, b, c);
-- BAD: Index on boolean (only 2 values)
CREATE INDEX idx_active ON users(is_active);
-- Usually not used - full scan might be faster
-- GOOD: Partial index (if supported)
CREATE INDEX idx_active_users ON users(id) WHERE is_active = true;
-- BAD: Cannot use index
WHERE name LIKE '%smith%'
-- BETTER: Use full-text search
WHERE MATCH(name) AGAINST('smith')
-- OR: Only trailing wildcard (uses index)
WHERE name LIKE 'smith%'
-- SLOW: Must scan and discard 10000 rows
SELECT * FROM posts ORDER BY created_at DESC LIMIT 10 OFFSET 10000;
-- FAST: Seeks directly to the right position
SELECT * FROM posts
WHERE created_at < '2024-01-15 10:30:00'
ORDER BY created_at DESC
LIMIT 10;
-- Client passes the last seen value for next page
-- Only index the subset you actually query
CREATE INDEX idx_active_orders ON orders(customer_id)
WHERE status = 'active';
-- Smaller index, faster maintenance
-- Only useful for queries that include WHERE status = 'active'