本文的示例代码参考conf_opt
目录
脚本
环境
测试-默认配置
测试-innodb_buffer_pool_size+innodb_flush_log_at_trx_commit
脚本
vim insert.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import pymysql
import time
create_table_sql = """
CREATE TABLE users(
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) UNIQUE,
nickname VARCHAR(255) NOT NULL
)
"""
insert_table_sql = """
INSERT INTO users(username, nickname)
VALUES(%s,%s)
"""
conn = pymysql.connect(host='47.99.194.4', user='root',
passwd='123456', db='sbtest', port=4408, charset='utf8')
cur = conn.cursor()
cur.execute('DROP TABLE IF EXISTS users')
cur.execute(create_table_sql)
conn.commit()
print('开始时间: '+ time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
start = time.time()
failed = 0 # 插入失败条数
count = 10000 # 插入1W条数据
for i in range(1, count + 1):
username = 'username_' + str(i)
nickname = 'nickname_' + str(i)
try:
cur.execute(insert_table_sql, (username, nickname))
conn.commit()
except:
print('failed')
end = time.time()
print('结束时间: '+ time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
print('消耗秒数: ' + str(int(end - start)))
print('失败条数: ' + str(failed))
vim insert.php
query('DROP TABLE IF EXISTS users');
$conn->query($create_table_sql);
echo '开始时间: ' . date("Y-m-d H:i:s") . "\n";
$start_ts = time();
$failed = 0; // 插入失败条数
$count = 10000; // 插入1W条数据
$i = 1;
while ($i <= $count) {
$username = 'username_' . $i;
$nickname = 'nickname_' . $i;
try {
$conn->query("INSERT INTO users(username, nickname) VALUES('$username', '$nickname')");
$conn->commit();
} catch (mysqli_sql_exception $e) {
$failed++;
echo $e->getMessage();
}
$i++;
}
$end_ts = time();
echo '结束时间: ' . date("Y-m-d H:i:s") . "\n";
echo '消耗秒数: ' . ($end_ts - $start_ts) . "\n";
echo '失败条数: ' . $failed;
$conn->close();
环境
阿里云: 2vCPU + 8G Memory + 100M带宽 + I/O优化
MySQL:
mysqld --version
# mysqld Ver 5.7.24-0ubuntu0.16.04.1 for Linux on x86_64 ((Ubuntu))
- sysbench
sysbench --version
# sysbench 1.0.15
- Python
python --version
# Python 3.5.2
- PHP
php --version
# PHP 7.1.15 (cli) (built: May 2 2018 13:57:10) ( NTS )
测试-默认配置
python insert.py
开始时间: 2018-11-19 11:41:11
结束时间: 2018-11-19 11:45:24
消耗秒数: 253
失败条数: 0
php insert.php
开始时间: 2018-11-19 03:58:50
结束时间: 2018-11-19 04:03:06
消耗秒数: 256
失败条数: 0
sysbench --mysql-host=47.99.194.4 \
--mysql-port=4408 \
--mysql-user=root \
--mysql-password=123456 \
/usr/local/share/sysbench/oltp_common.lua \
--tables=10 \
--table_size=100000 \
prepare
sysbench --mysql-host=47.99.194.4 \
--mysql-port=4408 \
--mysql-user=root \
--mysql-password=123456 \
/usr/local/share/sysbench/oltp_read_write.lua \
--tables=10 \
--table_size=100000 \
--time=60 \
--report-interval=10 \
--threads=5 \
run
[ 10s ] thds: 5 tps: 18.80 qps: 380.33 (r/w/o: 266.95/75.29/38.09) lat (ms,95%): 350.33 err/s: 0.00 reconn/s: 0.00
[ 20s ] thds: 5 tps: 19.70 qps: 394.41 (r/w/o: 275.90/79.10/39.40) lat (ms,95%): 303.33 err/s: 0.00 reconn/s: 0.00
[ 30s ] thds: 5 tps: 18.60 qps: 372.50 (r/w/o: 261.00/74.30/37.20) lat (ms,95%): 450.77 err/s: 0.00 reconn/s: 0.00
[ 40s ] thds: 5 tps: 19.39 qps: 387.15 (r/w/o: 271.10/77.27/38.79) lat (ms,95%): 292.60 err/s: 0.00 reconn/s: 0.00
[ 50s ] thds: 5 tps: 17.40 qps: 346.70 (r/w/o: 242.20/69.70/34.80) lat (ms,95%): 539.71 err/s: 0.00 reconn/s: 0.00
[ 60s ] thds: 5 tps: 19.11 qps: 385.31 (r/w/o: 270.08/77.02/38.21) lat (ms,95%): 331.91 err/s: 0.00 reconn/s: 0.00
SQL statistics:
queries performed:
read: 15890
write: 4540
other: 2270
total: 22700
transactions: 1135 (18.85 per sec.)
queries: 22700 (377.01 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 60.2100s
total number of events: 1135
Latency (ms):
min: 219.47
avg: 264.69
max: 829.04
95th percentile: 337.94
sum: 300428.48
Threads fairness:
events (avg/stddev): 227.0000/9.34
execution time (avg/stddev): 60.0857/0.07
sysbench --mysql-host=47.99.194.4 \
--mysql-port=4408 \
--mysql-user=root \
--mysql-password=123456 \
/usr/local/share/sysbench/oltp_common.lua \
--tables=10 \
cleanup
测试-innodb_buffer_pool_size+innodb_flush_log_at_trx_commit
SET GLOBAL innodb_buffer_pool_size=4294967296;
SHOW STATUS WHERE Variable_name='InnoDB_buffer_pool_resize_status';
+----------------------------------+----------------------------------------------------+
| Variable_name | Value |
|----------------------------------+----------------------------------------------------|
| Innodb_buffer_pool_resize_status | Completed resizing buffer pool at 181119 13:34:18. |
+----------------------------------+----------------------------------------------------+
SELECT @@innodb_buffer_pool_size/1024/1024/1024;
+--------------------------------------------+
| @@innodb_buffer_pool_size/1024/1024/1024 |
|--------------------------------------------|
| 4.000000000000 |
+--------------------------------------------+
SET GLOBAL innodb_flush_log_at_trx_commit=2;
SELECT @@innodb_flush_log_at_trx_commit;
+------------------------------------+
| @@innodb_flush_log_at_trx_commit |
|------------------------------------|
| 2 |
+------------------------------------+
python insert.py
开始时间: 2018-11-19 13:36:01
结束时间: 2018-11-19 13:40:08
消耗秒数: 246
失败条数: 0
php insert.php
开始时间: 2018-11-19 05:40:42
结束时间: 2018-11-19 05:44:23
消耗秒数: 221
失败条数: 0
sysbench --mysql-host=47.99.194.4 \
--mysql-port=4408 \
--mysql-user=root \
--mysql-password=123456 \
/usr/local/share/sysbench/oltp_common.lua \
--tables=10 \
--table_size=100000 \
prepare
sysbench --mysql-host=47.99.194.4 \
--mysql-port=4408 \
--mysql-user=root \
--mysql-password=123456 \
/usr/local/share/sysbench/oltp_read_write.lua \
--tables=10 \
--table_size=100000 \
--time=60 \
--report-interval=10 \
--threads=5 \
run
[ 10s ] thds: 5 tps: 20.49 qps: 413.82 (r/w/o: 290.37/81.96/41.48) lat (ms,95%): 277.21 err/s: 0.00 reconn/s: 0.00
[ 20s ] thds: 5 tps: 20.91 qps: 419.91 (r/w/o: 294.28/83.82/41.81) lat (ms,95%): 262.64 err/s: 0.00 reconn/s: 0.00
[ 30s ] thds: 5 tps: 20.70 qps: 414.04 (r/w/o: 289.66/82.99/41.39) lat (ms,95%): 277.21 err/s: 0.00 reconn/s: 0.00
[ 40s ] thds: 5 tps: 21.30 qps: 422.59 (r/w/o: 295.19/84.80/42.60) lat (ms,95%): 277.21 err/s: 0.00 reconn/s: 0.00
[ 50s ] thds: 5 tps: 21.10 qps: 423.62 (r/w/o: 296.84/84.58/42.19) lat (ms,95%): 272.27 err/s: 0.00 reconn/s: 0.00
[ 60s ] thds: 5 tps: 20.90 qps: 419.35 (r/w/o: 293.83/83.81/41.70) lat (ms,95%): 262.64 err/s: 0.00 reconn/s: 0.00
SQL statistics:
queries performed:
read: 17626
write: 5036
other: 2518
total: 25180
transactions: 1259 (20.89 per sec.)
queries: 25180 (417.75 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 60.2737s
total number of events: 1259
Latency (ms):
min: 211.87
avg: 238.74
max: 517.04
95th percentile: 272.27
sum: 300567.43
Threads fairness:
events (avg/stddev): 251.8000/6.27
execution time (avg/stddev): 60.1135/0.09
sysbench --mysql-host=47.99.194.4 \
--mysql-port=4408 \
--mysql-user=root \
--mysql-password=123456 \
/usr/local/share/sysbench/oltp_common.lua \
--tables=10 \
cleanup
优化innodb_buffer_pool_size+innodb_flush_log_at_trx_commit对性能有大约10%的提升