Home Database Mysql Tutorial InnoDB缓存相关

InnoDB缓存相关

Jun 01, 2016 pm 01:13 PM
technology Database systems Record

InnoDB存储引擎是基于磁盘存储的,并将其中的记录按照页的方式进行管理。在数据库系统中,由于CPU速度和磁盘速度之前的鸿沟,通常使用缓冲池技术来提高数据库的整体性能。

1. Innodb_buffer_pool

缓冲池(buffer pool)简单来说就是一块内存区域。缓冲池中缓存的数据页类型有:索引页、数据页、undo页、插入缓冲、自适应哈希索引、InnoDB存储的锁信息、数据字典信息等。不能简单认为,缓冲池只是缓存索引页和数据页,它们只是占缓冲池很大的一部分而已。

在数据库中进行读取页的操作,首先将从磁盘读到的页存放在缓冲池中,下一次再读相同的页时,首先判断该页是否在缓冲池中。若在,称该页在缓冲池中被命中,直接读取该页。否则,读取磁盘中的页。

root@rac3 mysql> show global status like 'Innodb_buffer_pool_%';+---------------------------------------+--------+| Variable_name | Value |+---------------------------------------+--------+| Innodb_buffer_pool_pages_data | 1118 || Innodb_buffer_pool_pages_dirty | 0 || Innodb_buffer_pool_pages_flushed | 1950 || Innodb_buffer_pool_pages_free | 129951 || Innodb_buffer_pool_pages_misc | 3 || Innodb_buffer_pool_pages_total | 131072 || Innodb_buffer_pool_read_ahead_rnd | 0 || Innodb_buffer_pool_read_ahead | 311 || Innodb_buffer_pool_read_ahead_evicted | 0 || Innodb_buffer_pool_read_requests | 202858 || Innodb_buffer_pool_reads | 756 || Innodb_buffer_pool_wait_free | 0 || Innodb_buffer_pool_write_requests | 43825 |+---------------------------------------+--------+13 rows in set (0.00 sec)
Copy after login

从上面的值我们可以看出总共 131072  pages,还有 129951 是 Free 状态的,仅仅只有 1118 个 page 有数据, read 请求 202858 次,其中有 756 次所请求的数据在 buffer  pool 中没有,也就是说有 756 次是通过读取物理磁盘来读取数据的,所以很容易也就得出了 Innodb Buffer  Pool 的 Read 命中率大概在为:(202858 - 756)/ 202858  * 100% 。

Innodb 在修改数据的时候同样也只是修改 buffer pool中的数据,并不是在一个事务提交的时候就将buffer pool中被修改的数据同步到磁盘,而是通过另外一种支持事务的数据库系统常用的手段,将修改信息记录到相应的事务日志中。

我们的应用所修改的buffer pool中的数据都很随机,每次所做的修改都是一个或者少数几个数据页,多次修改的数据页也很少会连续。如果我们每次修改之后都将buffer pool的数据同步到磁盘, 那么磁盘就只能一直忙于频繁的随即读写操作。而事务日志在创建之初就是申请的连续的物理空间,而且每次写入都是紧接着之前的日志数据顺序的往后写入,基本上都是一个顺序的写入过程。所以,日志的写入操作远比同步buffer pool中被修改的数据要更快。

2. redo log_buffer

事务日志本身也有 buffer,也就是redo log_buffer,每次事务日志的写入并不是直接写入到文件,也都是暂时先写入到 redo log_buffer中,然后再在一定的事件触发下才会同步到文

事务日志文件的大小与 Innodb 的整体 IO 性能有非常大的关系。理论上来讲,日志文件越大,则 Buffer  Pool 所需要做的刷新动作也就越少,性能也越高。但是,我们也不能忽略另外一个事情,那就是 当系统 Crash 之后的恢复。

Innodb中记录了每一次对数据库中的数据及索引所做的修改,以及与修改相关的事务信息。同时还记录了系统每次 checkpoint 与 log sequence number(日志序列号)。假设在某一时刻,MySQL Crash了,那么很显然,所有buffer pool中的数据都会丢失,也包括已经修改且没有来得及刷新到数据文件中的数据。难道我们就让这些数据丢失么?当然不会,当MySQL从Crash之后再次启动,Innodb 会通过比较事务日志中所记录的checkpoint信息和各个数据文件中的checkpoint信息,找到最后一次checkpoint所对应的log sequence number,然后通过事务日志中所记录的变更记录,将从 Crash 之前最后一次checkpoint往后的所有变更重新应用一次,同步所有的数据文件到一致状态,这样就找回了因为系统 Crash 而造成的所有数据丢失。当然,对于 log  buffer中未来得及同步到日志文件的变更数据就无法再找回了。系统 Crash 的时间离最后一次 checkpoint 的时间越长,所需要的恢复时间也就越长。而日志文件越大,Innodb 所做的 checkpoint 频率也越低,自然遇到长时间恢复的可能性也就越大了。

2.1 checkpoint

在InnoDB存储引擎中,可能发生如下几种情况的Fuzzy Checkpoint:

(1)Master Thread Checkpoint

对于Master Thread中发生的checkpoint,差不多以每秒或每十秒的速度从缓冲池的脏页列表中刷新一定比例的页回磁盘。这个过程是异步的,即此时InnoDB存储引擎可以进行其他的操作,用户查询线程不会阻塞。

(2)FLUSH_LRU_LIST Checkpoint

InnoDB存储引擎需要保证LRU列表中需要有差不多100个空闲页可供使用。若没有100个空闲页,那么InnoDB存储引擎会将LRU列表尾端的页移除。如果这些页中有脏页,那么需要进行checkpoint。这些页是来自LRU列表的,因此称为FLUSH_LRU_LIST Checkpoint。

(3)Async/Sync Flush Checkpoint

Async/Sync Flush Checkpoint是为了保证redo log的循环使用可用性。

(4)Dirty Page too much Checkpoint

脏页的数量太多,导致InnoDB存储引擎强制进行Checkpoint。可由参数innodb_max_dirty_pages_pct控制。 

root@rac3 mysql> show variables like 'innodb_max_dirty_pages_pct'/G*************************** 1. row ***************************Variable_name: innodb_max_dirty_pages_pctValue: 851 row in set (0.00 sec)
Copy after login

innodb_max_dirty_pages_pct的值为85,表示当缓冲池中脏页的数量占据85%时,强制进行checkpoint,刷新一部分的脏页到磁盘。

2.2 innodb_flush_log_at_trx_commit

参数innodb_flush_log_at_trx_commit用来控制事务日志刷新到磁盘的策略。

默认innodb_flush_log_at_trx_commit=1,表示在每次事务提交的时候,都把log buffer刷到文件系统中去,并且调用文件系统的“flush”操作将缓存刷新到磁盘上去。这样的话,数据库对IO的要求就非常高了,如果底层的硬件提供的IOPS比较差,那么MySQL数据库的并发很快就会由于硬件IO的问题而无法提升。为了提高效率,保证并发,牺牲一定的数据一致性。innodb_flush_log_at_trx_commit还可以设置为0和2。

innodb_flush_log_at_trx_commit=0时,提交事务时并不将log buffer写入磁盘,而是等待主线程每秒的刷新。

innodb_flush_log_at_trx_commit=2时,事务提交时将事务日志写入redo log file,但仅写入文件系统的缓存,不进行fsync操作。在这个设置下,当MySQL数据库发生宕机而操作系统不发生宕机,并不会导致事务的丢失。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Mar 12, 2024 pm 07:20 PM

Pinduoduo software provides a lot of good products, you can buy them anytime and anywhere, and the quality of each product is strictly controlled, every product is genuine, and there are many preferential shopping discounts, allowing everyone to shop online Simply can not stop. Enter your mobile phone number to log in online, add multiple delivery addresses and contact information online, and check the latest logistics trends at any time. Product sections of different categories are open, search and swipe up and down to purchase and place orders, and experience convenience without leaving home. With the online shopping service, you can also view all purchase records, including the goods you have purchased, and receive dozens of shopping red envelopes and coupons for free. Now the editor has provided Pinduoduo users with a detailed online way to view purchased product records. method. 1. Open your phone and click on the Pinduoduo icon.

The Stable Diffusion 3 paper is finally released, and the architectural details are revealed. Will it help to reproduce Sora? The Stable Diffusion 3 paper is finally released, and the architectural details are revealed. Will it help to reproduce Sora? Mar 06, 2024 pm 05:34 PM

StableDiffusion3’s paper is finally here! This model was released two weeks ago and uses the same DiT (DiffusionTransformer) architecture as Sora. It caused quite a stir once it was released. Compared with the previous version, the quality of the images generated by StableDiffusion3 has been significantly improved. It now supports multi-theme prompts, and the text writing effect has also been improved, and garbled characters no longer appear. StabilityAI pointed out that StableDiffusion3 is a series of models with parameter sizes ranging from 800M to 8B. This parameter range means that the model can be run directly on many portable devices, significantly reducing the use of AI

Have you really mastered coordinate system conversion? Multi-sensor issues that are inseparable from autonomous driving Have you really mastered coordinate system conversion? Multi-sensor issues that are inseparable from autonomous driving Oct 12, 2023 am 11:21 AM

The first pilot and key article mainly introduces several commonly used coordinate systems in autonomous driving technology, and how to complete the correlation and conversion between them, and finally build a unified environment model. The focus here is to understand the conversion from vehicle to camera rigid body (external parameters), camera to image conversion (internal parameters), and image to pixel unit conversion. The conversion from 3D to 2D will have corresponding distortion, translation, etc. Key points: The vehicle coordinate system and the camera body coordinate system need to be rewritten: the plane coordinate system and the pixel coordinate system. Difficulty: image distortion must be considered. Both de-distortion and distortion addition are compensated on the image plane. 2. Introduction There are four vision systems in total. Coordinate system: pixel plane coordinate system (u, v), image coordinate system (x, y), camera coordinate system () and world coordinate system (). There is a relationship between each coordinate system,

This article is enough for you to read about autonomous driving and trajectory prediction! This article is enough for you to read about autonomous driving and trajectory prediction! Feb 28, 2024 pm 07:20 PM

Trajectory prediction plays an important role in autonomous driving. Autonomous driving trajectory prediction refers to predicting the future driving trajectory of the vehicle by analyzing various data during the vehicle's driving process. As the core module of autonomous driving, the quality of trajectory prediction is crucial to downstream planning control. The trajectory prediction task has a rich technology stack and requires familiarity with autonomous driving dynamic/static perception, high-precision maps, lane lines, neural network architecture (CNN&GNN&Transformer) skills, etc. It is very difficult to get started! Many fans hope to get started with trajectory prediction as soon as possible and avoid pitfalls. Today I will take stock of some common problems and introductory learning methods for trajectory prediction! Introductory related knowledge 1. Are the preview papers in order? A: Look at the survey first, p

DualBEV: significantly surpassing BEVFormer and BEVDet4D, open the book! DualBEV: significantly surpassing BEVFormer and BEVDet4D, open the book! Mar 21, 2024 pm 05:21 PM

This paper explores the problem of accurately detecting objects from different viewing angles (such as perspective and bird's-eye view) in autonomous driving, especially how to effectively transform features from perspective (PV) to bird's-eye view (BEV) space. Transformation is implemented via the Visual Transformation (VT) module. Existing methods are broadly divided into two strategies: 2D to 3D and 3D to 2D conversion. 2D-to-3D methods improve dense 2D features by predicting depth probabilities, but the inherent uncertainty of depth predictions, especially in distant regions, may introduce inaccuracies. While 3D to 2D methods usually use 3D queries to sample 2D features and learn the attention weights of the correspondence between 3D and 2D features through a Transformer, which increases the computational and deployment time.

The first multi-view autonomous driving scene video generation world model | DrivingDiffusion: New ideas for BEV data and simulation The first multi-view autonomous driving scene video generation world model | DrivingDiffusion: New ideas for BEV data and simulation Oct 23, 2023 am 11:13 AM

Some of the author’s personal thoughts In the field of autonomous driving, with the development of BEV-based sub-tasks/end-to-end solutions, high-quality multi-view training data and corresponding simulation scene construction have become increasingly important. In response to the pain points of current tasks, "high quality" can be decoupled into three aspects: long-tail scenarios in different dimensions: such as close-range vehicles in obstacle data and precise heading angles during car cutting, as well as lane line data. Scenes such as curves with different curvatures or ramps/mergings/mergings that are difficult to capture. These often rely on large amounts of data collection and complex data mining strategies, which are costly. 3D true value - highly consistent image: Current BEV data acquisition is often affected by errors in sensor installation/calibration, high-precision maps and the reconstruction algorithm itself. this led me to

GSLAM | A general SLAM architecture and benchmark GSLAM | A general SLAM architecture and benchmark Oct 20, 2023 am 11:37 AM

Suddenly discovered a 19-year-old paper GSLAM: A General SLAM Framework and Benchmark open source code: https://github.com/zdzhaoyong/GSLAM Go directly to the full text and feel the quality of this work ~ 1 Abstract SLAM technology has achieved many successes recently and attracted many attracted the attention of high-tech companies. However, how to effectively perform benchmarks on speed, robustness, and portability with interfaces to existing or emerging algorithms remains a problem. In this paper, a new SLAM platform called GSLAM is proposed, which not only provides evaluation capabilities but also provides researchers with a useful way to quickly develop their own SLAM systems.

How to view your medication log history in the Health app on iPhone How to view your medication log history in the Health app on iPhone Nov 29, 2023 pm 08:46 PM

iPhone lets you add medications to the Health app to track and manage the medications, vitamins and supplements you take every day. You can then log medications you've taken or skipped when you receive a notification on your device. After you log your medications, you can see how often you took or skipped them to help you track your health. In this post, we will guide you to view the log history of selected medications in the Health app on iPhone. A short guide on how to view your medication log history in the Health App: Go to the Health App>Browse>Medications>Medications>Select a Medication>Options&a

See all articles