如果innodb_flush_log_at_trx_commit設置爲2,每次事務提交時MySQL都會把log buffer的數據寫入log file.但是flush(刷到磁盤)操作並不會同時進行。該模式下,MySQL會每秒執行一次 flush(刷到磁盤)操作。
sync_binlog與innodb_flush_log_at_trx_commit不同設置的場景(sysbench壓測結果)
innodb_flush_log_at_trx_commit
主要針對innodb的redo從log_buffer到log file的同步而設置的
如果innodb_flush_log_at_trx_commit設置爲0,log buffer將每秒一次地寫入log file中,並且log file的flush(刷到磁盤)操作同時進行.該模式下,在事務提交的時候,不會主動觸發寫入磁盤的操作。
如果innodb_flush_log_at_trx_commit設置爲1,每次事務提交時MySQL都會把log buffer的數據寫入log file,並且flush(刷到磁盤)中去.如果innodb_flush_log_at_trx_commit設置爲2,每次事務提交時MySQL都會把log buffer的數據寫入log file.但是flush(刷到磁盤)操作並不會同時進行。該模式下,MySQL會每秒執行一次 flush(刷到磁盤)操作。
sync_binlog
設置二進制日誌從binlog_buffer中刷新到磁盤的頻率。對MySQL系統來說,它是至關重要的參數,不僅影響到binlog對MySQL帶來的性能損耗,而且還影響到MySQL中數據的完整性。
sync_binlog 的默認值是0,像操作系統刷其他文件的機制一樣,MySQL不會同步到磁盤中去而是依賴操作系統來刷新binary log。
當sync_binlog =N (N>0) ,MySQL 在每寫 N次 二進制日誌binary log時,會使用fdatasync()函數將它的寫二進制日誌binary log同步到磁盤中去。
壓測語句:
sysbench --test=/root/sysbench-0.5/sysbench/tests/db/oltp.lua --oltp-table-size=1000000 --oltp-read-only=off --init-rng=on --num-threads=16 --max-requests=0 --oltp-dist-type=uniform --max-time=180 --mysql-user=root --mysql-socket=/tmp/mysqld.sock --mysql-password='' --db-driver=mysql --mysql-table-engine=innodb --oltp-test-mode=complex run
OLTP test statistics:
queries performed:
read: 480872
write: 137392
other: 68696
total: 686960
transactions: 34348 (190.78 per sec.)
deadlocks: 0 (0.00 per sec.)
read/write requests: 618264 (3434.07 per sec.)
other operations: 68696 (381.56 per sec.)
Test execution summary:
total time: 180.0381s
total number of events: 34348
total time taken by event execution: 2880.2442s
per-request statistics:
min: 5.07ms
avg: 83.85ms
max: 290.99ms
approx. 95 percentile: 108.72ms
Threads fairness:
events (avg/stddev): 2146.7500/6.87
execution time (avg/stddev): 180.0153/0.01
場景2: sync_binlog=1 & innodb_flush_log_at_trx_commit=1
OLTP test statistics:
queries performed:
read: 455504
write: 130144
other: 65072
total: 650720
transactions: 32536 (180.71 per sec.)
deadlocks: 0 (0.00 per sec.)
read/write requests: 585648 (3252.79 per sec.)
other operations: 65072 (361.42 per sec.)
Test execution summary:
total time: 180.0447s
total number of events: 32536
total time taken by event execution: 2880.1853s
per-request statistics:
min: 34.42ms
avg: 88.52ms
max: 163.85ms
approx. 95 percentile: 115.39ms
Threads fairness:
events (avg/stddev): 2033.5000/6.78
execution time (avg/stddev): 180.0116/0.02
場景3:sync_binlog=1000 & innodb_flush_log_at_trx_commit=1
OLTP test statistics:
queries performed:
read: 455420
write: 130120
other: 65060
total: 650600
transactions: 32530 (180.69 per sec.)
deadlocks: 0 (0.00 per sec.)
read/write requests: 585540 (3252.45 per sec.)
other operations: 65060 (361.38 per sec.)
Test execution summary:
total time: 180.0306s
total number of events: 32530
total time taken by event execution: 2879.9498s
per-request statistics:
min: 24.78ms
avg: 88.53ms
max: 175.68ms
approx. 95 percentile: 113.50ms
Threads fairness:
events (avg/stddev): 2033.1250/6.57
execution time (avg/stddev): 179.9969/0.02
場景4: sync_binlog=1000 & innodb_flush_log_at_trx_commit=2
OLTP test statistics:
queries performed:
read: 491624
write: 140464
other: 70232
total: 702320
transactions: 35116 (195.04 per sec.)
deadlocks: 0 (0.00 per sec.)
read/write requests: 632088 (3510.76 per sec.)
other operations: 70232 (390.08 per sec.)
Test execution summary:
total time: 180.0430s
total number of events: 35116
total time taken by event execution: 2880.1055s
per-request statistics:
min: 26.98ms
avg: 82.02ms
max: 166.06ms
approx. 95 percentile: 105.86ms
Threads fairness:
events (avg/stddev): 2194.7500/7.55
execution time (avg/stddev): 180.0066/0.03
結論:
sync_binlog等於1,innodb_flush_log_at_trx_commit=1時數據最安全,但性能最差,TPS最低
sync_binlog>1,innodb_flush_log_at_trx_commit=2 時性能最好。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.