MySQL服務器安裝完以後如何調節性能

原文做者: Peter Zaitsev
原文來源: http://www.mysqlperformanceblog.com/2006/09/29/what-to-tune-in-mysql-server-after-installation
譯者:葉金榮(Email:),轉載請註明譯者和出處,而且不能用於商業用途,違者必究。
mysql

My favorite question during Interview for people to work as MySQL DBAs or be involved with MySQL Performance in some way is to ask them what should be tuned in MySQL Server straight after installation, assuming it was installed with default settings. 面試

在面試MySQL DBA或者那些打算作MySQL性能優化的人時,我最喜歡問題是:MySQL服務器按照默認設置安裝完以後,應該作哪些方面的調節呢? sql

I’m surprised how many people fail to provide any reasonable answer to this question, and how many servers are where in wild which are running with default settings. 緩存

令我很驚訝的是,有多少人對這個問題沒法給出合理的答案,又有多少服務器都運行在默認的設置下。 安全

Even though you can tune quite a lot of variables in MySQL Servers only few of them are really important for most common workload. After you get these settings right other changes will most commonly offer only incremental performance improvements. 性能優化

儘管你能夠調節不少MySQL服務器上的變量,可是在大多數一般的工做負載下,只有少數幾個才真正重要。若是你把這些變量設置正確了,那麼修改其餘變量最多隻能對系統性能改善有必定提高。 服務器

key_buffer_size – Very important if you use MyISAM tables. Set up to 30-40% of available memory if you use MyISAM tables exclusively. Right size depends on amount of indexes, data size and workload – remember MyISAM uses OS cache to cache the data so you need to leave memory for it as well, and data can be much larger than indexes in many cases. Check however if all of key_buffer is used over time – it is not rare to see key_buffer being set to 4G while combined size of .MYI files is just 1GB. This would be just a waste. If you use few MyISAM tables you’ll want to keep it lower but still at least 16-32Mb so it is large enough to accommodate indexes for temporary tables which are created on disk. session

key_buffer_size – 這對MyISAM表來講很是重要。若是隻是使用MyISAM表,能夠把它設置爲可用內存的 30-40%。合理的值取決於索引大小、數據量以及負載 — 記住,MyISAM表會使用操做系統的緩存來緩存數據,所以須要留出部份內存給它們,不少狀況下數據比索引大多了。儘管如此,須要老是檢查是否全部的 key_buffer 都被利用了 — .MYI 文件只有 1GB,而 key_buffer 卻設置爲 4GB 的狀況是很是少的。這麼作太浪費了。若是你不多使用MyISAM表,那麼也保留低於 16-32MB 的 key_buffer_size 以適應給予磁盤的臨時表索引所需。 併發

innodb_buffer_pool_size This is very important variable to tune if you’re using Innodb tables. Innodb tables are much more sensitive to buffer size compared to MyISAM. MyISAM may work kind of OK with default key_buffer_size even with large data set but it will crawl with default innodb_buffer_pool_size. Also Innodb buffer pool caches both data and index pages so you do not need to leave space for OS cache so values up to 70-80% of memory often make sense for Innodb only installations. Same rules as for key_buffer apply – if you have small data set and it is not going to grow dramatically do not oversize innodb_buffer_pool_size you might find better use for memory available. app

innodb_buffer_pool_size – 這對Innodb表來講很是重要。Innodb相比MyISAM表對緩衝更爲敏感。MyISAM能夠在默認的 key_buffer_size 設置下運行的能夠,然而Innodb在默認的 innodb_buffer_pool_size 設置下卻跟蝸牛似的。因爲Innodb把數據和索引都緩存起來,無需留給操做系統太多的內存,所以若是隻須要用Innodb的話則能夠設置它高達 70-80% 的可用內存。一些應用於 key_buffer 的規則有 — 若是你的數據量不大,而且不會暴增,那麼無需把 innodb_buffer_pool_size 設置的太大了。

innodb_additional_pool_size This one does not really affect performance too much, at least on OS with decent memory allocators. Still you might want to have it 20MB (sometimes larger) so you can see how much memory Innodb allocates for misc needs.

innodb_additional_pool_size – 這個選項對性能影響並不太多,至少在有差很少足夠內存可分配的操做系統上是這樣。不過若是你仍然想設置爲 20MB(或者更大),所以就須要看一下Innodb其餘須要分配的內存有多少。

innodb_log_file_size Very important for write intensive workloads especially for large data sets. Larger sizes offer better performance but increase recovery times so be careful. I normally use values 64M-512M depending on server size.

innodb_log_file_size 在高寫入負載尤爲是大數據集的狀況下很重要。這個值越大則性能相對越高,可是要注意到可能會增長恢復時間。我常常設置爲 64-512MB,跟據服務器大小而異。

innodb_log_buffer_size Default for this one is kind of OK for many workloads with medium write load and shorter transactions. If you have update activity spikes however or work with blobs a lot you might want to increase it. Do not set it too high however as it would be waste of memory – it is flushed every 1 sec anyway so you do not need space for more than 1 sec worth of updates. 8MB-16MB are typically enough. Smaller installations should use smaller values.

innodb_log_buffer_size 默認的設置在中等強度寫入負載以及較短事務的狀況下,服務器性 能還能夠。若是存在更新操做峯值或者負載較大,就應該考慮加大它的值了。若是它的值設置過高了,可能會浪費內存 — 它每秒都會刷新一次,所以無需設置超過1秒所需的內存空間。一般 8-16MB 就足夠了。越小的系統它的值越小。

innodb_flush_logs_at_trx_commit Crying about Innodb being 100 times slower than MyISAM ? You probably forgot to adjust this value. Default value of 1 will mean each update transaction commit (or each statement outside of transaction) will need to flush log to the disk which is rather expensive, especially if you do not have Battery backed up cache. Many applications, especially those moved from MyISAM tables are OK with value 2 which means do not flush log to the disk but only flush it to OS cache. The log is still flushed to the disk each second so you normally would not loose more than 1-2 sec worth of updates. Value 0 is a bit faster but is a bit less secure as you can lose transactions even in case MySQL Server crashes. Value 2 only cause data loss with full OS crash.

innodb_flush_logs_at_trx_commit 是否爲Innodb比MyISAM慢1000倍而頭大?看來也許你忘了修改這個參數了。默認值是 1,這意味着每次提交的更新事務(或者每一個事務以外的語句)都會刷新到磁盤中,而這至關耗費資源,尤爲是沒有電池備用緩存時。不少應用程序,尤爲是從 MyISAM轉變過來的那些,把它的值設置爲 2 就能夠了,也就是不把日誌刷新到磁盤上,而只刷新到操做系統的緩存上。日誌仍然會每秒刷新到磁盤中去,所以一般不會丟失每秒1-2次更新的消耗。若是設置 爲 0 就快不少了,不過也相對不安全了 — MySQL服務器崩潰時就會丟失一些事務。設置爲 2 指揮丟失刷新到操做系統緩存的那部分事務。

table_cache – Opening tables can be expensive. For example MyISAM tables mark MYI header to mark table as currently in use. You do not want this to happen so frequently and it is typically best to size your cache so it is large enough to keep most of your tables open. It uses some OS resources and some memory but for modern hardware it is typically not the problem. 1024 is good value for applications with couple hundreds tables (remember each connection needs its own entry) if you have many connections or many tables increase it larger. I’ve seen values over 100.000 used.

table_cache — 打開一個表的開銷可能很大。例如MyISAM把MYI文件頭標誌該表正在使用中。你確定不但願這種操做太頻繁,因此一般要加大緩存數量,使得足以最大限度 地緩存打開的表。它須要用到操做系統的資源以及內存,對當前的硬件配置來講固然不是什麼問題了。若是你有200多個表的話,那麼設置爲 1024 也許比較合適(每一個線程都須要打開表),若是鏈接數比較大那麼就加大它的值。我曾經見過設置爲 100,000 的狀況。

thread_cache Thread creation/destructions can be expensive, which happen at each connect/disconnect. I normally set this value to at least 16. If application has large jumps in amount of concurrent connections and I see fast growth of
Threads_Created variable I boost it higher. The goal is not to have threads created in normal operation.

thread_cache — 線程的建立和銷燬的開銷可能很大,由於每一個線程的鏈接/斷開都須要。我一般至少設置爲 16。若是應用程序中有大量的跳躍併發鏈接而且 Threads_Created 的值也比較大,那麼我就會加大它的值。它的目的是在一般的操做中無需建立新線程。

query_cache If your application is read intensive and you do not have application level caches this can be great help. Do not set it too large as it may slow things down as its maintenance may get expensive. Values from 32M to 512M normally make sense. Check it however after a while and see if it is well used. For certain workloads cache hit ratio is lower than would justify having it enabled.

query_cache — 若是你的應用程序有大量讀,並且沒有應用程序級別的緩存,那麼這頗有用。不要把它設置太大了,由於想要維護它也須要很多開銷,這會致使MySQL變慢。通 常設置爲 32-512Mb。設置完以後最好是跟蹤一段時間,查看是否運行良好。在必定的負載壓力下,若是緩存命中率過低了,就啓用它。

Note: as you can see all of these are global variables. These variables depend on hardware and mix of storage engines, while per session variables are typically workload specific. If you have simple queries there is no reason to increase sort_buffer_size even if you have 64GB of memory to waste. Furthermore doing so may decrease performance.
I normally leave per session variable tuning to second step after I can analyze workload.

注意:就像你看到的上面這些全局表量,它們都是依據硬件配置以及不一樣的存儲引擎而不一樣,可是會話變量一般是根據不一樣的負載來設定的。若是你只有一些簡單的查詢,那麼就無需增長 sort_buffer_size 的值了,儘管你有 64GB 的內存。搞很差也許會下降性能。
我一般在分析系統負載後纔來設置會話變量。

P.S Note MySQL distribution contains bunch of sample my.cnf files which may be great templates to use. Typically they would already be much better than defaults if you chose correct one.

P.S,MySQL的發行版已經包含了各類 my.cnf 範例文件了,能夠做爲配置模板使用。一般這比你使用默認設置好的多了。

相關文章
相關標籤/搜索