Transaction
确保事务内的SQL都可以同步执行 要么一起成功 要么一起失败。事务有四个特性原子性 一致性,隔离性,持久性
Implementation method
- Write data to InnoDB buffer pool plus exclusive lock
- Writes undo information to the rollback segment of the Undo table for use when rolling back data
- Change the data in the cached page and write the update record to redo buffer
- The data in the redo buffer is flushed to the Redo log file and the exclusive lock is released in a different manner depending on the settings of the Innodb_flush_log_at_trx_commit at the time of submission
- Finally, the background IO thread refreshes the data in the cache to disk
Innodb_flush_log_at_trx_commit
默认设置为1 既在每次事务提交的时候 都会将缓冲池中的数据写入到日志文件中 并立即调用系统fsync 刷新日志文件到磁盘设置为0 每秒钟执行数据写入日志并调用系统fsync设置为2 只在事务提交的时候 属性buffer中的redo 数据写入日志文件中 但是将日志文件写入到磁盘 则由系统配置确认
Innodb_log_buffer_size
Redo Log Redo Journal Buffering
主要是解决 提交的事务没有执行完成但是数据库奔溃了,当数据库恢复之后,可以完整的恢复数据。InnoDB存储引擎会首先将重做日志信息放到这个缓冲区 redo log buffer,然后按照不同的策略和频率将buffer中的数据刷新到重做日志中。redo log 在磁盘中保存的名称为 ib_logfile0 and ib_logfile1
Checkpoing Technology
当事务提交时 需要先写重做日志 然后再修改页。如果数据库由于未知原因崩溃 而导致数据丢失的时候 需要通过重做日志来完成当数据库发生宕机的时候 数据的恢复 不需要重做所有的日志 这个时候就用到checkpoint。数据恢复的时候 只要恢复Checkpoint这个点,之后的数据即可。对于InnoDB存储引擎来来说 有一个LSN的数字 来标记版本的
Database concurrency
相对于串行处理 支持并发可以提高数据库的利用率 但是数据库并发可能会带来下面的问题
- Updates lost such as the same data being updated by different things cause a transaction update error
- Dirty read transaction updated data is not committed and read by another transaction is likely to be dirty data
- Non-repeatable reading of data before and after two times the data is changed by another transaction resulting in inconsistent data
- Phantom read resolves non-repeatable reads but if another transaction commits new data at the same time
InnoDB Isolation Level
MariaDB [(none)]> show variables like "%tx_isolation%";+---------------+-----------------+| Variable_name | Value |+---------------+-----------------+| tx_isolation | REPEATABLE-READ |+---------------+-----------------+
在同一个事务内的查询 与事务开始时刻的数据 保持一致 ,但是存在幻读
Phantom Read Demo
Session1 Open Transaction
mysql> begin;Query OK, 0 rows affectedmysql> select * from phone; ##事务开始的时候 电话是123456的数据只有一条数据+----+----------+| id | phone |+----+----------+| 3 | 123456 |+----+----------+
Session2 the same result if the transaction is added
## 这里插入一条新的数据 电话也是 123456INSERT INTO `tmp`.`phone` (`id`, `phone`) VALUES (‘2‘, ‘123456‘);
Session1
mysql> select * from phone; ##这里再次读取数据 和事务开始的时候 数据相同 电话是123456的数据只有一条数据+----+----------+| id | phone |+----+----------+| 3 | 123456 |+----+----------+## 这里更新数据的时候 显示有2行数据被更新了mysql> update phone set phone=111111 where phone=123456;Query OK, 2 rows affectedRows matched: 2 Changed: 2 ## 这个时候 数据库有两条数据 被更新成111111了 出现了幻读mysql> select * from phone;+----+----------+| id | phone |+----+----------+| 2 | 111111 || 3 | 111111 |
Resolution Lock
Session1 open a transaction and lock it through the For Update method
mysql> begin;mysql> select * from phone for update;
Session2 Open a transaction
mysql> INSERT INTO `tmp`.`phone` (`id`, `phone`) VALUES (‘2‘, ‘123456‘);1205 - Lock wait timeout exceeded; try restarting transaction## 锁等待超时 退出事务
MVCC
多版本并发控制协议 优点是 读不加锁 读写不冲突。InnoDB通过Undo Log实现了数据的多版本。而并发控制则是通过锁来实现。读操作可以分为两种 一种是快照读另外一种是当前读快照读:读取的是记录的可见版本 也有可能是历史版本 不用加锁。当前读:读取的是记录的最新版本InnoDB实现的MVVC没有解决幻读 可以通过加锁的方式解决这个问题
InnoDB Buffer Pool
参考博客 :https://michael.bouvy.net/blog/en/2015/01/18/understanding-mysql-innodb-buffer-pool-size/
View the types and sizes of data contained
SELECT page_type AS Page_Type, sum(data_size) / 1024 / 1024 AS Size_in_MBFROM information_schema.innodb_buffer_pageGROUP BY page_typeORDER BY Size_in_MB DESC;
Results
+-------------------+------------+| Page_Type | Size_in_MB |+-------------------+------------+| INDEX | 0.13445091 || UNKNOWN | 0.00000000 || INODE | 0.00000000 || IBUF_INDEX | 0.00000000 || TRX_SYSTEM | 0.00000000 || SYSTEM | 0.00000000 || UNDO_LOG | 0.00000000 || FILE_SPACE_HEADER | 0.00000000 || IBUF_BITMAP | 0.00000000 |+-------------------+------------+
Some important and common data types are explained
- Index:b-tree Index This should also contain data for the row data page because the primary key index and the data in the clustered index are put together
- Ibuf_index: Insert Cache Index (innodb_change_buffering below)
- UNKNOWN: Not used or unaware of the state.
- Trx_system: System data (which may refer to transactions not determined here)
- Undo_log:undo log (Undo log below)
Innodb_change_buffering
https://dev.mysql.com/doc/refman/5.5/en/innodb-performance-change_buffering.html
The general meaning is as follows
在 insert update 和 delete操作 如果涉及到索引列 特别是 secondary keys 的时候;如果所涉及到的数据不在buffer pool的时候,由于涉及到的数据 是无序的 那么 频繁的操作会导致大量的IO消耗。到数据被载入到buffer pool的时候,change buffer 会将数据合并在一起 然后更新到磁盘文件中。在空闲的时候,InnoDB的主线程 会合并buffer changes的数据。由于change buffer 占用了部分buffer pool,降低了内存中可以缓存的数据页,如果数据已经载入到buffer pool或者有很少的secondary indexs,最好禁用掉这个特性。
The values you can set are as follows
- All
The default Value:buffer inserts, delete-marking operations, and purges.
- None
Do not buffer any operations.
- Inserts
Buffer insert operations.
- Deletes
Buffer delete-marking operations.
- Changes
Buffer both inserts and delete-marking operations.
- Purges
Buffer the physical deletion operations that happen in the background.
Undo Log
Undo log是InnoDB MVCC事务特性的重要组成部分,记录的是老版本的数据。主要作用是回滚数据,也可以根据undo log回溯到某个特别的版本的数据,实现MVCC。undo数据会首先被刷新到undo buffer中 之后在合适的时间 undo buffer中的数据 会被刷新到磁盘中,所有的undo log 会存放在ibd数据文件中(表空间)。Innodb 中存在purge线程,他们会查询那些无人问津的旧版本数据或者也内容标记为删除的操作也会被清理掉,从而保证undo log 不至于无限增长。
Memory management Algorithms
通常,数据库中的缓冲池是通过LRU即最近最少使用算法来管理,最少使用的也在LRU列表的尾端,当缓冲池中的数据满了之后,会首先释放LRU列表中的尾端的页数据。但是InnoDBd的存储引擎在读取到新的数据页的时候,不是直接放到LRU列表的首部,而是根据midpoint位置。这样的好处是如果是扫描数据的时候,通常需要操作许多的数据页,而这些数据又仅仅是在这次查询中需要,并不属于活跃的热数据。防止将最活跃的数据被移动到LRU尾部而被释放。
InnoDB System Architecture Summary (II)