基礎知識:
1。 Sort_Buffer_Size 是一個connection級參數,在每一個connection第一次須要使用這個buffer的時候,一次性分配設置的內存。
2。 Sort_Buffer_Size 並非越大越好,因爲是connection級的參數,過大的設置+高併發可能會耗盡系統內存資源。
3。 文檔說「On Linux, there are thresholds of 256KB and 2MB where larger values may significantly slow down memory allocation」
本文主要針對第三點作測試:
聽說Sort_Buffer_Size 超過2KB的時候,就會使用mmap() 而不是 malloc() 來進行內存分配,致使效率下降。
環境:
爲了更大的體現性能差距,使用 1GB內存的Fedora 虛擬機進行測試
測試表結構:
1w 行的表, 表結構mysql
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| k | int(10) unsigned | NO | MUL | 0 | |
| c | char(120) | NO | | | |
| pad | char(60) | NO | | | |
+-------+------------------+------+-----+---------+----------------+
測試語句:
分別設置Sort_Buffer_Size 爲 250K ,512K, 3M ,而後運行如下語句,查看運行時間。
1. sql_no_cache 防止query cache起效。
2. limit 1 爲了減小排序佔執行時間的比重,更多的體現內存分配帶來的影響
3. 語句explain的結果是 filesort , 以確保使用sort_buffersql
測試結果:
執行時間併發
250K : 1.318s
512K : 1.944s
3M : 2.412s
250 K
[root@localhost tmp]# mysqlslap -uroot -h127.0.0.1 -q ' select sql_no_cache * from sbtest order by pad limit 1' -c 100 --create-schema=test -i 10
Benchmark
Average number of seconds to run all queries: 1.318 seconds
Minimum number of seconds to run all queries: 1.285 seconds
Maximum number of seconds to run all queries: 1.378 seconds
Number of clients running queries: 100
Average number of queries per client: 1
512 K
[root@localhost tmp]# mysqlslap -uroot -h127.0.0.1 -q ' select sql_no_cache * from sbtest order by pad limit 1' -c 100 --create-schema=test -i 10
Benchmark
Average number of seconds to run all queries: 1.944 seconds
Minimum number of seconds to run all queries: 1.362 seconds
Maximum number of seconds to run all queries: 4.406 seconds
Number of clients running queries: 100
Average number of queries per client: 1
3M
[root@localhost tmp]# mysqlslap -uroot -h127.0.0.1 -q ' select sql_no_cache * from sbtest order by pad limit 1' -c 100 --create-schema=test -i 10
Benchmark
Average number of seconds to run all queries: 2.412 seconds
Minimum number of seconds to run all queries: 2.376 seconds
Maximum number of seconds to run all queries: 2.527 seconds
Number of clients running queries: 100
Average number of queries per client: 1
結論:
確實如文檔所說, 使用mmap 分配內存時,會帶來性能上的損耗,影響大約在 30% 左右。高併發