Skip to content

Commit ae32d74

Browse files
committed
[docs add]深度分页介绍及优化建议
1 parent 04ebec5 commit ae32d74

9 files changed

+103
-23
lines changed

README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -373,18 +373,16 @@ JVM 这部分内容主要参考 [JVM 虚拟机规范-Java8](https://docs.oracle.
373373

374374
## 高性能
375375

376-
### 数据库读写分离&分库分表
376+
### 数据库优化
377377

378-
[数据库读写分离和分库分表常见知识点&面试题总结](./docs/high-performance/read-and-write-separation-and-library-subtable.md)
378+
- [数据库读写分离和分库分表](./docs/high-performance/read-and-write-separation-and-library-subtable.md)
379+
- [常见 SQL 优化手段总结](./docs/high-performance/sql-optimization.md)
380+
- [深度分页介绍及优化建议](./docs/high-performance/deep-pagination-optimization.md)
379381

380382
### 负载均衡
381383

382384
[负载均衡常见知识点&面试题总结](./docs/high-performance/load-balancing.md)
383385

384-
### SQL 优化
385-
386-
[常见 SQL 优化手段总结](./docs/high-performance/sql-optimization.md)
387-
388386
### CDN
389387

390388
[CDN(内容分发网络)常见知识点&面试题总结](./docs/high-performance/cdn.md)

docs/.vuepress/sidebar/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ export default sidebar({
542542
children: [
543543
"read-and-write-separation-and-library-subtable",
544544
"sql-optimization",
545+
"deep-pagination-optimization",
545546
],
546547
},
547548
{

docs/.vuepress/theme.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default hopeTheme({
1212
logo: "/logo.png",
1313
favicon: "/favicon.ico",
1414

15-
iconAssets: "//at.alicdn.com/t/c/font_2922463_twrlnf18xy9.css",
15+
iconAssets: "//at.alicdn.com/t/c/font_2922463_o9q9dxmps9.css",
1616

1717
author: {
1818
name: "Guide",

docs/high-performance/cdn.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: CDN常见问题总结
2+
title: CDN工作原理详解
33
category: 高性能
44
head:
55
- - meta
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: 深度分页介绍及优化建议
3+
category: 高性能
4+
head:
5+
- - meta
6+
- name: keywords
7+
content: 读写分离,分库分表,主从复制
8+
- - meta
9+
- name: description
10+
content: 读写分离主要是为了将对数据库的读写操作分散到不同的数据库节点上。 这样的话,就能够小幅提升写性能,大幅提升读性能。 读写分离基于主从复制,MySQL 主从复制是依赖于 binlog 。分库就是将数据库中的数据分散到不同的数据库上。分表就是对单表的数据进行拆分,可以是垂直拆分,也可以是水平拆分。引入分库分表之后,需要系统解决事务、分布式 id、无法 join 操作问题。
11+
---
12+
13+
## 深度分页介绍
14+
15+
查询偏移量过大的场景我们称为深度分页,这会导致查询性能较低,例如:
16+
17+
```sql
18+
# MySQL 在无法利用索引的情况下跳过1000000条记录后,再获取10条记录
19+
SELECT * FROM t_order ORDER BY id LIMIT 1000000, 10
20+
```
21+
22+
## 深度分页优化建议
23+
24+
这里以 MySQL 数据库为例介绍一下如何优化深度分页。
25+
26+
### 范围查询
27+
28+
当可以保证 ID 的连续性时,根据 ID 范围进行分页是比较好的解决方案:
29+
30+
```sql
31+
# 查询指定 ID 范围的数据
32+
SELECT * FROM t_order WHERE id > 100000 AND id <= 100010 ORDER BY id
33+
# 也可以通过记录上次查询结果的最后一条记录的ID进行下一页的查询:
34+
SELECT * FROM t_order WHERE id > 100000 LIMIT 10
35+
```
36+
37+
这种优化方式限制比较大,且一般项目的 ID 也没办法保证完全连续。
38+
39+
### 子查询
40+
41+
我们先查询出 limit 第一个参数对应的主键值,再根据这个主键值再去过滤并 limit,这样效率会更快一些。
42+
43+
阿里巴巴《Java 开发手册》中也有对应的描述:
44+
45+
> 利用延迟关联或者子查询优化超多分页场景。
46+
>
47+
> ![](https://oss.javaguide.cn/github/javaguide/mysql/alibaba-java-development-handbook-paging.png)
48+
49+
```sql
50+
# 通过子查询来获取 id 的起始值,把 limit 1000000 的条件转移到子查询
51+
SELECT * FROM t_order WHERE id >= (SELECT id FROM t_order limit 1000000, 1) LIMIT 10;
52+
```
53+
54+
不过,子查询的结果会产生一张新表,会影响性能,应该尽量避免大量使用子查询。并且,这种方法只适用于 ID 是正序的。在复杂分页场景,往往需要通过过滤条件,筛选到符合条件的 ID,此时的 ID 是离散且不连续的。
55+
56+
当然,我们也可以利用子查询先去获取目标分页的 ID 集合,然后再根据 ID 集合获取内容,但这种写法非常繁琐,不如使用 INNER JOIN 延迟关联。
57+
58+
### INNER JOIN 延迟关联
59+
60+
延迟关联的优化思路,跟子查询的优化思路其实是一样的:都是把条件转移到主键索引树,然后减少回表。不同点是,延迟关联使用了 INNER JOIN 代替子查询。
61+
62+
```sql
63+
SELECT t1.* FROM t_order t1
64+
INNER JOIN (SELECT id FROM t_order limit 1000000, 1) t2
65+
ON t1.id >= t2.id
66+
LIMIT 10;
67+
```
68+
69+
### 覆盖索引
70+
71+
覆盖索引是指查询的结果可以直接从索引中获取,不需要回表查询。这样可以减少随机 IO 的次数,提高查询速度。
72+
73+
```sql
74+
# 如果只需要查询 id, code, type 这三列,可建立 code 和 type 的覆盖索引
75+
SELECT id, code, type, FROM t_order
76+
ORDER BY text
77+
LIMIT 1000000, 10;
78+
```
79+
80+
不过,当查询的结果集占表的总行数的很大一部分时,可能就不会走索引了,自动转换为全表扫描。当然了,也可以通过 `FORCE INDEX` 来强制查询优化器走索引,但这种提升效果一般不明显。
81+
82+
## 参考
83+
84+
- 聊聊如何解决 MySQL 深分页问题 - 捡田螺的小男孩:<https://juejin.cn/post/7012016858379321358>
85+
- 数据库深分页介绍及优化方案 - 京东零售技术:<https://mp.weixin.qq.com/s/ZEwGKvRCyvAgGlmeseAS7g>
86+
- MySQL深分页优化 - 得物技术:<https://juejin.cn/post/6985478936683610149>

docs/high-performance/load-balancing.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: 负载均衡常见问题总结
2+
title: 负载均衡原理及算法详解
33
category: 高性能
44
head:
55
- - meta

docs/high-performance/read-and-write-separation-and-library-subtable.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: 读写分离和分库分表常见问题总结
2+
title: 读写分离和分库分表详解
33
category: 高性能
44
head:
55
- - meta

docs/home.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -359,18 +359,16 @@ JVM 这部分内容主要参考 [JVM 虚拟机规范-Java8](https://docs.oracle.
359359

360360
## 高性能
361361

362-
### 数据库读写分离&分库分表
362+
### 数据库优化
363363

364-
[数据库读写分离和分库分表常见知识点&面试题总结](./high-performance/read-and-write-separation-and-library-subtable.md)
364+
- [数据库读写分离和分库分表](./high-performance/read-and-write-separation-and-library-subtable.md)
365+
- [常见 SQL 优化手段总结](./high-performance/sql-optimization.md)
366+
- [深度分页介绍及优化建议](./high-performance/deep-pagination-optimization.md)
365367

366368
### 负载均衡
367369

368370
[负载均衡常见知识点&面试题总结](./high-performance/load-balancing.md)
369371

370-
### SQL 优化
371-
372-
[常见 SQL 优化手段总结](./high-performance/sql-optimization.md)
373-
374372
### CDN
375373

376374
[CDN(内容分发网络)常见知识点&面试题总结](./high-performance/cdn.md)

docs/java/concurrent/java-concurrent-questions-03.md

+4-7
Original file line numberDiff line numberDiff line change
@@ -381,33 +381,30 @@ ExecutorService threadPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSiz
381381
**2、自己实现 `ThreadFactory`**
382382

383383
```java
384-
import java.util.concurrent.Executors;
385384
import java.util.concurrent.ThreadFactory;
386385
import java.util.concurrent.atomic.AtomicInteger;
386+
387387
/**
388388
* 线程工厂,它设置线程名称,有利于我们定位问题。
389389
*/
390390
public final class NamingThreadFactory implements ThreadFactory {
391391

392392
private final AtomicInteger threadNum = new AtomicInteger();
393-
private final ThreadFactory delegate;
394393
private final String name;
395394

396395
/**
397396
* 创建一个带名字的线程池生产工厂
398397
*/
399-
public NamingThreadFactory(ThreadFactory delegate, String name) {
400-
this.delegate = delegate;
401-
this.name = name; // TODO consider uniquifying this
398+
public NamingThreadFactory(String name) {
399+
this.name = name;
402400
}
403401

404402
@Override
405403
public Thread newThread(Runnable r) {
406-
Thread t = delegate.newThread(r);
404+
Thread t = new Thread(r);
407405
t.setName(name + " [#" + threadNum.incrementAndGet() + "]");
408406
return t;
409407
}
410-
411408
}
412409
```
413410

0 commit comments

Comments
 (0)