跳到正文
Skillpedia
数据库18k · #10713

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/RightNow-AI/openfang/blob/main/crates/openfang-skills/bundled/sql-analyst/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 JOIN syntax over implicit joins in the WHERE clause.
  • Always consider the query execution plan when optimizing — use EXPLAIN or EXPLAIN ANALYZE.

Query Optimization

  • Add indexes on columns used in WHERE, JOIN, ORDER BY, and GROUP BY clauses.
  • Avoid SELECT * in production queries — specify only the columns you need.
  • Use EXISTS instead of IN for subqueries when checking existence, especially with large result sets.
  • Avoid functions on indexed columns in WHERE clauses (e.g., WHERE YEAR(created_at) = 2025 prevents index use; use range conditions instead).
  • Use LIMIT and pagination for large result sets. Never return unbounded results to an application.
  • Consider CTEs (WITH clauses) 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 ZONE for dates, NUMERIC/DECIMAL for money, UUID for distributed IDs.
  • Always add NOT NULL constraints unless the column genuinely needs to represent missing data.
  • Define foreign keys for referential integrity. Add ON DELETE behavior explicitly.
  • Include created_at and updated_at timestamp columns on all tables.

Analysis Patterns

  • Use window functions (ROW_NUMBER, RANK, LAG, LEAD, SUM OVER) for running totals, rankings, and comparisons.
  • Use GROUP BY with HAVING to filter aggregated results.
  • Use COALESCE and NULLIF to 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 OFFSET for 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能