首页 > 技术文章 > mysql调优

cainiaoit 2018-03-16 11:44 原文

innodb_flush_log_at_trx_commit

0:log buffer将每秒一次地写入log file中,并且log file的flush(刷到磁盘)操作同时进行。该模式下在事务提交的时候,不会主动触发写入磁盘的操作。

1:每次事务提交时MySQL都会把log buffer的数据写入log file,并且flush(刷到磁盘)中去,该模式为系统默认。

2:每次事务提交时MySQL都会把log buffer的数据写入log file,但是flush(刷到磁盘)操作并不会同时进行。该模式下,MySQL会每秒执行一次 flush(刷到磁盘)操作。

 

innodb_autoextend_increment(动态,默认为8M)

当自动扩展表空间被填满之时,为扩展而增加的尺寸(MB为单位)。

 

sync_binlog:是MySQL 的二进制日志(binary log)同步到磁盘的频率。

取值:0-N

sync_binlog=0,当事务提交之后,MySQL不做fsync之类的磁盘同步指令刷新binlog_cache中的信息到磁盘,而让Filesystem自行决定什么时候来做同步,或者cache满了之后才同步到磁盘。这个是性能最好的。

sync_binlog=1,当每进行1次事务提交之后,MySQL将进行一次fsync之类的磁盘同步指令来将binlog_cache中的数据强制写入磁盘。

sync_binlog=n,当每进行n次事务提交之后,MySQL将进行一次fsync之类的磁盘同步指令来将binlog_cache中的数据强制写入磁盘。

 

了解了上面的三个参数后开始调优,准备测试脚本

 

 

# -*- coding:utf-8 -*-
import threading
import time
import MySQLdb

start = time.clock()
db = MySQLdb.connect("IP","账号","密码","库名" )
cursor = db.cursor()

class SummingThread(threading.Thread):
    def __init__(self, low, high):
        super(SummingThread, self).__init__()
        self.low = low
        self.high = high
        self.total = 0
 
    def run(self):
        for i in range(self.low, self.high):
            try:
                # 执行sql语句
                cursor.execute("自行写入")
                db.commit()
                # 提交到数据库执行
            except:

                db.rollback()
            
                self.total += i
                
            print(i)
 #指定执行次数
thread1 = SummingThread(0, 1000000)
thread1.start()
thread1.join()
result = thread1.total 
end = time.clock()
print "read: %f s" % (end - start)
print(result)

 

调优结果

 

read: 11.400000 s
read: 11.790000 s
innodb_flush_log_at_trx_commit = 2



read: 7.490000 s
read: 7.480000 s
read: 7.700000 s
read: 8.810000 s
read: 9.080000 s
read: 9.380000 s



innodb_flush_log_at_trx_commit = 2
innodb_autoextend_increment = 256


read: 7.300000 s
read: 6.820000 s
read: 6.840000 s
read: 8.640000 s
read: 8.440000 s

innodb_flush_log_at_trx_commit = 2
innodb_autoextend_increment = 256
sync_binlog = 500

read: 7.810000 s
read: 7.310000 s
read: 7.740000 s

 

推荐阅读