012、索引及壓力測試實驗
阿新 • • 發佈:2021-06-16
索引分類介紹
B+樹索引分為:聚集索引(可以理解為主鍵)和輔助索引(普通索引)。聚集索引:可以理解為主鍵,索引和資料是在一起的,所以經常講,innodb表資料即索引,索引即資料。輔助索引又分為:單列索引、多列聯合索引、全文索引、字首索引。輔助索引:也叫非聚集索引,葉子級別不包含行的全部資料,葉節點除了包含索引本身鍵值以外,每個葉級別中的索引行中還包含一個書籤,該書籤就是相應行資料的聚集索引鍵,資料查詢會根據聚集索引鍵去聚集索引查詢資料。聯合索引:是指對錶上的多個列作索引,可以更好的縮短段池的範圍,更快的檢索到資料,在使用過程中,要遵循最左字首原則。但是不管是聚集索引還是非聚集索引,其內部都是B+樹的,即高度是平衡的,葉子節點存放著所有的資料
索引的建立和刪除
B+樹索引的建立和刪除可以通過兩種方法:- alter table
- create/drop index
構造資料
1、建立實驗表2、編寫儲存過程mysql> use test; Database changed mysql> create table su( -> id int unsigned not null auto_increment, -> c1 int not null default '0', -> c2 int not null default '0', -> c3 int not null default '0', -> c4 int not null default '0', -> c5 timestamp not null, -> c6 varchar(200) not null default '', -> primary key(id) -> ); Query OK, 0 rows affected (0.09 sec)
mysql> delimiter $$
mysql> drop procedure if exists `insert_su` $$
Query OK, 0 rows affected (0.00 sec)
mysql> create procedure `insert_su`(in row_num int)
-> begin
-> declare i int default 0;
-> while i < row_num do
-> insert into su(c1,c2,c3,c4,c5,c6) values(floor(rand()*row_num),floor(rand()*row_num),floor(rand()*row_num),floor(rand()*row_num),now(),repeat('su',floor(rand()*20)));
-> set i = i+1;
-> end while;
-> end$$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
3、呼叫儲存過程插入資料mysql> call insert_su(50000);
Query OK, 1 row affected (8 min 40.95 sec)
mysql> select count(*) from su;
+----------+
| count(*) |
+----------+
| 50000 |
+----------+
1 row in set (0.01 sec)
檢視執行計劃
1、執行sql語句mysql> select * from su a,(select c2 from su where id=10) b where a.c2=b.c2; --括號中的部分會先執行
+-------+-------+-------+-------+-------+---------------------+----------------------------------+-------+
| id | c1 | c2 | c3 | c4 | c5 | c6 | c2 |
+-------+-------+-------+-------+-------+---------------------+----------------------------------+-------+
| 10 | 45855 | 15814 | 41507 | 10093 | 2021-04-14 16:23:57 | susususususususususu | 15814 |
| 25642 | 20821 | 15814 | 16607 | 35591 | 2021-04-14 16:28:22 | sususususususususususu | 15814 |
| 34191 | 40308 | 15814 | 8147 | 43293 | 2021-04-14 16:29:53 | susususususususususususususususu | 15814 |
+-------+-------+-------+-------+-------+---------------------+----------------------------------+-------+
3 rows in set (0.04 sec)
--這個sql是自連線查詢,含義是:將所有與(id為10的c2列)值相同的記錄篩選出來
2、檢視一張表有什麼索引mysql> show index from su;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| su | 0 | PRIMARY | 1 | id | A | 49919 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
3、檢視sql語句的執行計劃用explain/desc 來檢視sql的執行計劃,sql本身並沒有執行,只是看plan:mysql> desc select * from su a,(select c2 from su where id=10) b where a.c2=b.c2;
+----+-------------+------------+--------+---------------+---------+---------+-------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-------+-------+-------------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL |
| 1 | PRIMARY | a | ALL | NULL | NULL | NULL | NULL | 49919 | Using where |
| 2 | DERIVED | su | const | PRIMARY | PRIMARY | 4 | const | 1 | NULL |
+----+-------------+------------+--------+---------------+---------+---------+-------+-------+-------------+
3 rows in set (0.00 sec)
mysql> explain select * from su a,(select c2 from su where id=10) b where a.c2=b.c2;
+----+-------------+------------+--------+---------------+---------+---------+-------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-------+-------+-------------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL |
| 1 | PRIMARY | a | ALL | NULL | NULL | NULL | NULL | 49919 | Using where |
| 2 | DERIVED | su | const | PRIMARY | PRIMARY | 4 | const | 1 | NULL |
+----+-------------+------------+--------+---------------+---------+---------+-------+-------+-------------+
3 rows in set (0.00 sec)
4、執行計劃的檢視要點- 先看type列,當type列出現all這樣的關鍵字的時候,代表全表掃描
- 再看key列,然後看rows(行數)一般情況下,rows的值不應超過5000行。
- 最後看Extra,額外資訊池。Using filesort 代表磁碟排序,Using index 證明使用到了覆蓋索引。
mysql> select count(distinct c2)/count(*) from su;
+-----------------------------+
| count(distinct c2)/count(*) |
+-----------------------------+
| 0.6314 |
+-----------------------------+
1 row in set (0.03 sec)
值越接近於1,選擇性越高,越適合建立索引。建立索引
B+樹索引的建立和刪除可以通過兩種方法:- alter table
- create/drop index
1、建立單列索引
mysql> alter table su add index idx_c2(c2);
Query OK, 0 rows affected (0.25 sec)
Records: 0 Duplicates: 0 Warnings: 0
或者:mysql> create index idx_c2 on su(c2);
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看錶結構:mysql> show create table su \G;
*************************** 1. row ***************************
Table: su
Create Table: CREATE TABLE `su` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`c1` int(11) NOT NULL DEFAULT '0',
`c2` int(11) NOT NULL DEFAULT '0',
`c3` int(11) NOT NULL DEFAULT '0',
`c4` int(11) NOT NULL DEFAULT '0',
`c5` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`c6` varchar(200) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `idx_c2` (`c2`)
) ENGINE=InnoDB AUTO_INCREMENT=50001 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
查看錶su有哪些索引:mysql> show index from su;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| su | 0 | PRIMARY | 1 | id | A | 49919 | NULL | NULL | | BTREE | | |
| su | 1 | idx_c2 | 1 | c2 | A | 49919 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
2、再次檢視執行計劃mysql> explain select * from su a,(select c2 from su where id=10) b where a.c2=b.c2;
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL |
| 1 | PRIMARY | a | ref | idx_c2 | idx_c2 | 4 | const | 3 | NULL |
| 2 | DERIVED | su | const | PRIMARY | PRIMARY | 4 | const | 1 | NULL |
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
3 rows in set (0.00 sec)
可以看到key列已經使用了索引,rows減少了很多。覆蓋索引:查詢的欄位,作為檢索條件的時候又是索引。不回表直接可以從索引中,找到我想要的資料。
mysql> explain select c2 from su where c2=111;
+----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
| 1 | SIMPLE | su | ref | idx_c2 | idx_c2 | 4 | const | 2 | Using index |
+----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
1 row in set (0.08 sec)
2、建立聯合索引
聯合索引必須滿足最左字首原則(a,b,c),語法如下:mysql> create index idx_c3_c4 on su(c3,c4);
Query OK, 0 rows affected (0.31 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from su;
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| su | 0 | PRIMARY | 1 | id | A | 49919 | NULL | NULL | | BTREE | | |
| su | 1 | idx_c2 | 1 | c2 | A | 49919 | NULL | NULL | | BTREE | | |
| su | 1 | idx_c3_c4 | 1 | c3 | A | 49919 | NULL | NULL | | BTREE | | |
| su | 1 | idx_c3_c4 | 2 | c4 | A | 49919 | NULL | NULL | | BTREE | | |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)
檢視以下幾種模式下的執行計劃:以c3為where條件進行查詢:mysql> desc select * from su where c3=40680;
+----+-------------+-------+------+---------------+-----------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+-----------+---------+-------+------+-------+
| 1 | SIMPLE | su | ref | idx_c3_c4 | idx_c3_c4 | 4 | const | 1 | NULL |
+----+-------------+-------+------+---------------+-----------+---------+-------+------+-------+
1 row in set (0.00 sec)
以c4為條件進行查詢:mysql> desc select * from su where c4=40680;
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | su | ALL | NULL | NULL | NULL | NULL | 49919 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
1 row in set (0.00 sec)
以c3為條件查詢,並按照c4排序:mysql> desc select * from su where c3=40680 order by c4;
+----+-------------+-------+------+---------------+-----------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+-----------+---------+-------+------+-------------+
| 1 | SIMPLE | su | ref | idx_c3_c4 | idx_c3_c4 | 4 | const | 1 | Using where |
+----+-------------+-------+------+---------------+-----------+---------+-------+------+-------------+
1 row in set (0.00 sec)
以c4為條件查詢,按照c3排序:mysql> desc select * from su where c4=40680 order by c3;
+----+-------------+-------+------+---------------+------+---------+------+-------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+-----------------------------+
| 1 | SIMPLE | su | ALL | NULL | NULL | NULL | NULL | 49919 | Using where; Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+-------+-----------------------------+
1 row in set (0.00 sec)
查詢c3或者c4:使用or的查詢,無法使用索引,除非建立兩個單列索引在c3、c4列上。mysql> desc select * from su where c3=40680 or c4=40680;
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | su | ALL | idx_c3_c4 | NULL | NULL | NULL | 49919 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+-------+-------------+
1 row in set (0.00 sec)
查詢c3和c4:mysql> desc select * from su where c3=40680 and c4=40680;
+----+-------------+-------+------+---------------+-----------+---------+-------------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+-----------+---------+-------------+------+-------+
| 1 | SIMPLE | su | ref | idx_c3_c4 | idx_c3_c4 | 8 | const,const | 1 | NULL |
+----+-------------+-------+------+---------------+-----------+---------+-------------+------+-------+
1 row in set (0.00 sec)
結論:查詢條件滿足最左字首原則會使用索引。建議:聯合索引的建立,其中一個欄位用於檢索,另外的欄位用於排序,這樣可能會避免磁碟排序(Using filesort)。order by後的欄位,最好也建立索引。查詢語句中,只能用到一個(索引)index。用到兩個索引的情況:or語句中兩個列都建立了索引,type:index_merge(索引合併)。mysql> drop index idx_c3_c4 on su;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index idx_c3 on su(c3);
Query OK, 0 rows affected (0.27 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create index idx_c4 on su(c4);
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc select * from su where c3=40680 or c4=40680;
+----+-------------+-------+-------------+---------------+---------------+---------+------+------+-----------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------------+---------------+---------------+---------+------+------+-----------------------------------------+
| 1 | SIMPLE | su | index_merge | idx_c3,idx_c4 | idx_c3,idx_c4 | 4,4 | NULL | 2 | Using union(idx_c3,idx_c4); Using where |
+----+-------------+-------+-------------+---------------+---------------+---------+------+------+-----------------------------------------+
1 row in set (0.00 sec)
or子句如何優化:- 我們可以在or前後欄位加兩個單列索引;
- 把or子句,改成union al;
3、建立全文索引
InnoDB引擎對FULLTEXT索引的支援是MySQL5.6新引入的特性,之前只有MyISAM引擎支援FULLTEXT索引。對於FULLTEXT索引的內容可以使用MATCH()…AGAINST()語法進行查詢。mysql> create fulltext index idx_c6 on su(c6);
Query OK, 0 rows affected, 1 warning (1.78 sec)
Records: 0 Duplicates: 0 Warnings: 1
檢視執行計劃:mysql> explain select * from su where match(c6) against('sususu');
+----+-------------+-------+----------+---------------+--------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+----------+---------------+--------+---------+------+------+-------------+
| 1 | SIMPLE | su | fulltext | idx_c6 | idx_c6 | 0 | NULL | 1 | Using where |
+----+-------------+-------+----------+---------------+--------+---------+------+------+-------------+
1 row in set (0.00 sec)
4、字首索引
建立索引時,只取欄位值的前n個字母或者字元就可以確定該欄位的值,不需要給整個欄位建立索引。--create index idx_name on table_name (name(5));
mysql> create index idx_c6 on su(c6(5));
Query OK, 0 rows affected (0.37 sec)
Records: 0 Duplicates: 0 Warnings: 0
檢視執行計劃:mysql> explain select * from su where c6='ab';
+----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
| 1 | SIMPLE | su | ref | idx_c6 | idx_c6 | 17 | const | 1 | Using where |
+----+-------------+-------+------+---------------+--------+---------+-------+------+-------------+
1 row in set (0.00 sec)
索引對null值的處理
建立測試表:mysql> create table tb_1(
-> id int unsigned not null auto_increment,
-> c1 varchar(200) default null,
-> c2 int not null,
-> primary key(id)
-> );
Query OK, 0 rows affected (0.18 sec)
插入資料:mysql> insert into tb_1(c1,c2) values(null,1),(1,2),(null,3);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
查詢:mysql> select * from tb_1;
+----+------+----+
| id | c1 | c2 |
+----+------+----+
| 1 | NULL | 1 |
| 2 | 1 | 2 |
| 3 | NULL | 3 |
+----+------+----+
3 rows in set (0.00 sec)
mysql> select count(c1),count(*),count(1),count(c2),count(id) from tb_1;
+-----------+----------+----------+-----------+-----------+
| count(c1) | count(*) | count(1) | count(c2) | count(id) |
+-----------+----------+----------+-----------+-----------+
| 1 | 3 | 3 | 3 | 3 |
+-----------+----------+----------+-----------+-----------+
1 row in set (0.00 sec)
結論:索引不包含null值。刪除tb_1表,修改sql,重新建立:mysql> drop table tb_1;
Query OK, 0 rows affected (0.05 sec)
mysql> create table tb_1(
-> id int unsigned not null auto_increment,
-> c1 varchar(200) default '',
-> c2 int not null,
-> primary key(id)
-> );
Query OK, 0 rows affected (0.15 sec)
插入測試資料:mysql> insert into tb_1(c1,c2) values('',1),(1,2),('',3);
Query OK, 3 rows affected (0.09 sec)
Records: 3 Duplicates: 0 Warnings: 0
檢視結果:mysql> select count(c1),count(*),count(1),count(c2),count(id) from tb_1;
+-----------+----------+----------+-----------+-----------+
| count(c1) | count(*) | count(1) | count(c2) | count(id) |
+-----------+----------+----------+-----------+-----------+
| 3 | 3 | 3 | 3 | 3 |
+-----------+----------+----------+-----------+-----------+
1 row in set (0.00 sec)
結論:在索引中,不儲存null值,但是儲存空字串的值。所以,建立表,欄位上面有索引時,定義欄位預設值時,修改default null為default '' 取代null.另外,where條件中,函式不使用索引:select * from su where YEAR(adddate)<2007;建議符號左邊不要進行函式運算,將函式放在符號右側。索引優點:提高檢索效率,提高聚合函式的效率(max,min,avg),提高排序效率,表連線。使用不到索引的情況:
- 通過索引掃描的記錄數超過30%,變成全表掃描 *
- 聯合索引中,第一個索引列使用範圍查詢,所謂範圍查詢就是查詢一個區間的值,比如:BETWEEN
- 聯合索引中,第一個查詢條件不是最左索引列
- 模糊查詢條件列最左以萬用字元 % 開始
- 記憶體表(HEAP 表)使用HASH索引時,使用範圍檢索或者ORDER BY
- 兩個獨立索引,其中一個用於檢索,一個用於排序
- 使用了不同的 ORDER BY 和 GROUP BY 表示式
書寫壓力測試報告
新業務上線之前,需要對資料庫進行壓力測試。目的是為反映出資料庫能處理的事務量和資料庫能處理的請求數(TPS,QPS的值)。- 介紹新上線業務系統的名稱;
- 介紹資料庫的基礎資訊:資料庫架構(主從、主主、主從從)、DB的相關引數(主要是重要引數,innodb_buffer_pool,redo log的重新整理機制,binlog的重新整理機制,髒頁page的重新整理機制);
- 介紹作業系統的資訊(CPU核數、是否做超執行緒,記憶體大小,swap大小,磁碟型別、磁碟陣列,CPU是否最大效能模式);
通過測試工具sysbench反應出資料庫(TPS,QPS的值)。
安裝sysbench
1、上傳軟體包,並解壓:[root@localhost soft]# pwd
/u01/soft
[root@localhost soft]# ls
sysbench-0.4.8.tar.gz
[root@localhost soft]# gunzip sysbench-0.4.8.tar.gz
[root@localhost soft]# ls
sysbench-0.4.8.tar
[root@localhost soft]# tar -xvf sysbench-0.4.8.tar
2、編譯,指定MySQL的lib庫[root@localhost sysbench-0.4.8]# pwd
/u01/soft/sysbench-0.4.8
[root@localhost sysbench-0.4.8]# ls
acinclude.m4 autogen.sh config configure.ac doc install-sh Makefile.in mkinstalldirs scripts TODO
aclocal.m4 ChangeLog configure COPYING INSTALL Makefile.am missing README sysbench
編譯過程中如果報錯,可能是lib包不全,使用yum安裝libtool之後再進行編譯。[root@localhost ~]# yum install libtool
[root@localhost sysbench-0.4.8]# ./configure --with-mysql-includes=/usr/local/mysql/include --with-mysql-libs=/usr/local/mysql/lib
3、安裝[root@localhost sysbench-0.4.8]# pwd
/u01/soft/sysbench-0.4.8
[root@localhost sysbench-0.4.8]# ls
acinclude.m4 ChangeLog config.status COPYING install-sh Makefile.am mkinstalldirs sysbench
aclocal.m4 config configure doc libtool Makefile.in README TODO
autogen.sh config.log configure.ac INSTALL Makefile missing scripts
[root@localhost sysbench-0.4.8]# make
[root@localhost sysbench-0.4.8]# make install
4、設定環境變數安裝完成後,會產生一個可執行檔案,執行時報錯:[root@localhost sysbench-0.4.8]# cd sysbench/
[root@localhost sysbench]# ls
db_driver.c drivers Makefile.in sb_logger.h sb_options.h sb_timer.h sysbench.c tests
db_driver.h Makefile sb_list.h sb_logger.o sb_options.o sb_timer.o sysbench.h
db_driver.o Makefile.am sb_logger.c sb_options.c sb_timer.c sysbench sysbench.o
[root@localhost sysbench]# ./sysbench
./sysbench: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
需要設定環境變數:[root@localhost ~]# vi /etc/profile
--新增內容:export LD_LIBRARY_PATH=/usr/local/mysql/lib
[root@localhost ~]# source /etc/profile
4、壓力測試之前構造資料[root@localhost sysbench]# ./sysbench --test=oltp --mysql-table-engine=innodb --mysql-db=test --oltp-table-size=100000 \
> --db-driver=mysql --mysql-socket=/tmp/mysql.sock --mysql-user=root --mysql-host=localhost --mysql-password=root prepare
--準備10萬條資料
5、執行壓力測試[root@localhost sysbench]# ./sysbench --test=oltp --mysql-table-engine=innodb --mysql-db=test --oltp-table-size=100000 \
> --db-driver=mysql --num-threads=2 --mysql-socket=/tmp/mysql.sock \
> --mysql-user=root --mysql-host=localhost --mysql-password=root run
sysbench v0.4.8: multi-threaded system evaluation benchmark
WARNING: Preparing of "BEGIN" is unsupported, using emulation
(last message repeated 1 times)
Running the test with following options:
Number of threads: 2
Doing OLTP test.
Running mixed OLTP test
Using Special distribution (12 iterations, 1 pct of values are returned in 75 pct cases)
Using "BEGIN" for starting transactions
Using auto_inc on the id column
Maximum number of requests for OLTP test is limited to 10000
Threads started!
Done.
OLTP test statistics:
queries performed:
read: 140000
write: 50000
other: 20000
total: 210000
transactions: 10000 (114.84 per sec.) --每秒處理事務數(tps)
deadlocks: 0 (0.00 per sec.)
read/write requests: 190000 (2181.94 per sec.) --每秒請求數(qps)
other operations: 20000 (229.68 per sec.)
Test execution summary:
total time: 87.0783s
total number of events: 10000
total time taken by event execution: 174.0889
per-request statistics:
min: 0.0072s
avg: 0.0174s
max: 0.6707s
approx. 95 percentile: 0.0491s
Threads fairness:
events (avg/stddev): 5000.0000/5.00
execution time (avg/stddev): 87.0444/0.00
引數說明:--number-threads=8表示發起8個併發連線 *cpu--oltp-read-only=off表示不要進行只讀測試,也就是會採用讀寫混合模式測試--report-interval=10表示每10秒輸出一次測試進度報告*--rand-type=uniform表示隨即型別為固定模式,其他幾個可選隨機模式:uniform(固定),gaussian(高斯),special(特定的),pareto(帕累託)--max-time=120表示最大執行時長為120秒 *--max-requests=0表示總請求數為0,因為上面已經定義了總執行時長,所以總請求數可以設定為0;也可以只設定總請求數,不設定最大執行時長--percentile=99表示設定取樣比例,預設值時95%,99表示丟棄1%的長請求,再剩餘的99%裡取最大值。
可以通過調整資料庫引數使tps和qps更高:調整redo log和binlog的重新整理引數:
mysql> set global innodb_flush_log_at_trx_commit=2;
Query OK, 0 rows affected (0.00 sec)
mysql> set global sync_binlog=0;
Query OK, 0 rows affected (0.00 sec)
重新執行壓力測試:[root@localhost sysbench]# sysbench --test=oltp --mysql-table-engine=innodb --mysql-db=test --oltp-table-size=100000
> --db-driver=mysql --num-threads=2 --mysql-socket=/tmp/mysql.sock
> --mysql-user=root --mysql-host=localhost --mysql-password=root run
sysbench v0.4.8: multi-threaded system evaluation benchmark
WARNING: Preparing of "BEGIN" is unsupported, using emulation
(last message repeated 1 times)
Running the test with following options:
Number of threads: 2
Doing OLTP test.
Running mixed OLTP test
Using Special distribution (12 iterations, 1 pct of values are returned in 75 pct cases)
Using "BEGIN" for starting transactions
Using auto_inc on the id column
Maximum number of requests for OLTP test is limited to 10000
Threads started!
Done.
OLTP test statistics:
queries performed:
read: 140000
write: 50000
other: 20000
total: 210000
transactions: 10000 (702.90 per sec.)
deadlocks: 0 (0.00 per sec.)
read/write requests: 190000 (13355.08 per sec.)
other operations: 20000 (1405.80 per sec.)
Test execution summary:
total time: 14.2268s
total number of events: 10000
total time taken by event execution: 28.3987
per-request statistics:
min: 0.0018s
avg: 0.0028s
max: 0.8632s
approx. 95 percentile: 0.0033s
Threads fairness:
events (avg/stddev): 5000.0000/15.00
execution time (avg/stddev): 14.1994/0.00
調整完引數以後可以發現tps和qps值都變大了。來自為知筆記(Wiz)