File tree 1 file changed +14
-0
lines changed
1 file changed +14
-0
lines changed Original file line number Diff line number Diff line change 18
18
# MySQL 在无法利用索引的情况下跳过1000000条记录后,再获取10条记录
19
19
SELECT * FROM t_order ORDER BY id LIMIT 1000000 , 10
20
20
```
21
+ ## 深度分页问题的原因
22
+ ** 全表扫描** :当OFFSET值较大时,MySQL可能会选择执行全表扫描而不是使用索引。
23
+ ![ image] ( https://github.com/user-attachments/assets/d2537428-74db-4eba-bd1b-20b0ef681b8e )
24
+ ![ image] ( https://github.com/user-attachments/assets/00467d02-b5bd-4241-8145-acded334b76a )
25
+
26
+ 具体的临界点每个机器不一样,我的机器上是5980,为什么产生呢?
27
+ ![ image] ( https://github.com/user-attachments/assets/19bb5403-398b-4bff-934a-7db2e31995aa )
28
+ ![ image] ( https://github.com/user-attachments/assets/d01a5b84-a47e-4ddd-966d-520cc3d3b3bd )
29
+ MySQL数据库的查询优化器是采用了基于代价的,而查询代价的估算是基于CPU代价和IO代价。
30
+ 如果MySQL在查询代价估算中,认为全表扫描方式比走索引扫描的方式效率更高的话,就会放弃索引,直接全表扫描。
31
+ 这就是为什么在大分页的SQL查询中,明明给该字段加了索引,但是MySQL却走了全表扫描的原因。
32
+
33
+
34
+
21
35
22
36
## 深度分页优化建议
23
37
You can’t perform that action at this time.
0 commit comments