MySQL 并不是跳过 offset 行,而是取 offset+N 行,然后返回放弃前 offset 行,返回 N 行。

1
2
3
4
5
6
7
8
9
# 反例(耗时129.570s)
select * from task_result LIMIT 20000000, 10;

# 正例(耗时5.114s)
SELECT a.* FROM task_result a, (select id from task_result LIMIT 20000000, 10) b where a.id = b.id;

# 说明
task_result表为生产环境的一个表,总数据量为3400万,id为主键,偏移量达到2000万

# 获取一条数据时的 Limit 1

在很多情况下我们已知数据仅存在一条,此时我们应该告知数据库只用查一条,否则将会转化为全表扫描

1
2
3
4
5
6
7
8
9
# 反例(耗时2424.612s)
select * from task_result where unique_key = 'ebbf420b65d95573db7669f21fa3be3e_861414030800727_48';

# 正例(耗时1.036s)
select * from task_result where unique_key = 'ebbf420b65d95573db7669f21fa3be3e_861414030800727_48' LIMIT 1;

# 说明
task_result表为生产环境的一个表,总数据量为3400万,where条件非索引字段,数据所在行为第19486条记录

# 批量插入

1
2
3
4
5
6
7
8
9
10
11
# 反例
INSERT into person(name,age) values('A',24)
INSERT into person(name,age) values('B',24)
INSERT into person(name,age) values('C',24)

# 正例
INSERT into person(name,age) values('A',24),('B',24),('C',24);

# 说明
比较常规,就不多做说明了

https://juejin.im/post/5ea16dede51d45470b4ffc5b?utm_source=gold_browser_extension

阅读次数

请我喝[茶]~( ̄▽ ̄)~*

有李说不清 微信支付

微信支付

有李说不清 支付宝

支付宝

有李说不清 贝宝

贝宝