How MySQL BufferPool cache and Redo log improve transaction performance


Insert image description here

introduction

Background: Today I watched an open class from a training institution about BufferPool caching and Redo logs. The lecture was very in-depth, but too limited, so I took the time to sort it out and summarize it for easy understanding and digestion.

In database systems, optimization of transaction performance is a key task. MySQL database uses two important technologies, BufferPool cache and redo log, to improve transaction performance. BufferPool caching can greatly increase data reading speed and reduce the IO burden on the disk. Redo logs can ensure that the database can be restored to a consistent state in the event of a failure, ensuring data integrity and consistency. The combination of these two technologies can greatly improve transaction performance and improve database operating efficiency.

BufferPool cache is a key memory management mechanism in the MySQL database. By storing frequently accessed data and index pages in memory, the data reading speed can be greatly improved and frequent disk IO operations can be avoided. For transaction processing, this can greatly speed up transaction processing and improve transaction throughput.

Redo log is a recovery mechanism used in MySQL database to ensure data integrity and consistency. When a transaction performs a modification operation, the modification record is first written to the redo log instead of directly modifying the data on the disk. When the transaction commits, the changes in the redo log are applied to the disk. The advantage of this is that if the system fails before the transaction is committed, after the system is restored, the unfinished transaction can be rolled back through the redo log to ensure data consistency.

Through BufferPool caching and redo logs, MySQL database can greatly improve transaction processing performance and improve database operating efficiency while ensuring data integrity and consistency. This is of great significance for database systems with large-scale concurrent access.

BufferPool caching and redo logs work together in the database to provide efficient data access and data security. The BufferPool cache improves data reading and modification performance by reducing disk access; the redo log ensures data persistence and consistency by recording transaction modification operations. Together, they ensure the high performance of the database and the reliability of the data.

Insert image description here
Image source MySQL official website "15.4 InnoDB Architecture" https://dev.mysql.com/doc/refman/8.0/en/innodb-architecture.html

1. The functions and advantages of BufferPool caching

1.1 Definition and function of BufferPool cache

BufferPool is a memory area in the database management system, used to cache data and index pages in the database. It is one of the core components of the database and can improve the read performance of the database. The size of the BufferPool can be adjusted according to the system configuration and is usually a portion of the database's memory space.

1.2 The role of BufferPool caching

The main function of the BufferPool cache is to load data and index pages on the disk into memory to reduce the number of disk IO accesses. When the database needs to read data, it will first search in the BufferPool. If the required data is found, it can be read directly from the memory without accessing the disk. This can greatly improve the reading performance of the database and reduce disk IO overhead.

The advantages of BufferPool also include:

  • Reduce the number of disk IO operations: Loading commonly used data and index pages into memory can reduce the number of disk IO operations and improve the efficiency of reading data.
  • Improve the reading speed of data: Because the data is in memory, the reading speed is faster, which can greatly shorten the response time of the database.
  • Reduce disk load and improve overall system performance: By caching data and index pages into memory, you can reduce disk load and improve overall system performance.

1.3 Change Buffer function

Reference document "MySQL Official Document 15.5.2 Change Buffer" https://dev.mysql.com/doc/refman/8.0/en/innodb-change-buffer.html
From the official architecture diagram in the introduction of this article, we You can see that there is a separate area in the Buffer Pool named Change Buffer. In fact, in the memory model of most database systems, Change Buffer is an important part of the Buffer Pool. Change Buffer is used to cache modification operations to the index when a transaction is committed to reduce disk I/O overhead.Insert image description here

Change Buffer is not suitable for all types of index modification operations. It is mainly used for insert, update and delete operations of non-unique indexes. For unique index modification operations, you still need to directly access the index page on the disk for processing.

When a transaction modifies an index in the database, it usually involves reading and writing index pages on disk, which causes additional disk I/O consumption and reduces system performance. The introduction of Change Buffer is to solve this problem.

Change Buffer works as follows:

  1. When a transaction modifies the index, if the relevant index page is not cached in the Buffer Pool, the modification operation will not be written to disk immediately, but will be recorded in the Change Buffer.
  2. Change Buffer is a dedicated buffer located in the Buffer Pool, used to save index modification information when a transaction is committed.
  3. When the transaction commits, the modification operations in the Change Buffer are applied to the index pages on disk. This process is called Change Buffer Merge.
  4. The process of Change Buffer Merge merges the modification operations in the Change Buffer with the index page on the disk, generates the latest index page content, and writes it back to the disk.
  5. The merged index pages will be added to the Buffer Pool for subsequent query operations.

The existence of Change Buffer brings several advantages:

  • Reduce disk I/O: By caching index modification operations into the Change Buffer, frequent disk read and write operations are avoided and system performance is improved.
  • Improve transaction concurrency performance: When multiple transactions modify the same index at the same time, their modification operations can be cached in the Change Buffer and merged at once when the transaction is committed, avoiding lock competition and improving concurrency performance. .
  • Reduce the amount of log writing: The modification operation of the Change Buffer is persisted to the disk when the transaction is committed, which can reduce the amount of writing to the transaction log and improve the log writing performance.
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
 insert 0, delete mark 0, delete 0
discarded operations:
 insert 0, delete mark 0, delete 0
Hash table size 4425293, used cells 32, node heap has 1 buffer(s)
13577.57 hash searches/s, 202.47 non-hash searches/s

2. Advantages of BufferPool caching

The advantages of BufferPool caching are mainly reflected in reducing the number of disk IO operations, increasing data reading speed and reducing disk load, thereby improving overall system performance. By properly configuring and utilizing the BufferPool cache, the read performance and response speed of the database can be effectively improved.

2.1 Reduce the number of disk IO operations

Since the BufferPool cache loads commonly used data and index pages into memory, when data needs to be read, it can be read directly from memory without the need for disk IO operations. This can greatly reduce the number of disk IOs, reduce the overhead and delay of disk access, and improve the reading performance of the database.

2.2 Improve data reading speed

Because the BufferPool cache stores data and index pages in memory, memory can be read much faster than disk. When data needs to be read, it can be read directly from the memory, reducing the waiting time for disk IO and speeding up data reading. This can greatly improve database performance and response speed for most read operations.

2.3 Reduce disk load and improve overall system performance

The disk is a relatively slow component in the database, and disk IO operations are expensive. By caching frequently used data and index pages into memory, the load on the disk can be reduced and the access pressure on the disk can be reduced. This can improve the performance of the entire system, reduce the mechanical movement of the disk, and extend the service life of the disk.

3. Working principle of BufferPool cache

Reference document https://dev.mysql.com/doc/refman/8.0/en/innodb-buffer-pool.html
Insert image description here

3.1 Cache hits and cache misses

BufferPool is a memory area in the MySQL database, used to store data pages of the database. When querying or modifying data, MySQL will first check whether the required data page exists in the BufferPool, which involves the concepts of cache hits and cache misses.

  • Cache Hit: If the required data page is already in the BufferPool, it is called a cache hit. In the case of a cache hit, MySQL can obtain data directly from the BufferPool, avoiding the cost of disk access and improving the speed of data access.

  • Cache Miss: If the required data page is not in the BufferPool, it is called a cache miss. In the case of a cache miss, MySQL needs to read the corresponding data page from the disk to the BufferPool so that subsequent query or modification operations can be performed in the BufferPool.

3.2 Cache replacement algorithm

When the BufferPool space is full, a cache replacement algorithm needs to be used to select the data page to be replaced to make room for the new data page. Common cache replacement algorithms include:

  • Least Recently Used (LRU): The LRU algorithm replaces data pages based on their recent usage. When a data page needs to be replaced, the least recently used data page is selected for replacement.
    Insert image description here

  • Least Frequently Used (LFU): The LFU algorithm replaces data pages based on the frequency with which they are accessed. When a data page needs to be replaced, the data page with the least number of accesses is selected for replacement.

  • Random: The random algorithm is a simple cache replacement algorithm that randomly selects a data page for replacement.

  • Other algorithms: There are other cache replacement algorithms, such as Clock algorithm, LRU-K algorithm, etc.

We can view it through the SHOW ENGINE INNODB STATUS\G command

----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 2198863872
Dictionary memory allocated 776332
Buffer pool size   131072
Free buffers       124908
Database pages     5720
Old database pages 2071
Modified db pages  910
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 4, not young 0
0.10 youngs/s, 0.00 non-youngs/s
Pages read 197, created 5523, written 5060
0.00 reads/s, 190.89 creates/s, 244.94 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not
0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read
ahead 0.00/s
LRU len: 5720, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
mysql> SHOW ENGINE INNODB STATUS\G
*************************** 1. row ***************************
  Type: InnoDB
  Name:
Status:
=====================================
2018-04-12 15:14:08 0x7f971c063700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 4 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 15 srv_active, 0 srv_shutdown, 1122 srv_idle
srv_master_thread log flush and writes: 0
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 24
OS WAIT ARRAY INFO: signal count 24
RW-shared spins 4, rounds 8, OS waits 4
RW-excl spins 2, rounds 60, OS waits 2
RW-sx spins 0, rounds 0, OS waits 0
Spin rounds per wait: 2.00 RW-shared, 30.00 RW-excl, 0.00 RW-sx
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2018-04-12 14:57:24 0x7f97a9c91700 Transaction:
TRANSACTION 7717, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
4 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 3
MySQL thread id 8, OS thread handle 140289365317376, query id 14 localhost root update
INSERT INTO child VALUES (NULL, 1), (NULL, 2), (NULL, 3), (NULL, 4), (NULL, 5), (NULL, 6)
Foreign key constraint fails for table `test`.`child`:
,
  CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON DELETE
  CASCADE ON UPDATE CASCADE
Trying to add in child table, in index par_ind tuple:
DATA TUPLE: 2 fields;
 0: len 4; hex 80000003; asc     ;;
 1: len 4; hex 80000003; asc     ;;

But in parent table `test`.`parent`, in index PRIMARY,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 4; hex 80000004; asc     ;;
 1: len 6; hex 000000001e19; asc       ;;
 2: len 7; hex 81000001110137; asc       7;;

------------
TRANSACTIONS
------------
Trx id counter 7748
Purge done for trx's n:o < 7747 undo n:o < 0 state: running but idle
History list length 19
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 421764459790000, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 7747, ACTIVE 23 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 9, OS thread handle 140286987249408, query id 51 localhost root updating
DELETE FROM t WHERE i = 1
------- TRX HAS BEEN WAITING 23 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 4 page no 4 n bits 72 index GEN_CLUST_INDEX of table `test`.`t`
trx id 7747 lock_mode X waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
 0: len 6; hex 000000000202; asc       ;;
 1: len 6; hex 000000001e41; asc      A;;
 2: len 7; hex 820000008b0110; asc        ;;
 3: len 4; hex 80000001; asc     ;;

------------------
TABLE LOCK table `test`.`t` trx id 7747 lock mode IX
RECORD LOCKS space id 4 page no 4 n bits 72 index GEN_CLUST_INDEX of table `test`.`t`
trx id 7747 lock_mode X waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
 0: len 6; hex 000000000202; asc       ;;
 1: len 6; hex 000000001e41; asc      A;;
 2: len 7; hex 820000008b0110; asc        ;;
 3: len 4; hex 80000001; asc     ;;

--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
 ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 0
833 OS file reads, 605 OS file writes, 208 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
 insert 0, delete mark 0, delete 0
discarded operations:
 insert 0, delete mark 0, delete 0
Hash table size 553253, node heap has 0 buffer(s)
Hash table size 553253, node heap has 1 buffer(s)
Hash table size 553253, node heap has 3 buffer(s)
Hash table size 553253, node heap has 0 buffer(s)
Hash table size 553253, node heap has 0 buffer(s)
Hash table size 553253, node heap has 0 buffer(s)
Hash table size 553253, node heap has 0 buffer(s)
Hash table size 553253, node heap has 0 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number          19643450
Log buffer assigned up to    19643450
Log buffer completed up to   19643450
Log written up to            19643450
Log flushed up to            19643450
Added dirty pages up to      19643450
Pages flushed up to          19643450
Last checkpoint at           19643450
129 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 2198863872
Dictionary memory allocated 409606
Buffer pool size   131072
Free buffers       130095
Database pages     973
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 810, created 163, written 404
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 973, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
----------------------
INDIVIDUAL BUFFER POOL INFO
----------------------
---BUFFER POOL 0
Buffer pool size   65536
Free buffers       65043
Database pages     491
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 411, created 80, written 210
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 491, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 1
Buffer pool size   65536
Free buffers       65052
Database pages     482
Old database pages 0
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 399, created 83, written 194
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 482, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Process ID=5772, Main thread ID=140286437054208 , state=sleeping
Number of rows inserted 57, updated 354, deleted 4, read 4421
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================

3.3 InnoDB buffer pool indicators

These metrics provide information about various aspects of the InnoDB buffer pool, including memory allocation, page management, IO operations, etc.

name describe
total memory allocated Total memory allocated for the buffer pool (bytes).
allocated dictionary memory Total memory allocated for the InnoDB data dictionary (bytes).
buffer pool size The total page size allocated to the buffer pool.
free buffer The total page size of the buffer pool free list.
Database page The total page size of the buffer pool LRU list.
old database page The total page size of the buffer pool old LRU sublist.
Modified database page The number of pages currently modified in the buffer pool.
pending reads The number of buffer pool pages waiting to be read into the buffer pool.
Pending LRU writes The number of old dirty pages from the bottom of the LRU list are waiting to be written in the buffer pool.
Pending refresh list writes The number of buffer pool pages that need to be flushed during a checkpoint.
Pending single page writes The number of independent page writes pending in the buffer pool.
Make database pages younger The total number of pages to make young in the buffer pool LRU list (moved to the head of the "new" page sublist).
Database pages are not young The total number of pages in the buffer pool LRU list that were not made young (pages remained unchanged in the "old" sublist and were not made young).
Rejuvenation rate per second The average number of accesses per second to old pages in the buffer pool's LRU list that rejuvenate the page.
Rejuvenation rate per second The average number of accesses per second to old pages in the buffer pool LRU list that do not rejuvenate the page.
Page read The total number of pages read from the buffer pool.
Created page The total number of pages created in the buffer pool.
pages written The total number of pages written from the buffer pool.
read rate per second The average number of buffer pool page reads per second.
creation rate per second The average number of buffer pool pages created per second.
write rate per second The average number of pages written from the buffer pool per second.
buffer pool hit rate Buffer pool page hit ratio for reading pages from the buffer pool versus reading pages from disk storage.
Rejuvenation hit rate The average hit rate of page visits resulting in page rejuvenation.
Unrejuvenated hit rate The average hit rate for page visits that did not result in page rejuvenation.
read-ahead Average number of read-ahead operations per second.
No access to evicted page The average number of evictions per second for pages that were not accessed from the buffer pool.
random read ahead Average number of random readahead operations per second.
LRU length The total page size of the buffer pool LRU list.
Decompression LRU length The length (in pages) of the buffer pool decompression LRU list.
I/O capacity The total number of buffer pool LRU list pages accessed.
Before I/O Sorry, my answer was truncated. Here's what's left in the table:
name describe
Before I/O The total number of buffer pool LRU list pages accessed during the current interval.
Total decompression I/O The total number of decompressed LRU list pages in the buffer pool decompressed.
Decompress I/O current The total number of buffer pool decompressed LRU list pages decompressed in the current interval.

4. The functions and advantages of redo logs

Refer to the document "MySQL 8.0 Version Document 15.6.5 Redo Log" https://dev.mysql.com/doc/refman/8.0/en/innodb-redo-log.html
to set the Redo log size

SET GLOBAL innodb_redo_log_capacity = 8589934592;

Redo log files use the "#ib_redoN" naming convention, where N is the number of the redo log file. Redundant redo log files are represented by the "_tmp" suffix. The following example shows the redo log files in an #innodb_redo directory, which has 21 active redo log files and 11 standby redo log files, numbered sequentially.

'#ib_redo582'  '#ib_redo590'  '#ib_redo598'      '#ib_redo606_tmp'
'#ib_redo583'  '#ib_redo591'  '#ib_redo599'      '#ib_redo607_tmp'
'#ib_redo584'  '#ib_redo592'  '#ib_redo600'      '#ib_redo608_tmp'
'#ib_redo585'  '#ib_redo593'  '#ib_redo601'      '#ib_redo609_tmp'
'#ib_redo586'  '#ib_redo594'  '#ib_redo602'      '#ib_redo610_tmp'
'#ib_redo587'  '#ib_redo595'  '#ib_redo603_tmp'  '#ib_redo611_tmp'
'#ib_redo588'  '#ib_redo596'  '#ib_redo604_tmp'  '#ib_redo612_tmp'
'#ib_redo589'  '#ib_redo597'  '#ib_redo605_tmp'  '#ib_redo613_tmp'

Each normal redo log file is associated with a specific range of LSN values; for example, the following query shows the START_LSN and END_LSN values ​​for the active redo log files listed in the example above:

mysql> SELECT FILE_NAME, START_LSN, END_LSN FROM performance_schema.innodb_redo_log_files;
+----------------------------+--------------+--------------+
| FILE_NAME                  | START_LSN    | END_LSN      |
+----------------------------+--------------+--------------+
| ./#innodb_redo/#ib_redo582 | 117654982144 | 117658256896 |
| ./#innodb_redo/#ib_redo583 | 117658256896 | 117661531648 |
| ./#innodb_redo/#ib_redo584 | 117661531648 | 117664806400 |
| ./#innodb_redo/#ib_redo585 | 117664806400 | 117668081152 |
| ./#innodb_redo/#ib_redo586 | 117668081152 | 117671355904 |
| ./#innodb_redo/#ib_redo587 | 117671355904 | 117674630656 |
| ./#innodb_redo/#ib_redo588 | 117674630656 | 117677905408 |
| ./#innodb_redo/#ib_redo589 | 117677905408 | 117681180160 |
| ./#innodb_redo/#ib_redo590 | 117681180160 | 117684454912 |
| ./#innodb_redo/#ib_redo591 | 117684454912 | 117687729664 |
| ./#innodb_redo/#ib_redo592 | 117687729664 | 117691004416 |
| ./#innodb_redo/#ib_redo593 | 117691004416 | 117694279168 |
| ./#innodb_redo/#ib_redo594 | 117694279168 | 117697553920 |
| ./#innodb_redo/#ib_redo595 | 117697553920 | 117700828672 |
| ./#innodb_redo/#ib_redo596 | 117700828672 | 117704103424 |
| ./#innodb_redo/#ib_redo597 | 117704103424 | 117707378176 |
| ./#innodb_redo/#ib_redo598 | 117707378176 | 117710652928 |
| ./#innodb_redo/#ib_redo599 | 117710652928 | 117713927680 |
| ./#innodb_redo/#ib_redo600 | 117713927680 | 117717202432 |
| ./#innodb_redo/#ib_redo601 | 117717202432 | 117720477184 |
| ./#innodb_redo/#ib_redo602 | 117720477184 | 117723751936 |
+----------------------------+--------------+--------------+

4.1 Definition and function of redo log

In MySQL, the redo log is a transaction log used to record modification operations in the database. It plays an important role in ensuring transaction durability and data integrity.

4.2 Ensure transaction durability and data integrity

  • Durability: One of the main functions of the redo log is to ensure the durability of transactions. When a transaction is committed, the relevant modification operations will first be written to the redo log and then applied to the database data file. In this way, even if the system fails or crashes after the transaction is submitted, the modification operations that are not persisted to the data file can be re-applied through the recovery mechanism of the redo log, thereby ensuring the durability of the transaction.

  • 数据完整性:redo日志还起到了保障数据完整性的作用。当数据库执行修改操作时,相关的修改信息会先被写入redo日志,然后才会被应用到实际的数据文件中。如果在这个过程中发生了故障,数据库可以利用redo日志重新应用这些修改,确保数据的完整性。在数据库恢复的过程中,redo日志是非常重要的,它能够将未完成的事务完成,并将数据库恢复到故障发生之前的状态,避免了数据的不一致性。

  • 高性能:由于redo日志是以顺序方式写入的,相比于随机写入数据文件,顺序写入日志文件的性能更高。这是因为磁盘的顺序写入速度通常比随机写入速度更快,从而提高了数据库的整体性能。
  • 数据恢复:通过redo日志,数据库可以在崩溃或故障中恢复到事务提交之前的状态。redo日志中的修改操作可以被重放,确保数据的完整性和一致性。
  • 并发控制:通过redo日志,数据库可以实现并发控制。多个事务可以并发地进行修改操作,并将这些操作记录在redo日志中。数据库可以根据事务的隔离级别和锁机制来协调并发访问,并保证数据的一致性。

五、redo日志的优势

通过redo日志,数据库可以在故障恢复过程中实现数据的持久性和一致性。即使在异常情况下,如系统崩溃或断电,数据库可以通过重放redo日志中的操作来恢复数据,并确保数据的完整性。

5.1 减少数据的写入操作:

redo日志的一大优势是减少了对数据文件的直接写入操作。当事务执行修改操作时,相关的修改信息首先被写入redo日志中,而不是立即写入到实际的数据文件中。这样可以减少磁盘的随机写入操作,提高了数据库的写入性能。

5.2 提高事务的提交速度:

由于redo日志的写入是以顺序方式进行的,相比于随机写入数据文件,顺序写入日志文件的速度更快。因此,事务的提交速度可以得到提升。当事务提交时,只需要将redo日志写入到磁盘中,而不需要等待所有的修改操作直接写入到数据文件中。

5.3 提供数据库的故障恢复能力:

redo日志在数据库的故障恢复过程中起着关键的作用。当数据库发生崩溃或故障时,通过redo日志可以将未持久化到数据文件中的修改操作重新应用,从而将数据库恢复到故障发生之前的状态。redo日志记录了事务的修改信息,可以用于将未完成的事务完成,并确保数据的一致性和完整性。

六、redo日志的工作原理

redo日志的工作原理包括生成与写入过程,以及重做和回滚的操作。它记录了事务的修改操作,并通过重做操作将修改应用到数据库的数据文件中,以确保数据的持久性和一致性;同时,通过回滚操作可以撤销事务的修改操作,将数据库回滚到事务开始之前的状态。

6.1 redo日志的生成与写入:

当事务执行修改操作时,相关的修改信息会生成并写入redo日志。具体的工作原理如下:

  • 在事务开始时,会为该事务分配一个唯一的事务ID。
  • 在事务执行过程中,对数据库的修改操作(如插入、更新、删除)会生成对应的redo日志记录,记录了修改前和修改后的数据值。
  • 生成的redo日志记录会被写入到redo日志缓冲区中,同时也会被异步刷写到磁盘上的redo日志文件中。

6.2 redo日志的重做与回滚:

  • 重做(Redo):当事务提交时,redo日志中的修改操作将被应用到数据库的数据文件中,以确保事务的持久性。重做操作会按照redo日志的顺序进行,将修改操作重新执行,将数据的物理状态更新到磁盘上的数据文件中。这样可以保证事务的修改操作在数据库的崩溃或故障后能够得到恢复。

  • 回滚(Rollback):在事务回滚的情况下,redo日志也发挥了作用。当事务被回滚时,相关的redo日志记录会被撤销,即将对应的修改操作反向执行。这样可以将事务的修改操作从数据库中撤销,使数据库回滚到事务开始之前的状态。

通过redo日志的重做和回滚机制,数据库可以实现数据的持久性和一致性。重做操作确保了数据的修改操作在事务提交后持久化到数据文件中,而回滚操作确保了在事务回滚时能够撤销相关的修改操作。

七、BufferPool缓存与redo日志的协同作用

7.1 BufferPool cache and redo log data synchronization:

BufferPool cache and redo log play different roles in the database, but there is a certain synergy between them, mainly reflected in data synchronization.

  • When a transaction performs a modification operation, the relevant data pages will be loaded into the BufferPool cache to improve data access performance. At the same time, for modification operations, relevant modification information will also be written to the redo log to ensure data persistence.
  • When the data page in the BufferPool cache is modified, the data will be modified in the corresponding data page, and the modified data will be updated to the BufferPool. At this time, the relevant redo log records will also be written to the redo log buffer.
  • When the transaction is committed, the modification of the data page in the BufferPool cache will be written back to the data file on the disk, and the relevant redo log records will be flushed to the redo log file on the disk. This ensures the consistency and durability of the data and ensures that the data can be recovered after a database failure.

7.2 The complementary role of BufferPool cache and redo log:

BufferPool caching and redo logs play complementary roles in the operation of the database, mainly in terms of data access performance and data security.

  • BufferPool cache provides an efficient data access method. By loading hot data into memory, it avoids frequent disk access operations and improves data reading and modification performance.
  • The redo log records transaction modification operations, ensuring data persistence and consistency. Even in the event of a database failure or crash, unpersisted modification operations can be re-applied through the redo log to restore the consistent state of the data.

Insert image description here

Hello everyone, this is Bingdian. Today, that’s all about how MySQL BufferPool caching and Redo logs improve transaction performance. If you have questions or insights, you can leave a message in the comment area.

Guess you like

Origin blog.csdn.net/wangshuai6707/article/details/132990207