当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL为什么要避免大事务以及大事务解决的方法

MySQL为什么要避免大事务以及大事务解决的方法

2020年08月29日  | 移动技术网IT编程  | 我要评论
什么是大事务运行时间比较长,长时间未提交的事务就可以称为大事务大事务产生的原因 操作的数据比较多 大量的锁竞争 事务中有其他非db的耗时操作 。。。 大事务造成的影响 并发情况下,数据库连

什么是大事务

运行时间比较长,长时间未提交的事务就可以称为大事务

大事务产生的原因

  • 操作的数据比较多
  • 大量的锁竞争
  • 事务中有其他非db的耗时操作
  • 。。。

大事务造成的影响

  • 并发情况下,数据库连接池容易被撑爆
  • 锁定太多的数据,造成大量的阻塞和锁超时
  • 执行时间长,容易造成主从延迟
  • 回滚所需要的时间比较长
  • undo log膨胀
  • 。。。

如何查询大事务

**注**:本文的sql的操作都是基于mysql5.7版本

以查询执行时间超过10秒的事务为例:

select \* from information\_schema.innodb\_trx where time\_to\_sec(timediff(now(),trx\_started))>10

如何避免大事务

通用解法

  • 在一个事务里面, 避免一次处理太多数据
  • 在一个事务里面,尽量避免不必要的查询
  • 在一个事务里面, 避免耗时太多的操作,造成事务超时。一些非db的操作,比如rpc调用,消息队列的操作尽量放到事务之外操作

基于mysql5.7的解法

  • 在innodb事务中,行锁是在需要的时候才加上的,但并不是不需要了就立刻释放,而是要等到事务结束时才释放。**如果你的事务中需要锁多个行,要把最可能造成锁冲突、最可能影响并发度的锁尽量往后放**
  • 通过setmax_execution_time命令, 来控制每个语句查询的最长时间,避免单个语句意外查询太长时间
  • 监控 information_schema.innodb_trx表,设置长事务阈值,超过就报警/或者kill
  • 在业务功能测试阶段要求输出所有的general_log,分析日志行为提前发现问题
  • 设置innodb_undo_tablespaces值,将undo log分离到独立的表空间。如果真的出现大事务导致回滚段过大,这样设置后清理起来更方便

附录查询事务相关语句

**注**:sql语句都是基于mysql5.7版本

# 查询所有正在运行的事务及运行时间

select t.\*,to\_seconds(now())-to\_seconds(t.trx\_started) idle\_time from information\_schema.innodb\_trx t



# 查询事务详细信息及执行的sql

select now(),(unix\_timestamp(now()) - unix\_timestamp(a.trx\_started)) diff\_sec,b.id,b.user,b.host,b.db,d.sql\_text from information\_schema.innodb\_trx a inner join information\_schema.processlist b

on a.trx\_mysql\_thread\_id=b.id and b.command = 'sleep'

inner join performance\_schema.threads c on b.id = c.processlist\_id

inner join performance\_schema.events\_statements\_current d on d.thread\_id = c.thread\_id;



# 查询事务执行过的所有历史sql记录

select

 ps.id 'process id',

 ps.user,

 ps.host,

 esh.event\_id,

 trx.trx\_started,

 esh.event\_name 'event name',

 esh.sql\_text 'sql',

 ps.time 

from

 performance\_schema.events\_statements\_history esh

 join performance\_schema.threads th on esh.thread\_id = th.thread\_id

 join information\_schema.processlist ps on ps.id = th.processlist\_id

 left join information\_schema.innodb\_trx trx on trx.trx\_mysql\_thread\_id = ps.id 

where

 trx.trx\_id is not null 

 and ps.user != 'system\_user' 

order by

 esh.event\_id;

 

 # 简单查询事务锁

 select \* from sys.innodb\_lock\_waits

 

 # 查询事务锁详细信息

 select

 tmp.\*,

 c.sql\_text blocking\_sql\_text,

 p.host blocking\_host 

from

 (

 select

 r.trx\_state wating\_trx\_state,

 r.trx\_id waiting\_trx\_id,

 r.trx\_mysql\_thread\_id waiting\_thread,

 r.trx\_query waiting\_query,

 b.trx\_state blocking\_trx\_state,

 b.trx\_id blocking\_trx\_id,

 b.trx\_mysql\_thread\_id blocking\_thread,

 b.trx\_query blocking\_query 

 from

 information\_schema.innodb\_lock\_waits w

 inner join information\_schema.innodb\_trx b on b.trx\_id = w.blocking\_trx\_id

 inner join information\_schema.innodb\_trx r on r.trx\_id = w.requesting\_trx\_id 

 ) tmp,

 information\_schema.processlist p,

 performance\_schema.events\_statements\_current c,

 performance\_schema.threads t 

where

 tmp.blocking\_thread = p.id 

 and t.thread\_id = c.thread\_id 

 and t.processlist\_id = p.id 

以上就是mysql避免大事务以及大事务解决的方法的详细内容,更多关于mysql 大事务的资料请关注移动技术网其它相关文章!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网