`
Supanccy2013
  • 浏览: 215081 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

mysql性能调优1---innodb_flush_log_at_trx_commit

阅读更多
注:个人经验分享,转载请注明出处

优化配置文件my.ini文件中的配置
第一个重要指标:innodb_flush_log_at_trx_commit=2
可选值有0,1,2 mysql默认配置的是1
这里引用mysql官方解释这个指标:
# If set to 1, InnoDB will flush (fsynct同步) the transaction logs to the
# disk at each commit, which offers full ACID behavior. If you are
# willing to compromise this safety, and you are running small
# transactions, you may set this to 0 or 2 to reduce disk I/O to the
# logs. Value 0 means that the log is only written to the log file and
# the log file flushed to disk approximately(大约) once per second. Value 2
# means the log is written to the log file at each commit, but the log
# file is only flushed to disk approximately(大约) once per second.
innodb_flush_log_at_trx_commit=2
翻译下来就是:
innodb_flush_log_at_trx_commit = 0,Innodb 中的Log Thread 没隔1 秒钟会将log buffer中的数据写入到文件,同时还会通知文件系统进行文件同步的flush 操作,保证数据确实已经写入到磁盘上面的物理文件。但是,每次事务的结束(commit 或者是rollback)并不会触发Log Thread 将log buffer 中的数据写入文件。所以,当设置为0 的时候,当MySQL Crash 和OS Crash 或者主机断电之后,最极端的情况是丢失1 秒时间的数据变更。

innodb_flush_log_at_trx_commit = 1,这也是Innodb 的默认设置。我们每次事务的结束都会触发Log Thread 将log buffer 中的数据写入文件并通知文件系统同步文件。这个设置是最安全的设置,能够保证不论是MySQL Crash 还是OS Crash 或者是主机断电都不会丢失任何已经提交的数据。

innodb_flush_log_at_trx_commit = 2,当我们设置为2 的时候,Log Thread 会在我们每次事务结束的时候将数据写入事务日志,但是这里的写入仅仅是调用了文件系统的文件写入操作。而我们的文件系统都是有缓存机制的,所以Log Thread 的这个写入并不能保证内容真的已经写入到物理磁盘上面完成持久化的动作。文件系统什么时候会将缓存中的这个数据同步到物理磁盘文件Log Thread 就完全不知道了。所以,当设置为2 的时候,MySQL Crash 并不会造成数据的丢失,但是OS Crash 或者是主机断电后可能丢失的数据量就完全控制在文件系统上了。各种文件系统对于自己缓存的刷新机制各不一样,

综上所述:0是最不安全的,如果断电会丢失mysql缓存,文件系统缓存中将近一秒的数据。
          1是最安全的,怎么都不会丢失数据,但是也是性能最差的。
          2是居于中间。
通常优化的时候把该值设置为2可大约提升百分之200到300的性能。

下面针对该配置进行测试:
sql语句:
DROP TABLE IF EXISTS `supan`;
CREATE TABLE `supan` (
  `name` varchar(30) DEFAULT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `grade` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;


java程序:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class InsertTestData
{
	public static void main(String[] args)
	{
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://127.0.0.1:3306/test";
		String user = "root";
		String password = "root";

		try
		{
			Class.forName(driver);
			Connection conn = DriverManager.getConnection(url, user, password);
			//故意设置自动提交为false
			conn.setAutoCommit(false);
			PreparedStatement pst = (PreparedStatement) conn.prepareStatement("insert into supan(name,grade) values(?,?)");  
			//记录开始时间
			Long startTime = System.currentTimeMillis();  
			
			for (long i = 0; i < 1000000; i++) { 
				pst.setString(1, "张三");
			    pst.setLong(2,i);;  
			    pst.addBatch();  
				pst.executeBatch(); 
				//故意每次都提交一个sql语句,增加事务的开启和关闭
				conn.commit();
				pst.clearBatch();
			} 
			
			// 语句执行完毕,提交本事务  
			Long endTime = System.currentTimeMillis();  
			System.out.println(endTime - startTime);
			conn.close();

		} catch (Exception e)
		{
			System.out.println("Sorry,can`t find the Driver!");
			e.printStackTrace();
		} 
	}
}


上面测试代码插入一百万数据:在配置为1的情况下执行时间为:1337496
                        在配置为2的情况下执行时间为:434137
效率提升整200%
分享到:
评论

相关推荐

    数据库优化配置.doc

    [client] port=3306 [mysql] no-beep default-character-set=utf8 [mysqld] datadir=D:/Data port=3306 server-id=...log_file_size=1G innodb_log_buffer_size=8M innodb_flush_log_at_trx_commit=2 innodb_file_per_t

    :innodb_flush_log_at_trx_commit 和 sync_binlog1

    这样的好处,减少了事务数据丢失的概率,而对底层硬件的 IO 要求也没有那么高(log buffer 写到文件系统中,一般只是从 log buffer 的内存转移

    fupengfei058#blog#MySQL 重要参数 innodb_flush_log_at_trx_commit 和 sy

    可以看到,只有1才能真正地保证事务的持久性,但是由于刷新操作 fsync() 是阻塞的,直到完成后才返回,我们知道写磁盘的速度是很慢的,因此 MySQL 的性能

    my.cnf参数配置实现InnoDB引擎性能优化

    在网上看了无数的my....结果是只有innodb_flush_log_at_trx_commit可以提高性能,对于1,2,3参数无论是开其中某一个,还是三个同时调节都没有影响到测试性能。我想了下,可能是我的测试数据量还不够大造成的,后续有条

    mysql参数及其优化

    query_cache_size、query_cache_type、innodb_buffer_pool_size、innodb_log_file_size、innodb_log_buffer_size、innodb_flush_logs_at_trx_commit、transaction_isolation、innodb_file_per_table、innodb_open_...

    centos下mysql主从同步快速设置步骤分享

    Master:/etc/my.cnf [mysqld] server-id = 1 log-bin innodb_flush_log_at_trx_commit=1 sync_binlog=1 datadir=/var/lib/mysql character-set-server=utf8 init_connect=’SET NAMES utf8’设定了默认字符集为utf8...

    MySQL 句柄数占用过多的解决方法

    在Windows下安装MySQL ,用了官方的配置... 后来又看到一个设置innodb_flush_log_at_trx_commit  innodb_flush_log_at_trx_commit (这个很管用)  抱怨Innodb比MyISAM慢 100倍?那么你大概是忘了调整这个值。默

    mysql主从同步快速设置方法

    安装环境 centos 5.4 mysql 5.1.xx 采用rpm直接安装 xtrabackup 1.2.22 采用rpm直接安装 代码如下: [mysqld] server-id = 1 log-bin innodb_flush_log_at_trx_commit=1 sync_binlog=1 datadir=/var/lib/mysql ...

    centos下mysql主从复制设置详解

    Master:/etc/my.cnf 代码如下:[mysqld] server-id = 1log-bin innodb_flush_log_at_trx_commit=1 sync_binlog=1 datadir=/var/lib/mysql character-set-server=utf8 init_connect=’SET NAMES utf8′ 设定了默认...

    mysql 数据库中my.ini的优化 2G内存针对站多 抗压型的设置

    默认为2402,调到512-1024最佳 innodb_additional_mem_pool_size=4M 默认为2M innodb_flush_log_at_trx_commit=1 (设置为0就是等到innodb_log_buffer_size列队满后再统一储存,默认为1) innodb_log_buffer_size=2M ...

    mysql数据库my.cnf配置文件

    # 0:如果innodb_flush_log_at_trx_commit的值为0,log buffer每秒就会被刷写日志文件到磁盘,提交事务的时候不做任何操作(执行是由mysql的master thread线程来执行的。 # 主线程中每秒会将重做日志缓冲写入磁盘的...

    InnoDBinsert性能拐点测试

    上篇blog《InnoDBselect性能拐点测试》测试了InnoDBselect的性能拐点,...  1、调整my.cnf的参数如下:  innodb_file_per_table=0  innodb_flush_log_at_trx_commit=2  innodb_buffer_pool_size=8G  innodb_file_i

    mysql数据库插入速度和读取速度的调整记录

    这次修改了下面四个配置项: 1)将 innodb_flush_log_at_trx_commit 配置设定为0;按过往经验设定为0,插入速度会有很大提高。 0: Write the log buffer to the log file and flush the log file every second, but ...

Global site tag (gtag.js) - Google Analytics