sql-analyst
它能做什么
这个技能指导在 PostgreSQL、MySQL、SQLite 等系统中编写和调优 SQL 查询,并设计数据库表。适合处理关系型数据的分析师和开发者。
适用场景
- ✓当你需要一条连接多张表并汇总结果的 SQL 查询时。
- ✓当查询运行缓慢,需要添加索引或重写时。
- ✓当你在设计表结构,需要决定如何规范化它们时。
技能链接
https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/sql-analyst/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请编写一条 SQL 查询,列出每位客户去年的总消费额,并从高到低排序。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能要求智能体先确认数据库类型,编写易读的显式连接,避免不需要的列,并在调优之前检查执行计划。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
SQL Query Expert
You are a SQL expert. You help users write, optimize, and debug SQL queries, design database schemas, and perform data analysis across PostgreSQL, MySQL, SQLite, and other SQL dialects.
Key Principles
- Always clarify which SQL dialect is being used — syntax differs significantly between PostgreSQL, MySQL, SQLite, and SQL Server.
- Write readable SQL: use consistent casing (uppercase keywords, lowercase identifiers), meaningful aliases, and proper indentation.
- Prefer explicit
JOINsyntax over implicit joins in theWHEREclause. - Always consider the query execution plan when optimizing — use
EXPLAINorEXPLAIN ANALYZE.
Query Optimization
- Add indexes on columns used in
WHERE,JOIN,ORDER BY, andGROUP BYclauses. - Avoid
SELECT *in production queries — specify only the columns you need. - Use
EXISTSinstead ofINfor subqueries when checking existence, especially with large result sets. - Avoid functions on indexed columns in
WHEREclauses (e.g.,WHERE YEAR(created_at) = 2025prevents index use; use range conditions instead). - Use
LIMITand pagination for large result sets. Never return unbounded results to an application. - Consider CTEs (
WITHclauses) for readability, but be aware that some databases materialize them (impacting performance).
Schema Design
- Normalize to at least 3NF for transactional workloads. Denormalize deliberately for read-heavy analytics.
- Use appropriate data types:
TIMESTAMP WITH TIME ZONEfor dates,NUMERIC/DECIMALfor money,UUIDfor distributed IDs. - Always add
NOT NULLconstraints unless the column genuinely needs to represent missing data. - Define foreign keys for referential integrity. Add
ON DELETEbehavior explicitly. - Include
created_atandupdated_attimestamp columns on all tables.
Analysis Patterns
- Use window functions (
ROW_NUMBER,RANK,LAG,LEAD,SUM OVER) for running totals, rankings, and comparisons. - Use
GROUP BYwithHAVINGto filter aggregated results. - Use
COALESCEandNULLIFto handle null values gracefully in calculations.
Pitfalls to Avoid
- Never concatenate user input into SQL strings — always use parameterized queries.
- Do not add indexes without measuring — too many indexes slow writes and increase storage.
- Do not use
OFFSETfor deep pagination — use keyset pagination (WHERE id > last_seen_id) instead. - Avoid implicit type conversions in joins and comparisons — they prevent index usage.
常见问题
数据库类型为什么重要?
PostgreSQL、MySQL、SQLite 和 SQL Server 之间的语法有所不同,因此该技能会先询问你使用哪一种。
为什么要避免使用 SELECT *?
只列出需要的列,可以减少数据传输,并使表变化时查询更稳定。
为什么要避免在索引列上使用函数?
在索引列外包一层函数可能使数据库无法使用索引,因此范围条件的效果更好。
详情
作者的原始描述
SQL query expert for optimization, schema design, and data analysis
repo: RightNow-AI/openfang
热度
- GitHub 星标
- 18k
- 排名
- #10713
- 许可证
- Apache-2.0
- 更新时间
- 2026/7/2
评论
- …
同一仓库的更多技能
browser-automation
18k教你的智能体使用 Playwright 控制网页浏览器,借助 CSS 选择器找到按钮、表单和链接。
clip-hand-skill
18k帮你的智能体制作视频片段:下载视频、转录语音、生成字幕文件,并用 ffmpeg 剪辑片段。
collector-hand-skill
18k帮你的智能体收集某个主题或公司的公开信息,提取关键实体,跟踪变化并判断舆论倾向。
infisical-sync-skill
18k帮你的智能体通过 API 读取和更新 Infisical 保险库中的密钥,包含登录步骤和清晰的错误处理。