阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

InnoDB事务日志(redo log 和 undo log)详解

210次阅读
没有评论

共计 3613 个字符,预计需要花费 10 分钟才能阅读完成。

为了最大程度避免数据写入时 io 瓶颈带来的性能问题,MySQL 采用了这样一种缓存机制:当 query 修改数据库内数据时,InnoDB 先将该数据从磁盘读取到内存中,修改内存中的数据拷贝,并将该修改行为持久化到磁盘上的事务日志(先写 redo log buffer,再定期批量写入),而不是每次都直接将修改过的数据记录到硬盘内,等事务日志持久化完成之后,内存中的脏数据可以慢慢刷回磁盘,称之为 Write-Ahead Logging。事务日志采用的是追加写入,顺序 io 会带来更好的性能优势。

为了避免脏数据刷回磁盘过程中,掉电或系统故障带来的数据丢失问题,InnoDB 采用事务日志(redo log)来解决该问题。

【redo log】

用于在实例故障恢复时,继续那些已经 commit 但数据尚未完全回写到磁盘的事务。

The redo log is a disk-based data structure used during crash recovery to correct data written by incomplete transactions. 

通常会初始化 2 个或更多的 ib_logfile 存储 redo log,由参数 innodb_log_files_in_group 确定个数,命名从 ib_logfile0 开始,依次写满 ib_logfile 并顺序重用(in a circular fashion)。如果最后 1 个 ib_logfile 被写满,而第一个 ib_logfile 中所有记录的事务对数据的变更已经被持久化到磁盘中,将清空并重用之。

在写入量比较高的情况下,redo log 的大小将显著地影响写入性能。

innodb_log_file_size 用来控制 ib_logfile 的大小。5.5 版本及以前:默认 5MB,最大 4G。

所有事务日志累加大小不超过 4G,事务日志过大,checkpoint 会减少,在节省磁盘 io 的同时,大的事务日志也意味着数据库 crash 后,恢复起来较慢。

官方文档建议 innodb_log_file_size 的设置可参考 show engine innodb status \G

Log sequence number -Last checkpoint < (innodb_log_files_in_group * innodb_log_file_size) * 0.75

【undo log】

记录了数据修改的前镜像。存放于 ibdata 中。(提一句,DDL 操作会修改数据字典,该信息也存放在 ibdata 中)

用于在实例故障恢复时,借助 undo log 将尚未 commit 的事务,回滚到事务开始前的状态。

An undo log is a collection of undo log records associated with a single transaction. An undo log record contains information about how to undo the latest change by a transaction to a clustered index record. If another transaction needs to see the original data (as part of a consistent read operation), the unmodified data is retrieved from the undo log records. Undo logs exist within undo log segments, which are contained within rollback segments. Rollback segments are physically part of the system tablespace. For related information, see Section 14.6,“InnoDB Multi-Versioning”.

The InnoDB crash recovery process consists of several steps:(指的是 MySQL server crash,不包括 hardware failure 或者 server system error)

Redo log application
Redo log application is the first step and is performed during initialization, before accepting any connections. If all changes are flushed from the buffer pool to the tablespaces (ibdata* and *.ibd files) at the time of the shutdown or crash, redo log application is skipped. InnoDB also skips redo log application if redo log files are missing at startup.

Removing redo logs to speed up recovery is not recommended, even if some data loss is acceptable. Removing redo logs should only be considered after a clean shutdown, with innodb_fast_shutdown set to 0 or 1.

Roll back of incomplete transactions
Incomplete transactions are any transactions that were active at the time of crash or fast shutdown. The time it takes to roll back an incomplete transaction can be three or four times the amount of time a transaction is active before it is interrupted, depending on server load.

You cannot cancel transactions that are being rolled back. In extreme cases, when rolling back transactions is expected to take an exceptionally long time, it may be faster to start InnoDB with an innodb_force_recovery setting of 3 or greater. See Section 14.23.2,“Forcing InnoDB Recovery”.

Change buffer merge
Applying changes from the change buffer (part of the system tablespace) to leaf pages of secondary indexes, as the index pages are read to the buffer pool.

Purge
Deleting delete-marked records that are no longer visible to active transactions.

The steps that follow redo log application do not depend on the redo log (other than for logging the writes) and are performed in parallel with normal processing. Of these, only rollback of incomplete transactions is special to crash recovery. The insert buffer merge and the purge are performed during normal processing.

After redo log application, InnoDB attempts to accept connections as early as possible, to reduce downtime. As part of crash recovery, InnoDB rolls back transactions that were not committed or in XA PREPARE state when the server crashed. The rollback is performed by a background thread, executed in parallel with transactions from new connections. Until the rollback operation is completed, new connections may encounter locking conflicts with recovered transactions.

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2018-01/150489.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计3613字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中