Skip to content

Commit 4fd2eea

Browse files
authored
Merge pull request #2866 from bengbengbalabalabeng/docs-mysql-pagination-index
docs(mysql): add the order by condition to the pagination statement.
2 parents cd30df4 + 4f3b65d commit 4fd2eea

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,21 @@ SELECT t1.*
9191
FROM t_order t1
9292
INNER JOIN (
9393
-- 这里的子查询可以利用覆盖索引,性能极高
94-
SELECT id FROM t_order LIMIT 1000000, 10
94+
SELECT id FROM t_order ORDER BY id LIMIT 1000000, 10
9595
) t2 ON t1.id = t2.id;
9696
```
9797

9898
**工作原理**:
9999

100-
1. 子查询 `(SELECT id FROM t_order LIMIT 1000000, 10)` 利用主键索引扫描并跳过前 1000000 条记录,返回目标分页的 10 条记录的 ID。
100+
1. 子查询 `(SELECT id FROM t_order ORDER BY id LIMIT 1000000, 10)` 利用主键索引扫描并跳过前 1000000 条记录,返回目标分页的 10 条记录的 ID。
101101
2. 通过 `INNER JOIN` 将子查询结果与主表 `t_order` 关联,获取完整的记录数据。
102102

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

105105
```sql
106106
-- 使用逗号进行延迟关联
107107
SELECT t1.* FROM t_order t1,
108-
(SELECT id FROM t_order LIMIT 1000000, 10) t2
108+
(SELECT id FROM t_order ORDER BY id LIMIT 1000000, 10) t2
109109
WHERE t1.id = t2.id;
110110
```
111111

0 commit comments

Comments
 (0)