MySQL:Innodb:innodb_flush_log_at_trx_commit参数影响的位置


  • 源码5.7.22
  • 影响阶段:MYSQL_BIN_LOG::ordered_commit 的flush阶段

一、影响的函数

innobase_flush_logs函数,函数如下其实注释也写得很清楚了。binlog_group_flush 参数为1。

/** Flush InnoDB redo logs to the file system.
@param[in]    hton            InnoDB handlerton
@param[in]    binlog_group_flush    true if we got invoked by binlog
group commit during flush stage, false in other cases.
@return false */
static
bool
innobase_flush_logs(
    handlerton*    hton,
    bool        binlog_group_flush)
{
    DBUG_ENTER("innobase_flush_logs");
    DBUG_ASSERT(hton == innodb_hton_ptr);

    if (srv_read_only_mode) {
        DBUG_RETURN(false);
    }

    /* If !binlog_group_flush, we got invoked by FLUSH LOGS or similar.
    Else, we got invoked by binlog group commit during flush stage. */

    if (binlog_group_flush && thd_flush_log_at_trx_commit(NULL) == 0) {
        /* innodb_flush_log_at_trx_commit=0
        (write and sync once per second).
        Do not flush the redo log during binlog group commit. */
        DBUG_RETURN(false);
    }

    /* Flush the redo log buffer to the redo log file.
    Sync it to disc if we are in FLUSH LOGS, or if
    innodb_flush_log_at_trx_commit=1
    (write and sync at each commit). */
    log_buffer_flush_to_disk(!binlog_group_flush
                 || thd_flush_log_at_trx_commit(NULL) == 1); 

    DBUG_RETURN(false);
}

二、可能的组合

  • innodb_flush_log_at_trx_commit参数为 0
    binlog_group_flush && thd_flush_log_at_trx_commit(NULL) == 0 条件成立,因此直接return了,那么这种情况下log_buffer_flush_to_disk函数不会调用,因此不会做redo刷盘。依赖master线程。
  • innodb_flush_log_at_trx_commit参数为 1
    !binlog_group_flush|| thd_flush_log_at_trx_commit(NULL) == 1 返回为1即为True,因此调用log_buffer_flush_to_disk(True),因此需要做redo刷盘,也要做sync。
  • innodb_flush_log_at_trx_commit参数为 2
    !binlog_group_flush|| thd_flush_log_at_trx_commit(NULL) == 1 返回为0即为Flase,因此调用

log_buffer_flush_to_disk(Flase),因此需要做redo刷盘,不做sync。依赖OS的刷盘机制。

三、调用栈帧

#0  innobase_flush_logs (hton=0x2e9edd0, binlog_group_flush=true) at /root/mysql5.7.14/percona-server-5.7.14-7/storage/innobase/handler/ha_innodb.cc:4385
#1  0x0000000000f65893 in flush_handlerton (thd=0x0, plugin=0x7ffff01558e8, arg=0x7ffff0155944) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:2606
#2  0x00000000015d7716 in plugin_foreach_with_mask (thd=0x0, func=0xf65835 <flush_handlerton(THD*, plugin_ref, void*)>, type=1, state_mask=4294967287, 
    arg=0x7ffff0155944) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_plugin.cc:2318
#3  0x0000000000f658ef in ha_flush_logs (db_type=0x0, binlog_group_flush=true) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:2617
#4  0x000000000185733d in MYSQL_BIN_LOG::process_flush_stage_queue (this=0x2e01c80, total_bytes_var=0x7ffff0155a88, rotate_var=0x7ffff0155a87, 
    out_queue_var=0x7ffff0155a78) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8541
#5  0x000000000185899f in MYSQL_BIN_LOG::ordered_commit (this=0x2e01c80, thd=0x7fff2c000b70, all=false, skip_commit=false)
    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:9189
#6  0x000000000185700c in MYSQL_BIN_LOG::commit (this=0x2e01c80, thd=0x7fff2c000b70, all=false) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/binlog.cc:8440
#7  0x0000000000f63df8 in ha_commit_trans (thd=0x7fff2c000b70, all=false, ignore_global_read_lock=false)
    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/handler.cc:1818
#8  0x00000000016b3e5d in trans_commit_stmt (thd=0x7fff2c000b70) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/transaction.cc:458
#9  0x00000000015ac677 in mysql_execute_command (thd=0x7fff2c000b70, first_level=true) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5239
#10 0x00000000015adcd6 in mysql_parse (thd=0x7fff2c000b70, parser_state=0x7ffff0158600) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:5836
#11 0x00000000015a1b95 in dispatch_command (thd=0x7fff2c000b70, com_data=0x7ffff0158d70, command=COM_QUERY)
    at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1447
#12 0x00000000015a09c6 in do_command (thd=0x7fff2c000b70) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/sql_parse.cc:1010
#13 0x00000000016e29d0 in handle_connection (arg=0x3803f30) at /root/mysql5.7.14/percona-server-5.7.14-7/sql/conn_handler/connection_handler_per_thread.cc:312
#14 0x0000000001d7b4b0 in pfs_spawn_thread (arg=0x3810b80) at /root/mysql5.7.14/percona-server-5.7.14-7/storage/perfschema/pfs.cc:2188
#15 0x0000003f74807aa1 in start_thread () from /lib64/libpthread.so.0
#16 0x0000003f740e8bcd in clone () from /lib64/libc.so.6

作者微信:gaopp_22389860

### 回答1: innodb_flush_log_at_trx_commitMySQLInnoDB 引擎的一个参数。它控制事务提交时日志缓冲区的刷新频率。当该参数设置为 1 时,每次事务提交都会将日志缓冲区刷新到磁盘上,这样可以保证事务的 ACID 特性。如果该参数设置为 0 或 2,则日志缓冲区可能不会立即刷新到磁盘上,这样可以提高性能,但会增加事务数据丢失的风险。 ### 回答2: innodb_flush_log_at_trx_commit 参数用于控制InnoDB存储引擎的日志刷新策略。其含义如下: 1. 参数值为0:表示将日志缓存在内存中,每秒将日志批量刷新到磁盘上。这种设置可以提高写入性能,但在断电等意外情况下可能会造成数据丢失。 2. 参数值为1:表示每次事务提交时都将日志立即刷新到磁盘上。这种设置可以保证在发生故障时不会丢失任何事务的数据,但会对性能产生一定的影响。 3. 参数值为2:表示将日志缓存在内存中,每秒将日志立即刷新到磁盘上。这种设置可以兼顾写入性能和数据的持久性,是默认的值,也是推荐的配置。 当需要强制保证数据完整性和一致性时,可以将该参数设置为1。而在一些对数据实时性要求较高的场景下,可以将该参数设置为0以提高写入性能,但这样会增加数据丢失的风险。对于一般的应用场景,推荐使用默认值2。 需要注意的是,innodb_flush_log_at_trx_commit 参数对事务日志的刷盘行为进行控制,而不是控制数据的持久化。InnoDB存储引擎通过redo log来保证事务的持久性,而innodb_flush_log_at_trx_commit参数决定了什么时候将日志持久化到磁盘,从而影响了事务的持久性保证和写入性能。 ### 回答3: innodb_flush_log_at_trx_commitMySQL InnoDB存储引擎的一个参数innodb_flush_log_at_trx_commit参数的含义是控制InnoDB存储引擎在事务提交时将事务日志(redo log)写入磁盘的时机。 该参数有三个可选值: 1. 0:表示事务提交时不立即将事务日志写入磁盘,而是将日志写入操作系统缓存,然后由操作系统根据自己的策略异步写入磁盘。这种方式具有最高的性能,但最小的数据持久性,即在数据库崩溃时可能会丢失一部分事务数据。 2. 1:表示默认值,事务提交时将事务日志立即写入磁盘。这种方式具有较高的数据持久性,即数据库崩溃时只会丢失最后一次提交的事务数据。但写入磁盘的操作会增加IO的开销,可能会影响系统的性能。 3. 2:表示事务提交时将事务日志先写入操作系统缓存,并标记为需要每秒写入磁盘(写back校对点),然后由后台线程按照每秒一次的频率将缓存中的日志写入磁盘。这种方式牺牲了一部分数据的持久性,但在性能和数据持久性之间做了平衡。 根据应用的需求和对数据持久性的要求,可以调整innodb_flush_log_at_trx_commit参数的值,以达到最佳的性能和数据安全性的平衡。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值