這裡使用的是mysql Ver 14.14 Distrib 5.6.19, for Linux (i686) using EditLine wrapper一、mysql目錄文件ibdata1:系統表空間 包含數據字典、回滾日誌/undolog等(insert buffer segment/doubl...
這裡使用的是mysql Ver 14.14 Distrib 5.6.19, for Linux (i686) using EditLine wrapper
一、mysql目錄文件
ibdata1:系統表空間 包含數據字典、回滾日誌/undolog等
(insert buffer segment/double write segment/rollback segment/index segment/dictionary segment/undo segment)
ib_logfile0/ib_logfile1:事務日誌/redolog
mysql-relay-bin:中繼日誌
binarylog:二進位日誌
general_log.log:常規日誌
mysql_error.log:錯誤日誌
slow_query.log:慢日誌
.ibd:用戶表空間-數據文件(insert buffer bitmap page/leaf page segment/none leaf page segment)
Innodb buffer pool(記憶體):undo page /insert buffer page/adaptive hash index/index page/lock info/data dictionary
二、mysql線程
FILE IO
-------- FILE I/O -------- I/O thread 0 state: waiting for i/o request (insert buffer thread) I/O thread 1 state: waiting for i/o request (log thread) I/O thread 2 state: waiting for i/o request (read thread) I/O thread 3 state: waiting for i/o request (read thread) I/O thread 4 state: waiting for i/o request (read thread) I/O thread 5 state: waiting for i/o request (read thread) I/O thread 6 state: waiting for i/o request (write thread) I/O thread 7 state: waiting for i/o request (write thread) I/O thread 8 state: waiting for i/o request (write thread) I/O thread 9 state: waiting for i/o request (write thread) Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] , ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0 Pending flushes (fsync) log: 0; buffer pool: 0 393 OS file reads, 5 OS file writes, 5 OS fsyncs 0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
innodb後臺所有線程
| thread/sql/main | BACKGROUND | YES | | thread/innodb/io_handler_thread | BACKGROUND | YES | | thread/innodb/io_handler_thread | BACKGROUND | YES | | thread/innodb/io_handler_thread | BACKGROUND | YES | | thread/innodb/io_handler_thread | BACKGROUND | YES | | thread/innodb/io_handler_thread | BACKGROUND | YES | | thread/innodb/io_handler_thread | BACKGROUND | YES | | thread/innodb/io_handler_thread | BACKGROUND | YES | | thread/innodb/io_handler_thread | BACKGROUND | YES | | thread/innodb/io_handler_thread | BACKGROUND | YES | | thread/innodb/io_handler_thread | BACKGROUND | YES | | thread/innodb/srv_master_thread | BACKGROUND | YES | | thread/innodb/srv_purge_thread | BACKGROUND | YES | | thread/innodb/srv_monitor_thread | BACKGROUND | YES | | thread/innodb/srv_error_monitor_thread | BACKGROUND | YES | | thread/innodb/srv_lock_timeout_thread | BACKGROUND | YES | | thread/innodb/page_cleaner_thread | BACKGROUND | YES | | thread/sql/signal_handler | BACKGROUND | YES | | thread/sql/slave_sql | BACKGROUND | YES | | thread/sql/slave_io | BACKGROUND | YES |
IO線程分別是insert buffer thread、log thread、read thread、write thread。
在MySQL 5.6.10之後,預設線程處理模型使用執行每個客戶端連接一個線程語句。隨著越來越多的客戶端連接到伺服器和執行語句,整體性能降低。線程池插件的提供旨在減少開銷,提高性能的其他線程的處理模式。該插件實現了通過有效地管理語句執行線程的大量客戶端連接的提高伺服器性能的線程池。InnoDB Plugin版本開始增加了預設IO thread的數量,預設的read thread和write thread分別增大到了4個,並且不再使用innodb_file_io_threads參數,而是分別使用innodb_read_io_threads和innodb_write_io_threads參數。 線程池解決每個連接模型解決單線程的幾個問題
-
Too many thread stacks make CPU caches almost useless in highly parallel execution workloads. The thread pool promotes thread stack reuse to minimize the CPU cache footprint.
-
With too many threads executing in parallel, context switching overhead is high. This also presents a challenging task to the operating system scheduler. The thread pool controls the number of active threads to keep the parallelism within the MySQL server at a level that it can handle and that is appropriate for the server host on which MySQL is executing.
-
Too many transactions executing in parallel increases resource contention. In InnoDB, this increases the time spent holding central mutexes. The thread pool controls when transactions start to ensure that not too many execute in parallel.
三、mysql訪問文件流程
Transaction 來自網路
三、影響IO/記憶體的一些參數
1、innodb_flush_log_at_trx_commit 設置為2
這參數是指 事務log(ib_logfile0、ib_logfile1)以怎樣的方式寫入到log buffer
=0 mysql crash 就丟失了,性能最好
buffer pool -> log buffer 每秒 wirte os cache & flush磁碟
=1 不會丟失,效率低
buffer pool -> log buffer 每次 write os cache & flush磁碟
=2 即使mysql崩潰也不會丟數據
buffer pool -> os cache 每秒flush 磁碟
註意:由於進程調度策略問題,這個“每秒執行一次 flush(刷到磁碟)操作”並不是保證100%的“每秒2、sync_binlog
二進位日誌(binary log)同步到磁碟的頻率。binary log 每寫入sync_binlog
次後,刷寫到磁碟。
如果 autocommit
開啟,每個語句都寫一次 binary log,否則每次事務寫一次。
預設值是 0
,不主動同步,而依賴操作系統本身不定期把文件內容 flush 到磁碟
設為 1
最安全,在每個語句或事務後同步一次 binary log,即使在崩潰時也最多丟失一個語句或事務的日誌,但因此也最慢。
大多數情況下,對數據的一致性並沒有很嚴格的要求,所以並不會把 sync_binlog
配置成 1,
為了追求高併發,提升性能,可以設置為 100
或直接用 0
3、write/read thread
非同步IO線程數
innodb_write_io_threads=16
innodb_read_io_threads=16
(該參數需要在配置文件中添加,重啟mysql實例起效)
臟頁寫的線程數,加大該參數可以提升寫入性能
4、innodb_max_dirty_pages_pct
最大臟頁百分數,
當系統中臟頁所占百分比超過這個值,INNODB就會進行寫操作以把頁中的已更新數據寫入到磁碟文件中。預設75,一般現在流行的SSD硬碟很難達到這個比例。可依據實際情況在75-80之間調節
5、innodb_io_capacity=5000
從緩衝區刷新臟頁時,一次刷新臟頁的數量。根據磁碟IOPS的能力一般建議設置如下:
SAS 200
SSD 5000
PCI-E 10000-50000
6、innodb_flush_method=O_DIRECT(該參數需要重啟mysql實例起效)
控制innodb 數據文件和redo log的打開、刷寫模式。有三個值:fdatasync(預設),O_DSYNC,O_DIRECT。
-
fdatasync模式:寫數據時,write這一步並不需要真正寫到磁碟才算完成(可能寫入到操作系統buffer中就會返回完成),真正完成是flush操作,buffer交給操作系統去flush,並且文件的元數據信息也都需要更新到磁碟。
-
O_DSYNC模式:寫日誌操作是在write這步完成,而數據文件的寫入是在flush這步通過fsync完成。
-
O_DIRECT模式:數據文件的寫入操作是直接從mysql innodb buffer到磁碟的,並不用通過操作系統的緩衝,而真正的完成也是在flush這步,日誌還是要經過OS緩衝。
通過圖可以看出O_DIRECT相比fdatasync的優點是避免了雙緩衝,本身innodb buffer pool就是一個緩衝區,不需要再寫入到系統的buffer,但是有個缺點是由於是直接寫入到磁碟,所以相比fdatasync的順序讀寫的效率要低些。
在大量隨機寫的環境中O_DIRECT要比fdatasync效率更高些,順序寫多的話,還是預設的fdatasync更高效。
7、innodb_adaptive_flushing 設置為 ON (使刷新臟頁更智能)
影響每秒刷新臟頁的數目
規則由原來的“大於innodb_max_dirty_pages_pct時刷新100個臟頁到磁碟”變為 “通過buf_flush_get_desired_flush_reate函數判斷重做日誌產生速度確定需要刷新臟頁的最合適數目”,即使臟頁比例小於 innodb_max_dirty_pages_pct時也會刷新一定量的臟頁。
8、innodb_adaptive_flushing_method 設置為 keep_average
影響checkpoint,更平均的計算調整刷臟頁的速度,進行必要的flush.(該變數為mysql衍生版本Percona Server下的一個變數,原生mysql不存在)
9、innodb_stats_on_metadata=OFF
關掉一些訪問information_schema庫下表而產生的索引統計。
當重啟mysql實例後,mysql會隨機的io取數據遍歷所有的表來取樣來統計數據,這個實際使用中用的不多,建議關閉.
10、innodb_change_buffering=all
當更新/插入的非聚集索引的數據所對應的頁不在記憶體中時(對非聚集索引的更新操作通常會帶來隨機IO),會將其放到一個insert buffer中,當隨後頁面被讀到記憶體中時,會將這些變化的記錄merge到頁中。當伺服器比較空閑時,後臺線程也會做merge操作。
由於主要用到merge的優勢來降低io,但對於一些場景並不會對固定的數據進行多次修改,此處則並不需要把更新/插入操作開啟change_buffering,如果開啟只是多餘占用了buffer_pool的空間和處理能力。這個參數要依據實際業務環境來配置。
11、innodb_old_blocks_time=1000
使Block在old sublist中停留時間長為1s,不會被轉移到new sublist中,避免了Buffer Pool被污染BP可以被認為是一條長鏈表。被分成young 和 old兩個部分,其中old預設占37%的大小(由innodb_old_blocks_pct 配置)。靠近頂端的Page表示最近被訪問。靠近尾端的Page表示長時間未被訪問。而這兩個部分的交匯處成為midpoint。每當有新的Page需要載入到BP時,該page都會被插入到midpoint的位置,並聲明為old-page。當old部分的page,被訪問到時,該page會被提升到鏈表的頂端,標識為young。
由於table scan的操作是先load page,然後立即觸發一次訪問。所以當innodb_old_blocks_time =0 時,會導致table scan所需要的page不讀的作為young page被添加到鏈表頂端。而一些使用較為不頻繁的page就會被擠出BP,使得之後的SQL會產生磁碟IO,從而導致響應速度變慢。
這時雖然mysqldump訪問的page會不斷載入在LRU頂端,但是高頻度的熱點數據訪問會以更快的速度把page再次搶占到LRU頂端。從而導致mysqldump載入入的page會被迅速刷下,並立即被evict(淘汰)。因此,time=0或1000對這種壓力環境下的訪問不會造成很大影響,因為dump的數據根本搶占不過熱點數據。不只dump,當大數據操作的時候也是如此。
12、binlog_cache_size
二進位日誌緩衝大小:一個事務,在沒有提交(uncommitted)的時候,產生的日誌,記錄到Cache中;等到事務提交(committed)需要提交的時候,則把日誌持久化到磁碟。
設置太大的話,會比較消耗記憶體資源(Cache本質就是記憶體),更加需要註意的是:binlog_cache是不是全局的,是按SESSION為單位獨享分配的,也就是說當一個線程開始一個事務的時候,Mysql就會為這個SESSION分配一個binlog_cache怎麼判斷我們當前的binlog_cache_size設置的沒問題呢?
mysql> show status like 'binlog_%'; +-----------------------+-----------+ | Variable_name | Value | +-----------------------+-----------+ | Binlog_cache_disk_use | 1425 | | Binlog_cache_use | 126945718 | +-----------------------+-----------+ 2 rows in set (0.00 sec) mysql> select @@binlog_cache_size; +---------------------+ | @@binlog_cache_size | +---------------------+ | 1048576 | +---------------------+ 1 row in set (0.00 sec)
運行情況Binlog_cache_use 表示binlog_cache記憶體方式被用上了多少次,Binlog_cache_disk_use表示binlog_cache臨時文件方式被用上了多少次
13、innodb_file_per_table
innodb_file_per_table=1
獨立表空間
優點:
-
每個表的數據和索引都會存在自已的表空間中
-
可以實現單表在不同的資料庫中移動
-
空間可以回收(除drop table操作)
-
刪除大量數據後可以通過:alter table TableName engine=innodb;回縮不用的空間
-
使用turncate table也會使空間收縮
-
對於使用獨立表空間的表,不管怎麼刪除,表空間的碎片不會太嚴重的影響性能
缺點:單表增加過大,如超過100個G
結論:共用表空間在Insert操作上少有優勢。其它都沒獨立表空間表現好。當啟用獨立表空間時,請合理調整一 下:innodb_open_files ,InnoDB Hot Backup(冷備)的表空間cp不會面對很多無用的copy了。而且利用innodb hot backup及表空間的管理命令可以實現單現移動。
14、增加本地埠,以應對大量連接
echo ‘1024 65000′ > /proc/sys/net/ipv4/ip_local_port_range
該參數指定埠的分配範圍,該埠是向外訪問的限制。mysql預設監聽的3306埠即使有多個請求鏈接,也不會有影響。但是由於mysql是屬於高記憶體、高cpu、高io應用,不建議把多少應用於mysql混搭在同一臺機器上。即使業務量不大,也可以通過降低單台機器的配置,多台機器共存來實現更好。
15、增加隊列的鏈接數
echo ‘1048576’ > /proc/sys/net/ipv4/tcp_max_syn_backlog
建立鏈接的隊列的數越大越好,但是從另一個角度想,實際環境中應該使用連接池更合適,避免重覆建立鏈接造成的性能消耗。使用連接池,鏈接數會從應用層面更可控些。
16、設置鏈接超時時間
echo ’10’ > /proc/sys/net/ipv4/tcp_fin_timeout
該參數主要為了降低TIME_WAIT占用的資源時長。尤其針對http短鏈接的服務端或者mysql不採用連接池效果比較明顯。
參考文章
http://dev.cmcm.com/archives/107/comment-page-1
http://www.zhdba.com/mysqlops/2012/05/24/mysql-io/
http://blog.itpub.net/22664653/viewspace-1063134/
http://liyangliang.me/posts/2014/03/innodb_flush_log_at_trx_commit-and-sync_binlog/
http://www.cnblogs.com/snifferhu/p/4736479.html
http://www.cnblogs.com/xuanzhi201111/p/4040681.html
http://mysqllover.com/?p=636