innodb_flush_method取值方法(实例讲解)

 更新时间:2017年03月22日 08:33:43   投稿:jingxian  
下面小编就为大家带来一篇innodb_flush_method取值方法(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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),使得走覆盖索引扫描,可大大地减少响应时间

以上这篇innodb_flush_method取值方法(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • mysql中left join设置条件在on与where时的用法区别分析

    mysql中left join设置条件在on与where时的用法区别分析

    这篇文章主要介绍了mysql中left join设置条件在on与where时的用法区别,结合实例形式分析了mysql中left join设置条件在on与where时的相关用法区别与操作注意事项,需要的朋友可以参考下
    2020-02-02
  • MySQL Binlog 日志处理工具对比分析

    MySQL Binlog 日志处理工具对比分析

    这篇文章主要介绍了MySQL Binlog 日志处理工具对比分析的相关资料,帮助大家更好的理解和学习使用MySQL数据库,感兴趣的朋友可以了解下
    2021-03-03
  • MySQL乐观锁和悲观锁具体实现

    MySQL乐观锁和悲观锁具体实现

    这篇文章主要介绍了MySQL乐观锁和悲观锁具体实现,文章围绕主题展开详细的内容戒杀,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • MySQL数据库必知必会之安全管理

    MySQL数据库必知必会之安全管理

    MySQL数据库通常包含关键的数据,为确保这些数据的安全和完整,需要利用访问控制和用户管理的功能,下面这篇文章主要给大家介绍了关于MySQL数据库必知必会之安全管理的相关资料,需要的朋友可以参考下
    2022-05-05
  • 解析MySQL设置当前时间为默认值的方法

    解析MySQL设置当前时间为默认值的方法

    本篇文章是对MySQL设置当前时间为默认值的方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • 图解MySQL中乐观锁扣减库存原理

    图解MySQL中乐观锁扣减库存原理

    这篇文章主要为大家详细介绍了MySQL中乐观锁扣减库存原理的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起了解一下
    2023-04-04
  • MySQL允许远程登录的操作实现

    MySQL允许远程登录的操作实现

    本文主要介绍了MySQL允许远程登录的操作实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-02-02
  • 简单介绍MySQL中的事务机制

    简单介绍MySQL中的事务机制

    这篇文章主要介绍了MySQL中的事务机制,通过实例介绍了大概的流程,需要的朋友可以参考下
    2015-04-04
  • mysqli多查询特性 实现多条sql语句查询

    mysqli多查询特性 实现多条sql语句查询

    mysqli相对于mysql有很多优势,mysqli连接数据库和mysqli预处理prepare使用,不仅如此,mysqli更是支持多查询特性
    2012-12-12
  • MySQL CPU过高的排查方法

    MySQL CPU过高的排查方法

    这篇文章主要介绍了MySQL CPU过高的排查方法,通过top命令查看服务器CPU资源使用情况,明确CPU占用率较高的是否是mysqld进程,文章通过图文介绍的非常详细,需要的朋友可以参考下
    2023-11-11

最新评论