Loading...
Loading...
Compare original and translation side by side
ORDER BYLIMIT NLIMIT NORDER BYLIMIT N| Original | Optimized | Why Safe |
|---|---|---|
| | Equivalent range |
| | Equivalent range |
| | Equivalent range |
| | Same boundaries |
| | Equivalent range |
| 原查询 | 优化后查询 | 安全性说明 |
|---|---|---|
| | 等价范围查询 |
| | 等价范围查询 |
| | 等价范围查询 |
| | 边界条件一致 |
| | 等价范围查询 |
| Pattern | Why Not |
|---|---|
| Dynamic values, cannot precompute range |
| Comparing two columns, both need function |
| Day-of-week has no contiguous range |
| Needed for grouping logic |
| Function in SELECT/GROUP BY is fine, only filter matters |
| 模式 | 原因 |
|---|---|
| 动态值,无法预计算范围 |
| 比较两列,均需使用函数 |
| 星期几没有连续范围 |
| 分组逻辑需要该函数 |
| SELECT/GROUP BY中使用函数无问题,仅过滤条件需关注 |
| Original | Optimized | Why Safe |
|---|---|---|
| | If both are same type (e.g., INTEGER) |
| | If data is already consistently cased |
| | If data has no leading/trailing spaces |
| 原查询 | 优化后查询 | 安全性说明 |
|---|---|---|
| | 如果两者类型相同(例如INTEGER) |
| | 如果数据大小写已保持一致 |
| | 如果数据没有前导/尾随空格 |
| Pattern | Why Not |
|---|---|
| Types genuinely differ, CAST required |
| Different granularity, DATE() required |
| If b.code might have different case |
| Arithmetic transformation, cannot remove |
| 模式 | 原因 |
|---|---|
| 类型确实不同,必须使用CAST |
| 粒度不同,必须使用DATE() |
| 如果b.code可能存在不同大小写 |
| 算术转换,无法移除 |
NOT INNOT IN| Original | Optimized | Why Safe |
|---|---|---|
| | Equivalent when subquery column is NOT NULL |
| | NOT NULL guarantees equivalence |
| 原查询 | 优化后查询 | 安全性说明 |
|---|---|---|
| | 当子查询列不为NULL时等价 |
| | NOT NULL约束保证等价性 |
| Pattern | Why Not |
|---|---|
| If subquery returns NULL, NOT IN returns no rows; NOT EXISTS doesn't |
| Multi-column NOT IN has complex NULL semantics |
| 模式 | 原因 |
|---|---|
| 如果子查询返回NULL,NOT IN不会返回任何行;NOT EXISTS则不会出现此问题 |
| 多列NOT IN的NULL语义复杂 |
| Original | Optimized |
|---|---|
| Subquery appears 2+ times identically | Extract to CTE, reference CTE multiple times |
| Same aggregation used in multiple places | Compute once in CTE |
| 原查询 | 优化后查询 |
|---|---|
| 相同子查询出现2次及以上 | 提取为CTE,多次引用该CTE |
| 同一聚合在多个地方使用 | 在CTE中计算一次 |
| Pattern | Why Not |
|---|---|
| Correlated subquery (references outer table) | Each execution is different, cannot cache |
| Subqueries with different filters | Not actually the same subquery |
| Subquery in SELECT that depends on current row | Correlation prevents extraction |
| 模式 | 原因 |
|---|---|
| 关联子查询(引用外部表) | 每次执行结果不同,无法缓存 |
| 子查询过滤条件不同 | 并非真正相同的子查询 |
| SELECT中依赖当前行的子查询 | 关联关系导致无法提取 |
FROM a, b, c WHERE a.id = b.id AND b.id = c.idFROM a, b, c WHERE a.id = b.id AND b.id = c.idSUM(SUM(x)) OVER(...)SUM(SUM(x)) OVER(...)