mysql慢查询记录(一)

(一)导致慢查询的操作

1.sql加for update锁
同样的查询条件,加锁会比不加锁慢100ms左右
表里总数据量180w,task_id=197的有35w,task_id建立了索引

sql语句是否加锁查询时长
SELECT * FROM picquery WHERE (state = 1 AND task_id = 197) ORDER BY id ASC LIMIT 1;0.331
SELECT * FROM picquery WHERE (state = 1 AND task_id = 197) ORDER BY id ASC LIMIT 1 for update;0.438

for update锁必须在事务中才会生效,下面是详细使用情况

事务A事务B是否阻塞锁表还是锁行
begin; select * from picquery where task_id=197 limit 1 for update;begin; select * from picquery where task_id=197 limit 1;不阻塞锁行
begin; select * from picquery where task_id=197 limit 1 for update;begin; select * from picquery where task_id=197 limit 1 for update;阻塞,需要等事务A提交之后,才能查询锁行
begin; select * from picquery where task_id=197 limit 1 for update;begin; select * from picquery where task_id=29 limit 1 for update;不阻塞,因为锁的是不同的行锁行
begin; select * from picquery where weight=1 limit 1 for update;begin; select * from picquery where weight=1 limit 1 for update;阻塞锁表
begin; select * from picquery where weight=1 limit 1 for update;begin; select * from picquery where weight=2 limit 1 for update;阻塞锁表
  1. count()慢
    count(
    )完全走索引就会很快,不完全走索引就会很慢
    表里总数据量180w,task_id=209的有100w,task_id建立了索引
sql语句是否完全走索引查询时长
SELECT count(*) from engine_picquery WHERE task_id=209;0.289
SELECT count(*) from engine_picquery WHERE task_id=209 and is_delete=False;1.565

3.sql中有混合排序
表里总数据量180w,task_id=209的有100w,task_id建立了索引

sql语句排序情况查询时长
SELECT * FROM picquery WHERE state = 1 AND task_id = 209 ORDER BY weight DESC, id ASC LIMIT 1;混合排序2.610
SELECT * FROM picquery WHERE state = 1 AND task_id = 209 ORDER BY id ASC LIMIT 1;没有混合排序0.057
SELECT * FROM picquery WHERE state = 1 AND task_id = 209 ORDER BY weight ASC LIMIT 1;没有混合排序,按非主键字段排序,且此字段上无索引2.911

(二)待证实会导致慢查询的操作

1.字段类型转换会导致慢查询
表里总数据量180w,task_id=209的有100w,task_id建立了索引

sql语句是否有类型转换查询时长
SELECT * FROM picquery WHERE state = 1 AND task_id = 209 ORDER BY id ASC LIMIT 1;0.057
SELECT * FROM picquery WHERE state = “1” AND task_id = “209” ORDER BY id ASC LIMIT 1;0.057
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值