12345678910 | # 1 SELECT id, name, price FROM products WHERE id IN (1, 2, 3, 4, ... 30000); # (1-2s)# 2. 将第一条查询转换成 30000 条语句SELECT id, name, price FROM products WHERE id = 1;...SELECT id, name, price FROM products WHERE id = 30000; # (2-3s)# 3. 将第一条转换成 OR 语句SELECT id, name, price FROM products WHERE id = 1 OR id = 2 OR ... OR id = 30000; # (8-10s) |
1 | SELECT * FROM t WHERE id = ?; |
1 | SELECT * FROM t WHERE name = ?; |
1 | content varchar(2046) NOT NULL COMMENT '原始消息'; |
用了缓存并不一定代表没有问题
降低扫描数据量照旧降低排序代价大多数查询只能使用一个索引, 因此在需要对多个列举行操作的 SQL 语句中, 我们需要准确评估每个索引的开销.
1234 | # name varchar(512)# ket idx_name(name(100))SELECT * FROM comment WHERE name >= 'destiny' ORDER BY name ASC LIMIT 100; |
123456 | -- uid varchar(190) NOT NULL DEFAULT '' COMMENT '用户 id',-- score bigint(20) NOT NULL DEFAULT '0' COMMENT '变动分值, 正增, 负减',-- primary key ID-- KEY idx_uid(uid)SELECT SUM(score) FROM name WHERE uid = '5993156' |
12345 | BEGIN;SELECT count FROM tb WHERE id = ? FOR UPDATE;-- do sthUPDATE tb SET count = count - ? WHERE id = ?;COMMIT; |
12345 | BEGIN;SELECT count FROM tb WHERE id = ?;UPDATE tb SET count = count - ? WHERE id = ? AND count = :count;COMMIT;-- do sth |
如果业务服务器将数据写入到主库后举行读取, 此时读操作访问从库, 而主库的数据没有完全复制过来, 从库是无法读取到最新数据的.办理方案:
欢迎光临 创意电子 (https://wxcydz.cc/) | Powered by Discuz! X3.4 |