Skip to content

Commit 3c28da8

Browse files
authored
Correct deep-pagination-optimization.md
In the section on deep pagination optimization recommendations, even with deferred joins and subqueries, the internal SQL statements will still result in deep pagination. These queries need to be rewritten to use the primary key index (id). Otherwise, there is no difference from the original SQL, and performance might even be worse.
1 parent 82b576e commit 3c28da8

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

docs/high-performance/deep-pagination-optimization.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ SELECT * FROM t_order WHERE id > 100000 LIMIT 10
6464
6565
```sql
6666
# 通过子查询来获取 id 的起始值,把 limit 1000000 的条件转移到子查询
67-
SELECT * FROM t_order WHERE id >= (SELECT id FROM t_order limit 1000000, 1) LIMIT 10;
67+
SELECT * FROM t_order WHERE id >= (SELECT id FROM t_order where id > 1000000 limit 1) LIMIT 10;
6868
```
6969

7070
**工作原理**:
7171

72-
1. 子查询 `(SELECT id FROM t_order LIMIT 1000000, 1)` 会利用主键索引快速定位到第 1000001 条记录,并返回其 ID 值。
72+
1. 子查询 `(SELECT id FROM t_order where id > 1000000 limit 1)` 会利用主键索引快速定位到第 1000001 条记录,并返回其 ID 值。
7373
2. 主查询 `SELECT * FROM t_order WHERE id >= ... LIMIT 10` 将子查询返回的起始 ID 作为过滤条件,使用 `id >=` 获取从该 ID 开始的后续 10 条记录。
7474

7575
不过,子查询的结果会产生一张新表,会影响性能,应该尽量避免大量使用子查询。并且,这种方法只适用于 ID 是正序的。在复杂分页场景,往往需要通过过滤条件,筛选到符合条件的 ID,此时的 ID 是离散且不连续的。
@@ -84,20 +84,20 @@ SELECT * FROM t_order WHERE id >= (SELECT id FROM t_order limit 1000000, 1) LIMI
8484
-- 使用 INNER JOIN 进行延迟关联
8585
SELECT t1.*
8686
FROM t_order t1
87-
INNER JOIN (SELECT id FROM t_order LIMIT 1000000, 10) t2 ON t1.id = t2.id;
87+
INNER JOIN (SELECT id FROM t_order where id > 1000000 LIMIT 10) t2 ON t1.id = t2.id;
8888
```
8989

9090
**工作原理**:
9191

92-
1. 子查询 `(SELECT id FROM t_order LIMIT 1000000, 10)` 利用主键索引快速定位目标分页的 10 条记录的 ID。
92+
1. 子查询 `(SELECT id FROM t_order where id > 1000000 LIMIT 10)` 利用主键索引快速定位目标分页的 10 条记录的 ID。
9393
2. 通过 `INNER JOIN` 将子查询结果与主表 `t_order` 关联,获取完整的记录数据。
9494

9595
除了使用 INNER JOIN 之外,还可以使用逗号连接子查询。
9696

9797
```sql
9898
-- 使用逗号进行延迟关联
9999
SELECT t1.* FROM t_order t1,
100-
(SELECT id FROM t_order limit 1000000, 10) t2
100+
(SELECT id FROM t_order where id > 1000000 LIMIT 10) t2
101101
WHERE t1.id = t2.id;
102102
```
103103

0 commit comments

Comments
 (0)