Files
nexus/wiki/concepts/Sort-Key.md
2026-04-28 20:03:11 +08:00

41 lines
1.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Sort Key"
type: concept
tags:
- Data-Engineering
- Database
- AWS-Redshift
- Performance-Optimization
last_updated: 2026-04-14
---
## Definition
Sort Key排序键是数据仓库中用于确定数据在存储介质上物理排列顺序的列或列组合。通过预先对数据进行排序Sort Key 可以显著提升范围查询、过滤和聚合操作的性能。
## Key Characteristics
- **物理排序**:数据按 Sort Key 列的值在磁盘上连续存储
- **范围查询优化**:对于 `WHERE date BETWEEN ...` 类查询,只需扫描相关数据块
- **ZONE MAP**:列式存储系统(如 Redshift利用 Sort Key 构建 ZONE MAP快速跳过不包含目标值的数据块
- **单一 vs 复合**
- **单一排序键Compound Sort Key**:按列顺序联合排序,支持前缀匹配查询
- **交错排序键Interleaved Sort Key**:各列权重相同,支持更灵活的查询模式,但维护成本更高
## Use Cases
- 时间序列数据的日期/时间列排序(典型选择)
- 频繁过滤的高基数列
- 需要加速 `ORDER BY``GROUP BY` 的列
## Related Concepts
- [[Distribution-Key]]:数据在集群节点间的分布方式
- [[Columnar-Storage]]:列式存储使 Sort Key 优化成为可能
- [[MPP]]Sort Key 优化局部性,提升 MPP 节点内效率
- [[Amazon-Redshift]]Sort Key 是 Redshift 性能优化的核心手段之一
## Sources
- [[ctp-topic-68-introduction-to-redshift]]Sort Key 在 Redshift 架构中的作用