mysql 高性能學習筆記。php
咱們在Ubuntu環境下裝一個測試用的MySQL來學習。html
sudo apt-get update
sudo apt-get install mysql-server mysql-client
在安裝的過程當中,須要輸入數據庫密碼。mysql
sudo service mysql restart
mysql -u root -p
sysbench
是一個開源的、模塊化的、跨平臺的多線程性能測試工具,能夠用來進行CPU、內存、磁盤I/O、線程、數據庫的性能測試。目前支持的數據庫有MySQL、Oracle和PostgreSQL。當前功能容許測試的系統參數有:web
file I/O performance (文件I / O性能)
scheduler performance (調度性能)
memory allocation and transfer speed (內存分配和傳輸速度)
POSIX threads implementation performance (POSIX線程執行績效)
database server performance (OLTP benchmark) (數據庫服務器性能)sql
Ubuntu系統能夠直接apt,如:數據庫
apt-get install sysbench
在安裝的時候出現了這樣的錯誤:Could not get lock /var/lib/dpkg/lock
windows
出現這個問題多是有另一個程序正在運行,致使資源被鎖不可用。而致使資源被鎖的緣由多是上次運行安裝或更新時沒有正常完成,進而出現此情況,解決的辦法其實很簡單:
在終端中敲入如下兩句數組
sudo rm /var/cache/apt/archives/lock sudo rm /var/lib/dpkg/lock
再試着安裝,問題解決。服務器
root@db2:~# sysbench Missing required command argument.
Usage: #使用方法session
sysbench [general-options]... --test=<test-name> [test-options]... command
General options: #通用選項
--num-threads=N number of threads to use [1] #建立測試線程的數目。默認爲1.
--max-requests=N limit for total number of requests [10000] #請求的最大數目。默認爲10000,0表明不限制。
--max-time=N limit for total execution time in seconds [0] #最大執行時間,單位是s。默認是0,不限制。
--forced-shutdown=STRING amount of time to wait after --max-time before forcing shutdown [off] #超過max-time強制中斷。默認是off。
--thread-stack-size=SIZE size of stack per thread [32K] #每一個線程的堆棧大小。默認是32K。
--init-rng=[on|off] initialize random number generator [off] #在測試開始時是否初始化隨機數發生器。默認是off。
--test=STRING test to run #指定測試項目名稱。
--debug=[on|off] print more debugging info [off] #是否顯示更多的調試信息。默認是off。
--validate=[on|off] perform validation checks where possible [off] #在可能狀況下執行驗證檢查。默認是off。
--help=[on|off] print help and exit #幫助信息。
--version=[on|off] print version and exit #版本信息。
Compiled-in tests: #測試項目
fileio - File I/O test #IO
cpu - CPU performance test #CPU
memory - Memory functions speed test #內存
threads - Threads subsystem performance test #線程
mutex - Mutex performance test #互斥性能測試
oltp - OLTP test # 數據庫,事務處理
Commands:
prepare
:測試前準備工做;run
:正式測試cleanup
:測試後刪掉測試數據help version
See 'sysbench --test=<name> help' for a list of options for each test. #查看每一個測試項目的更多選項列表
更多選項:
1):sysbench --test=fileio help
root@db2:~# sysbench --test=fileio help sysbench 0.4.12: multi-threaded system evaluation benchmark
fileio options
:
--file-num=N 建立測試文件的數量。默認是128
--file-block-size=N 測試時文件塊的大小。默認是16384(16K)
--file-total-size=SIZE 測試文件的總大小。默認是2G
--file-test-mode=STRING 文件測試模式{seqwr(順序寫), seqrewr(順序讀寫), seqrd(順序讀), rndrd(隨機讀), rndwr(隨機寫), rndrw(隨機讀寫)}
--file-io-mode=STRING 文件操做模式{sync(同步),async(異步),fastmmap(快速map映射),slowmmap(慢map映射)}。默認是sync
--file-extra-flags=STRING 使用額外的標誌來打開文件{sync,dsync,direct} 。默認爲空
--file-fsync-freq=N 執行fsync()的頻率。(0 – 不使用fsync())。默認是100
--file-fsync-all=[on|off] 每執行完一次寫操做就執行一次fsync。默認是off
--file-fsync-end=[on|off] 在測試結束時才執行fsync。默認是on
--file-fsync-mode=STRING 使用哪一種方法進行同步{fsync, fdatasync}。默認是fsync
--file-merged-requests=N 若是能夠,合併最多的IO請求數(0 – 表示不合並)。默認是0
--file-rw-ratio=N 測試時的讀寫比例。默認是1.5
2):sysbench --test=cpu help
--cpu-max-prime=N 最大質數發生器數量。默認是10000
3):sysbench --test=memory help
root@db2:~# sysbench --test=memory help
sysbench 0.4.12: multi-threaded system evaluation benchmark
memory options:
--memory-block-size=SIZE 測試時內存塊大小。默認是1K
--memory-total-size=SIZE 傳輸數據的總大小。默認是100G
--memory-scope=STRING 內存訪問範圍{global,local}。默認是global
--memory-hugetlb=[on|off] 從HugeTLB池內存分配。默認是off
--memory-oper=STRING 內存操做類型。{read, write, none} 默認是write
--memory-access-mode=STRING存儲器存取方式{seq,rnd} 默認是seq
4):sysbench --test=threads help
sysbench 0.4.12: multi-threaded system evaluation benchmark
threads options
:
--thread-yields=N 每一個請求產生多少個線程。默認是1000
--thread-locks=N 每一個線程的鎖的數量。默認是8
5):sysbench --test=mutex help
root@db2:~# sysbench --test=mutex help
sysbench 0.4.12: multi-threaded system evaluation benchmark
mutex options
:
--mutex-num=N 數組互斥的總大小。默認是4096
--mutex-locks=N 每一個線程互斥鎖的數量。默認是50000
--mutex-loops=N 內部互斥鎖的空循環數量。默認是10000
6): sysbench --test=oltp help
root@db2:~# sysbench --test=oltp help
sysbench 0.4.12: multi-threaded system evaluation benchmark
oltp options
:
--oltp-test-mode=STRING 執行模式{simple,complex(advanced transactional),nontrx(non-transactional),sp}。默認是complex
--oltp-reconnect-mode=STRING 從新鏈接模式{session(不使用從新鏈接。每一個線程斷開只在測試結束),transaction(在每次事務結束後從新鏈接),query(在每一個SQL語句執行完從新鏈接),random(對於每一個事務隨機選擇以上從新鏈接模式)}。默認是session
--oltp-sp-name=STRING 存儲過程的名稱。默認爲空
--oltp-read-only=[on|off] 只讀模式。Update,delete,insert語句不可執行。默認是off
--oltp-skip-trx=[on|off] 省略begin/commit語句。默認是off
--oltp-range-size=N 查詢範圍。默認是100
--oltp-point-selects=N number of point selects [10]
--oltp-simple-ranges=N number of simple ranges [1]
--oltp-sum-ranges=N number of sum ranges [1]
--oltp-order-ranges=N number of ordered ranges [1]
--oltp-distinct-ranges=N number of distinct ranges [1]
--oltp-index-updates=N number of index update [1]
--oltp-non-index-updates=N number of non-index updates [1]
--oltp-nontrx-mode=STRING 查詢類型對於非事務執行模式{select, update_key, update_nokey, insert, delete} [select]
--oltp-auto-inc=[on|off] AUTO_INCREMENT是否開啓。默認是on
--oltp-connect-delay=N 在多少微秒後鏈接數據庫。默認是10000
--oltp-user-delay-min=N 每一個請求最短等待時間。單位是ms。默認是0
--oltp-user-delay-max=N 每一個請求最長等待時間。單位是ms。默認是0
--oltp-table-name=STRING 測試時使用到的表名。默認是sbtest
--oltp-table-size=N 測試表的記錄數。默認是10000
--oltp-dist-type=STRING 分佈的隨機數{uniform(均勻分佈),Gaussian(高斯分佈),special(空間分佈)}。默認是special
--oltp-dist-iter=N 產生數的迭代次數。默認是12
--oltp-dist-pct=N 值的百分比被視爲'special' (for special distribution)。默認是1
--oltp-dist-res=N ‘special’的百分比值。默認是75
General database options:
--db-driver=STRING 指定數據庫驅動程序('help' to get list of available drivers)
--db-ps-mode=STRING編制報表使用模式{auto, disable} [auto]
Compiled-in database drivers:
mysql - MySQL driver
mysql options
:
--mysql-host=[LIST,...] MySQL server host [localhost]
--mysql-port=N MySQL server port [3306]
--mysql-socket=STRING MySQL socket
--mysql-user=STRING MySQL user [sbtest]
--mysql-password=STRING MySQL password []
--mysql-db=STRING MySQL database name [sbtest]
--mysql-table-engine=STRING storage engine to use for the test table {myisam,innodb,bdb,heap,ndbcluster,federated} [innodb]
--mysql-engine-trx=STRING whether storage engine used is transactional or not {yes,no,auto} [auto]
--mysql-ssl=[on|off] use SSL connections, if available in the client library [off]
--myisam-max-rows=N max-rows parameter for MyISAM tables [1000000]
--mysql-create-options=STRING additional options passed to CREATE TABLE []
sysbench --test=threads --num-threads=500 --thread-yields=100 --thread-locks=4 run
--num-threads 開啓的線程 --file-total-size 總的文件大小
1,prepare階段,生成須要的測試文件,完成後會在當前目錄下生成不少小文件。
sysbench --test=fileio --num-threads=16 --file-total-size=2G --file-test-mode=rndrw prepare
2,run階段
sysbench --test=fileio --num-threads=20 --file-total-size=2G --file-test-mode=rndrw run
下面的命令運行文件 I/O 混合隨機讀/寫基準測試:
sysbench --test=fileio --file-total-size=3G --file-test-mode=rndrw --max-time=300 --max-requests=0 run
執行結果:
輸出結果分析:
每秒請求數是:233.33 Requests/sec
吞吐量是:3.6458Mb/sec
清除運行文件:
sysbench --test=fileio --file-total-size=3G cleanup
sysbench --test=fileio --num-threads=20 --file-total-size=2G --file-test-mode=rndrw cleanup
sysbench --test=memory --memory-block-size=8k --memory-total-size=1G run
sysbench –test=mutex –num-threads=100 –mutex-num=1000 –mutex-locks=100000 –mutex-loops=10000 run
1,prepare階段,生成須要的測試表
sysbench --test=oltp --mysql-table-engine=innodb --mysql-host=192.168.X.X --mysql-db=test --oltp-table-size=500000 --mysql-user=root --mysql-password=123456 prepare
2,run階段
sysbench --num-threads=16 --test=oltp --mysql-table-engine=innodb --mysql-host=192.168.x.x --mysql-db=test --oltp-table-size=500000 --mysql-user=root --mysql-password=123456 run
3,清理測試時生成的測試表
sysbench --num-threads=16 --test=oltp --mysql-table-engine=innodb --mysql-host=192.168.x.x --mysql-db=test --oltp-table-size=500000 --mysql-user=root --mysql-password=123456 cleanup
咱們實際開發中,最常碰到三個性能相關的服務器請求:
一、如何確認服務器是否達到了性能最佳的狀態;
二、找出某條語句爲何執行不夠快
三、診斷被用戶描述成「停頓」、「堆積」或「卡死」的某些間歇性疑難故障
找到慢的緣由,咱們纔可以對症下藥。
何爲性能?
咱們將性能定義爲完成某件任務所須要的時間度量,換句話說,性能即響應時間。
完成一項任務所須要的時間能夠分紅兩部分:執行時間
和等待時間
。
這裏咱們用一個PHP性能剖析工具,名叫ifP(instrumentation-for-php)
,代碼託管在Google Code上(https://code.google.com/archi...),該工具能夠更好的關注數據庫的調用。因此當沒法在數據庫層面進行測試的時候,Ifp能夠很好的幫助應用剖析數據庫的利用率。Ifp是一個提供了計數器和計時器的單例類
,很容易部署到生產環境中,由於不須要訪問PHP的配置權限(由於對於不少開發人員來講,都沒有訪問PHP的配置的權限。)
error_reporting(-1); require_once('../src/Instrumentation.php'); $instance = false; $ret = Instrumentation::get_instance()->start_request(); print_r($ret);
在web開發中,咱們常常會寫出一些SQL語句,一條糟糕的SQL語句可能讓你的整個程序都很是慢,超過10秒通常用戶就會選擇關閉網頁,如何優化SQL語句將那些運行時間 比較長的SQL語句找出呢?MySQL給咱們提供了一個很好的功能,那就是慢查詢
!所謂的慢查詢
就是經過設置來記錄超過必定時間的SQL語句
!那麼如何應用慢查詢呢?
默認狀況下,MySQL是不會記錄超過必定執行時間的SQL語句的。要開啓這個功能,咱們須要修改MySQL的配置文件,windows下修改my.ini
,Linux下修改my.cnf
文件,在[mysqld]最後增長以下命令:
首先咱們先查看MYSQL服務器的慢查詢狀態是否開啓.執行以下命令:
show variables like '%quer%';
咱們能夠看到,數據庫的慢查詢是關閉的。
找到 my.cnf
配置文件
> cd / > sudo find -name my.cnf ./etc/mysql/my.cnf
編輯 my.cnf
slow_query_log long_query_time = 1
(1)進入MySql控制檯,執行以下語句:
select sleep(2);
mysql> select sleep(2); |
---|
sleep(2) |
0 |
1 row in set (2.12 sec)
(2)查看慢查詢日誌文件think-slow.log
,在文件最後發現:
# Time: 121120 20:06:23 # User@Host: root[root] @ localhost [127.0.0.1] # Query_time: 2.104120 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0 SET timestamp=1353413183; select sleep(2);
(1)slow_query_log 這句是開啓記錄慢查詢功能,slow_query_log=0關閉;slow_query_log=1開啓(這個1能夠不寫)
(2)long_query_time = 1 這句是記錄超過1秒的SQL執行語句
(3)那麼這個日誌文件存放在什麼地方呢?
默認是放在mysql的data目錄,而且文件名爲host_name-slow.log即 主機名-slow.log,好比在筆者的開發機上就是THINK-slow.log(由於偶用的Thinkpad,呵呵)
(4)若是日誌文件不想放在data目錄,咱們能夠經過以下配置指定存放的目錄及日誌文件名:
slow_query_log_file=file_name
其中file_name就是你的存放日誌的目錄和文件名,在這裏注意有的資料上多是log-slow-queries=file_name
,這個在mysql5.5版已通過時!
MySQL 的 ALTER TABLE
操做性能對大表來講是個大問題。MySQL執行大部分修改表結構操做的方法是用新的結構建立一個空表,從舊錶中查出全部數據插入新表,而後刪除舊錶。這樣操做可能須要花費很長時間,若是內存不足而表又很大,並且還有不少索引的狀況下,尤爲如此。ALTER TABLE
操做須要花費數個小時甚至幾天時間才能完成。
大部分ALTER TABLE
操做將致使MySQL服務中斷。
不是全部的 ALTER TABLE
操做都會引發表的重建。例如,有兩種方法能夠改變或刪除一個列的默認值(一種方法很快,另外一種則很慢)。
假如要修改下面表的字段的默認值,下面是很慢的方式:
mysql> ALTER TABLE sakila.film > MODIFY COLUMN rental_duration TINYINT(3) NOT NULL > DEFAULT 5;
若是咱們用 SHOW STATUS
顯示這個語句作了 10000 次讀和 10000次插入操做。換句話說,它拷貝了整張表到一張新表,甚至列的類型,大小和能否爲NULL屬性都沒變。
理論上,MySQL能夠跳過建立新表的步驟。列的默認值實際上存在表的 .frm
文件中,因此,能夠直接修改這個文件而不須要改動表自己。然而,MySQL尚未採用這種優化的方法,全部的 MODIFY COLUMN
操做都將致使表重建。
另外一種方法是經過 ALTER COLUMN
操做來改變列的默認值:
mysql> ALTER TABLE sakila.film > ALTER COLUMN rental_duration TINYINT(3) NOT NULL DEFAULT 5;
這個語句會直接修改 .frm
文件而不涉及表數據。因此,這個操做是很是快的。