innodb_flush_method取值方法(实例讲解)
作者:jingxian 发布时间:2024-01-13 10:12:44
innodb_flush_method的几个典型取值
fsync: InnoDB uses the fsync() system call to flush both the data and log files. fsync is the default setting.
O_DSYNC: InnoDB uses O_SYNC to open and flush the log files, and fsync() to flush the data files. InnoDB does not use O_DSYNC directly because there have been problems with it on many varieties of Unix.
O_DIRECT: InnoDB uses O_DIRECT (or directio() on Solaris) to open the data files, and uses fsync() to flush both the data and log files. This option is available on some GNU/Linux versions,FreeBSD, and Solaris.
如何取值,mysql官方文档是这么建议的
How each settings affects performance depends on hardware configuration and workload. Benchmark
your particular configuration to decide which setting to use, or whether to keep the default setting.
Examine the Innodb_data_fsyncs status variable to see the overall number of fsync() calls for
each setting. The mix of read and write operations in your workload can affect how a setting performs.
For example, on a system with a hardware RAID controller and battery-backed write cache, O_DIRECT
can help to avoid double buffering between the InnoDB buffer pool and the operating system's file
system cache. On some systems where InnoDB data and log files are located on a SAN, the default
value or O_DSYNC might be faster for a read-heavy workload with mostly SELECT statements. Always
test this parameter with hardware and workload that reflect your production environment
也就是说,具体的取值跟硬件配置和工作负载相关,最好做一次压测来决定。不过通常来说,linux环境下具有raid控制器和write-back写策略,o_direct是比较好的选择;如果存储介质是SAN,那么使用默认fsync或者osync或许更好一些。
通常来说,貌似绝大部分人都取值o_direct,底层有raid卡,读写策略设置为write-back。在使用sysbench压测oltp类型时,我发现o_direct确实比fsync性能优秀一些,看来适用于大部分场景,但是最近碰到一个这样的sql,客户反馈很慢,而在相同内存的情况下,它自己搭建的云主机执行相对快很多,后来我发现主要就是innodb_flush_method的设置值不同带来的巨大性能差异。
测试场景1
innodb_flush_method为默认值,即fsync,缓存池512M,表数据量1.2G,排除缓存池影响,稳定后的结果
mysql> show variables like '%innodb_flush_me%';
+---------------------+-------+
| Variable_name | Value |
+---------------------+-------+
| innodb_flush_method | |
+---------------------+-------+
1 row in set (0.00 sec)
mysql> SELECT sql_no_cache SUM(outcome)-SUM(income) FROM journal where account_id = '1c6ab4e7-main';
+--------------------------+
| SUM(outcome)-SUM(income) |
+--------------------------+
| -191010.51 |
+--------------------------+
1 row in set (1.22 sec)
mysql> SELECT sql_no_cache SUM(outcome)-SUM(income) FROM journal where account_id = '1c6ab4e7-main';
+--------------------------+
| SUM(outcome)-SUM(income) |
+--------------------------+
| -191010.51 |
+--------------------------+
1 row in set (1.22 sec)
mysql> explain SELECT sql_no_cache SUM(outcome)-SUM(income) FROM journal where account_id = '1c6ab4e7-main';
+----+-------------+---------+------+---------------+------------+---------+-------+--------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------------+---------+-------+--------+-----------------------+
| 1 | SIMPLE | journal | ref | account_id | account_id | 62 | const | 161638 | Using index condition |
+----+-------------+---------+------+---------------+------------+---------+-------+--------+-----------------------+
1 row in set (0.03 sec)
测试场景2
innodb_flush_method改为o_direct,排除缓存池影响,稳定后的结果
mysql> show variables like '%innodb_flush_me%';
+---------------------+----------+
| Variable_name | Value |
+---------------------+----------+
| innodb_flush_method | O_DIRECT |
+---------------------+----------+
1 row in set (0.00 sec)
mysql> SELECT sql_no_cache SUM(outcome)-SUM(income) FROM journal where account_id = '1c6ab4e7-main';
+--------------------------+
| SUM(outcome)-SUM(income) |
+--------------------------+
| -191010.51 |
+--------------------------+
1 row in set (3.22 sec)
mysql> SELECT sql_no_cache SUM(outcome)-SUM(income) FROM journal where account_id = '1c6ab4e7-main';
+--------------------------+
| SUM(outcome)-SUM(income) |
+--------------------------+
| -191010.51 |
+--------------------------+
1 row in set (3.02 sec)
mysql> explain SELECT sql_no_cache SUM(outcome)-SUM(income) FROM journal where account_id = '1c6ab4e7-main';
+----+-------------+---------+------+---------------+------------+---------+-------+--------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------------+---------+-------+--------+-----------------------+
| 1 | SIMPLE | journal | ref | account_id | account_id | 62 | const | 161638 | Using index condition |
+----+-------------+---------+------+---------------+------------+---------+-------+--------+-----------------------+
1 row in set (0.00 sec)
结果比较:
两者执行计划一摸一样,性能却差距很大。在数据库第一次启动时的查询结果也差距很大,o_direct也差很多(测试结果略)。不是很懂为啥这种情况下多了一层操作系统缓存,读取效率就高了很多,生产环境设置一定要以压测结果为准,实际效果为准,不能盲目信任经验值。
改进措施:
不改变innodb_flush_method的情况下,其实这条sql还可以进一步优化,通过添加组合索引(account_id,outcome,income),使得走覆盖索引扫描,可大大地减少响应时间


猜你喜欢
- Python内存管理一、对象池1.小整数池系统默认创建好的,等着你使用概述:整数在程序中的使用非常广泛,Python为了优化速度,使用了小整
- 一、需求来源工作中需要一种树形菜单组件,经过两天的构思最终通过作用域插槽实现: 此组件将每个节点(插槽名为 node)暴露出来。通过插槽的
- python有很多有趣的库,其中wxpy是连接微信的接口,具体可以查看官方文档。可以实现自动操作,wxpy 支持 Python 3.4-3.
- 本文实例讲述了Python使用sorted排序的方法。分享给大家供大家参考,具体如下:# 例1. 按照元素出现的次数来排序seq = [2,
- 1.开发环境 vue+element2.电脑系统 windows 10 专业版3.在开发的过程中,我们总是会使用到 git管理代码!使用方法
- 支持两种用法:(1)合并某一文件夹下的所有文件(忽略文件夹等非文件条目)(2)显示的合并多文件。import sysimport os
- 首先要解释一下:“极致之美”不是说月儿的这篇文章,因为本人还没有自大到这种程度:P,它形容的是Lisp和javascript结合的优美形态。
- 问题描述前端时间在公司的时候,要使用angular开发一个网站,因为angular很适合前后端分离,所以就做了一个简单的图书管理系统来模拟前
- 一、变量相关1.变量声明C# : int a;Go : var a int; 需要在前面加一个var关键字,后面定义类型可以使用 var(
- sql语句有一个非常长的sql,用编辑器打开编写的时候太长了导致编写非常吃力,而且容易错乱,我想做的是把A,B,C三个变量赋值到sql中的字
- 摘要: 当你想快速共享一个目录的时候,这是特别有用的,只需要1行代码即可实现。当你想快速共享一个目录的时候,这是特别有用的,只需
- 本文实例为大家分享了python openCV自制绘画板的具体代码,供大家参考,具体内容如下import numpy as npimport
- <P><HTML><HEAD><TITLE>javascriptboy</TITLE&
- 日志是用来记录程序在运行过程中发生的状况,在程序开发过程中添加日志模块能够帮助我们了解程序运行过程中发生了哪些事件,这些事件也有轻重之分。根
- 方法一:(by yangedie )这几天刚刚做了这个东西,有网友问到,所以分享一下。ie6、firefox2 通过,麻烦有ie7的网友测试
- 本文实例为大家分享了python实现事件驱动的具体代码,供大家参考,具体内容如下EventManager事件管理类实现,大概就百来行代码左右
- 事务在数据库中有时候需要把多个步骤的指令当作一个整体来运行,这个整体要么全部成功,要么全部失败,这就需要用到事务。1、 事务的特点事务有若干
- 今天学习Python的时候,需要安装一个第三方库,Python Imaging Library,是Python下面一个非常强大的处理图像的工
- 本文实例讲述了python中尾递归用法。分享给大家供大家参考。具体分析如下:如果一个函数中所有递归形式的调用都出现在函数的末尾,我们称这个递
- 遇到的问题:在做爬虫的时候,爬取的url链接内还有转义字符,反斜杠 \,打算用正则的re.sub()替换掉的时候遇到了问题,这是要做替换的字