千家信息网

MySQL中Innodb关于Handler_commit每次DML增加2的原因是什么

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容主要讲解"MySQL中Innodb关于Handler_commit每次DML增加2的原因是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"MyS
千家信息网最后更新 2026年11月01日MySQL中Innodb关于Handler_commit每次DML增加2的原因是什么

本篇内容主要讲解"MySQL中Innodb关于Handler_commit每次DML增加2的原因是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"MySQL中Innodb关于Handler_commit每次DML增加2的原因是什么"吧!

请教一个问题。我每次insert一条语句,查询show global status like 'Handler_commit'; 发现每次增加值是2,难道不应该是1吗?最简单的insert into table a values(1);

一、问题展示

语句如下:

mysql> flush status;Query OK, 0 rows affected (0.10 sec)mysql> set  sql_log_bin=1;Query OK, 0 rows affected (0.01 sec)mysql> insert into testm values(16,'gaopeng',34);Query OK, 1 row affected (0.15 sec)mysql> show status like '%commit%';+----------------+-------+| Variable_name  | Value |+----------------+-------+| Com_commit     | 0     || Com_xa_commit  | 0     || Handler_commit | 2     |+----------------+-------+3 rows in set (0.01 sec)

问为什么 Handler_commit是2而不是1。

二、原因分析

其实对于这个问题只要看看这个Handler_commit指标增加的方式就可以看出原因。实际上这个指标出现在ha_commit_low函数中如下:

for (; ha_info; ha_info= ha_info_next)    {      int err;      handlerton *ht= ha_info->ht();      if ((err= ht->commit(ht, thd, all)))      {        my_error(ER_ERROR_DURING_COMMIT, MYF(0), err);        error=1;      }      DBUG_ASSERT(!thd->status_var_aggregated);      thd->status_var.ha_commit_count++; //此处增加      ha_info_next= ha_info->next();      if (restore_backup_ha_data)        reattach_engine_ha_data_to_thd(thd, ht);      ha_info->reset(); /* keep it conveniently zero-filled */    }

可以清楚的看到ha_commit_count实际就是调用ht->commit的次数,由于有多个Handler的存在,因此这里需要调用多次。对于开启binlog+innodb的这种结构来讲分别要做:

  • binlog的commit

  • Innodb的commit

后面会看到实际binlog的commit什么都没做,但是这是一种协议。
那么如果我们关闭binlog可以发现Handler_commit为1了如下:

mysql> set  sql_log_bin=0;Query OK, 0 rows affected (0.00 sec)mysql> insert into testm values(15,'gaopeng',34);Query OK, 1 row affected (0.10 sec)mysql>  show status like '%commit%';+----------------+-------+| Variable_name  | Value |+----------------+-------+| Com_commit     | 0     || Com_xa_commit  | 0     || Handler_commit | 1     |+----------------+-------+3 rows in set (0.01 sec)

三、binlog commit栈帧

#0  binlog_commit (hton=0x3485e30, thd=0x7fff2c014430, all=false) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:1833#1  0x0000000000f64104 in ha_commit_low (thd=0x7fff2c014430, all=false, run_after_commit=false) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1923#2  0x000000000185772b in MYSQL_BIN_LOG::process_commit_stage_queue (this=0x2e01c80, thd=0x7fff2c014430, first=0x7fff2c014430)    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8647#3  0x0000000001858f5d in MYSQL_BIN_LOG::ordered_commit (this=0x2e01c80, thd=0x7fff2c014430, all=false, skip_commit=false)    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:9318#4  0x000000000185700c in MYSQL_BIN_LOG::commit (this=0x2e01c80, thd=0x7fff2c014430, all=false) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8440#5  0x0000000000f63df8 in ha_commit_trans (thd=0x7fff2c014430, all=false, ignore_global_read_lock=false)    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1818

但是实际上binlog_commit什么都没做,因为在此之前他已经做完了需要做的事情比如flush、sync等

static int binlog_commit(handlerton *hton, THD *thd, bool all){  DBUG_ENTER("binlog_commit");  /*    Nothing to do (any more) on commit.   */  DBUG_RETURN(0);}

四、Innodb commit接口

#0  innobase_commit (hton=0x2e9edd0, thd=0x7fff2c014430, commit_trx=false) at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:4652#1  0x0000000000f64104 in ha_commit_low (thd=0x7fff2c014430, all=false, run_after_commit=false) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1923#2  0x000000000185772b in MYSQL_BIN_LOG::process_commit_stage_queue (this=0x2e01c80, thd=0x7fff2c014430, first=0x7fff2c014430)    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8647#3  0x0000000001858f5d in MYSQL_BIN_LOG::ordered_commit (this=0x2e01c80, thd=0x7fff2c014430, all=false, skip_commit=false)    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:9318#4  0x000000000185700c in MYSQL_BIN_LOG::commit (this=0x2e01c80, thd=0x7fff2c014430, all=false) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8440#5  0x0000000000f63df8 in ha_commit_trans (thd=0x7fff2c014430, all=false, ignore_global_read_lock=false)    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1818

实际上innodb comit才是需要真正做的,这里包含一些事情要做,比如事物状态的改变,资源的释放。

最后select也会增加Handler_commit,增加为1。

到此,相信大家对"MySQL中Innodb关于Handler_commit每次DML增加2的原因是什么"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

实际 原因 实际上 问题 事情 内容 指标 语句 学习 查询 实用 更深 清楚 事物 兴趣 函数 原因分析 增加值 多个 完了 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 花魁咖啡 博彦科技互联网金融 湖南永感互联网科技有限公司 A股互联网金融科技公司 疫情对互联网科技公司影响 腾讯阿里互联网科技 科技与互联网发展 互联网女皇 腾讯科技 互联网科技大会案例 字节跳动和互联网科技 昭通云南众福互联网科技有限公司 重庆互联网医院沙龙医盟信息科技 语蝶互联网科技有限公司 中国航天科技大学国际工业互联网 新兴科技互联网创新大赛颁奖典礼 西安小鹰速贷互联网科技有限公司 黑科技 体验场景 互联网大会 九思连强互联网电子科技有限公司 互联网科技企业商标注意事项 第三届互联网大会安防黑科技 耶加雪菲 中国互联网科技工作委员会毛军 互联网科技行业发展 北京互联网科技有限公司 互联网科技排名 华服互联网科技官网 科技类公司包括互联网吗 科技互联网海报 互联网营销推广黑科技 现代互联网科技公司
0