SQL for FDE(FDE 的 SQL 技能)
知识库位置:
FDE Knowledge Base/03 Engineering/Data Engineering/SQL/SQL-for-FDE.md术语不确定时,请查阅 Glossary(中英文术语表)。
1. Why SQL Is a Core FDE Skill(为什么 SQL 是 FDE 核心技能)
Section titled “1. Why SQL Is a Core FDE Skill(为什么 SQL 是 FDE 核心技能)”SQL(Structured Query Language,结构化查询语言)是 FDE 最实用的基础技能之一。
因为真实客户项目经常需要:
Understand Data(理解数据)Validate Data(校验数据)Debug Data(调试数据)Measure Data(度量数据)Integrate Data(集成数据)这些工作大量依赖 SQL。
2. Typical FDE Usage(FDE 典型用法)
Section titled “2. Typical FDE Usage(FDE 典型用法)”FDE 使用 SQL 常见于:
- inspect customer tables(检查客户表)
- calculate business baseline(计算业务基线)
- debug missing records(排查丢失记录)
- validate API output(验证接口输出)
- identify duplicates(识别重复数据)
- reconcile systems(跨系统对账)
- analyze pilot metrics(分析试点指标)
- build prototypes(搭建原型)
3. Core Mental Model(核心心智模型)
Section titled “3. Core Mental Model(核心心智模型)”SQL 主要操作:
Table(表)↓Filter(过滤)↓Join(关联)↓Aggregate(聚合)↓Transform(转换)↓Result(结果)4. Minimum Skill Set(最低技能集)
Section titled “4. Minimum Skill Set(最低技能集)”FDE 至少应该熟练:
SELECT(查询)WHERE(条件过滤)JOIN(关联)GROUP BY(分组)ORDER BY(排序)CASE(条件表达式)CTE(Common Table Expression,公用表表达式)Window Functions(窗口函数)Aggregation(聚合)NULL handling(NULL 空值处理)同时理解:
Index(索引)Transaction(事务)Query Plan(查询计划)基础概念。
5. SELECT and Filtering(查询与过滤)
Section titled “5. SELECT and Filtering(查询与过滤)”最基础问题:
哪些数据符合某个业务条件?
例如:
SELECT customer_id, statusFROM customersWHERE status = 'active';FDE 应能够快速写这类查询验证数据。
6. JOIN(关联)
Section titled “6. JOIN(关联)”企业数据通常跨表。
例如:
Customer(客户)↓Order(订单)↓Invoice(发票)需要通过 Key(键)Join(关联)。
FDE 必须特别注意:
Join 是否导致记录数量意外增长?
7. Join Cardinality(关联基数)
Section titled “7. Join Cardinality(关联基数)”常见关系:
1:1(一对一)1:N(一对多)N:N(多对多)如果错误理解:
Customer JOIN Orders JOIN Tickets(客户关联订单关联工单)可能产生:
Cartesian-like row multiplication(类笛卡尔积的行数膨胀)。
这会严重污染指标。
8. GROUP BY(分组聚合)
Section titled “8. GROUP BY(分组聚合)”常用于计算:
- count(计数)
- average(平均值)
- total(合计)
- grouped metrics(分组指标)
例如:
SELECT status, COUNT(*)FROM ticketsGROUP BY status;9. Window Functions(窗口函数)
Section titled “9. Window Functions(窗口函数)”非常适合:
- latest record(最新一条记录)
- ranking(排名)
- running total(累计值)
- previous event(前一事件)
例如:
ROW_NUMBER(行号)LAG(取前值)LEAD(取后值)SUM OVER(窗口累计求和)FDE 处理业务数据时非常常用。
10. NULL(空值)
Section titled “10. NULL(空值)”NULL 不等于:
0''false(假值)数据质量分析中需要特别注意:
NULL可能代表:
- unknown(未知)
- not collected(未采集)
- not applicable(不适用)
- pipeline error(管道错误)
业务意义不同。
11. Transactions(事务)
Section titled “11. Transactions(事务)”事务意味着:
一组操作应该作为一个整体提交。
关键概念:
BEGIN(开启事务)COMMIT(提交)ROLLBACK(回滚)FDE 不一定每天写复杂事务,但必须理解:
多步写操作失败时会发生什么。
12. Index(索引)
Section titled “12. Index(索引)”Index(索引)通常用于提升 Query Performance(查询性能)。
但:
More Index(更多索引)≠Always Better(总是更好)因为会影响:
- write speed(写入速度)
- storage(存储)
- maintenance(维护)
13. Query Plan(查询计划)
Section titled “13. Query Plan(查询计划)”查询很慢时,不要只猜。
查看 Execution Plan(执行计划),判断:
- full scan(全表扫描)
- index usage(索引使用)
- join strategy(关联策略)
- row estimates(行数估计)
14. SQL vs RAG(SQL 与 RAG 的选择)
Section titled “14. SQL vs RAG(SQL 与 RAG 的选择)”非常重要:
如果问题是:
How many unpaid invoices does customer X have?(客户 X 有多少张未支付发票?)
应该优先:
SQL / API(结构化查询/接口)而不是:
RAG(检索增强生成)因为需要:
- exact(精确)
- current(实时)
- structured(结构化)
15. SQL as a Debugging Tool(把 SQL 当调试工具)
Section titled “15. SQL as a Debugging Tool(把 SQL 当调试工具)”例如 Pipeline(管道)缺记录。
可以一步一步查:
Source Count(源表计数)↓Staging Count(临时层计数)↓Transformed Count(转换后计数)↓Serving Count(服务层计数)通过 SQL 快速定位丢失发生在哪层。
16. Data Quality Queries(数据质量查询)
Section titled “16. Data Quality Queries(数据质量查询)”FDE 应能写:
Missing(缺失检查)
Section titled “Missing(缺失检查)”SELECT COUNT(*)FROM customersWHERE customer_id IS NULL;Duplicate(重复检查)
Section titled “Duplicate(重复检查)”SELECT customer_id, COUNT(*)FROM customersGROUP BY customer_idHAVING COUNT(*) > 1;Freshness(新鲜度检查)
Section titled “Freshness(新鲜度检查)”SELECT MAX(updated_at)FROM orders;17. Common Failure Modes(常见失败模式)
Section titled “17. Common Failure Modes(常见失败模式)”Failure 1 — SELECT *
Section titled “Failure 1 — SELECT *”在大表上可能:
- expensive(代价高)
- slow(慢)
- expose unnecessary data(暴露不必要数据)
Failure 2 — Wrong Join(错误关联)
Section titled “Failure 2 — Wrong Join(错误关联)”导致指标膨胀。
Failure 3 — Ignore Timezone(忽略时区)
Section titled “Failure 3 — Ignore Timezone(忽略时区)”时间数据分析错误。
Failure 4 — Ignore NULL(忽略空值)
Section titled “Failure 4 — Ignore NULL(忽略空值)”导致业务含义错误。
Failure 5 — Production Write Query Without Safety(无安全措施的生产写操作)
Section titled “Failure 5 — Production Write Query Without Safety(无安全措施的生产写操作)”FDE 在生产环境中必须特别谨慎。
18. FDE Safety Rule(FDE 安全规则)
Section titled “18. FDE Safety Rule(FDE 安全规则)”生产数据库操作前应确认:
Read-only?(只读吗?)Environment?(哪个环境?)Transaction?(有事务吗?)Backup?(有备份吗?)Row scope?(影响行范围?)Rollback?(能回滚吗?)不要因为 SQL 简单就低估风险。
19. How Deep Should an FDE Go?(FDE 应掌握到多深?)
Section titled “19. How Deep Should an FDE Go?(FDE 应掌握到多深?)”目标:
Level B/C(B 级操作 / C 级设计)能够:
- 独立查询
- Debug(调试)数据
- 做基础性能分析
- 理解事务与 Index(索引)
不要求成为 DBA(Database Administrator,数据库管理员)。