程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> mysql Key_buffer_size參數的優化設置

mysql Key_buffer_size參數的優化設置

編輯:MySQL綜合教程

mysql Key_buffer_size參數的優化設置。本站提示廣大學習愛好者:(mysql Key_buffer_size參數的優化設置)文章只能為提供參考,不一定能成為您想要的結果。以下是mysql Key_buffer_size參數的優化設置正文


先來看看document對這個參數的說明:

緩存myisam表的索引塊年夜小,可以被一切過程所同享。當設置key_buffer_size,操作體系不會立時分派key_buffer_size設置的值,而是在須要的時刻,再分派的。可以設置多個key_buffer,當設置不是默許key_buffer為0時,mysql會把緩存的索引塊移到默許的key_buffer中去並刪除不再應用的索引塊。Myisam表中只能cache索引塊,不克不及cache數據塊。

本來描寫:

Index blocks for MyISAM tables are buffered and are shared by all threads. key_buffer_size is the size of the buffer used for index blocks. The key buffer is also known as the key cache.

The maximum allowable setting for key_buffer_size is 4GB on 32-bit platforms. As of MySQL 5.0.52, values larger than 4GB are allowed for 64-bit platforms (except 64-bit Windows, for which large values are truncated to 4GB with a warning). The effective maximum size might be less, depending on your available physical RAM and per-process RAM limits imposed by your operating system or hardware platform. The value of this variable indicates the amount of memory requested. Internally, the server allocates as much memory as possible up to this amount, but the actual allocation might be less.

Increase the value to get better index handling (for all reads and multiple writes) to as much as you can afford. Using a value that is 25% of total memory on a machine that mainly runs MySQL is quite common. However, if you make the value too large (for example, more than 50% of your total memory) your system might start to page and become extremely slow. MySQL relies on the operating system to perform file system caching for data reads, so you must leave some room for the file system cache. Consider also the memory requirements of other storage engines.

1、樹立緩存索引 :

mysql> set global key_buffer_1.key_buffer_size=8384512;
Query OK, 0 rows affected (0.01 sec)
mysql> set global key_buffer_2.key_buffer_size=8384512;
Query OK, 0 rows affected (0.01 sec)

2、把指定表放到key buffer中

mysql> cache index t1,t2 in key_buffer_1;
+————+——————–+———-+———-+
| Table | Op | Msg_type | Msg_text |
+————+——————–+———-+———-+
| luoxuan.t1 | assign_to_keycache | status | OK |
| luoxuan.t2 | assign_to_keycache | status | OK |
+————+——————–+———-+———-+
2 rows in set (0.00 sec)

3、事後裝載表的索引塊

mysql> load index into cache t1,t2;
+————+————–+———-+———-+
| Table | Op | Msg_type | Msg_text |
+————+————–+———-+———-+
| luoxuan.t1 | preload_keys | status | OK |
| luoxuan.t2 | preload_keys | status | OK |
+————+————–+———-+———-+
2 rows in set (0.00 sec)

上面我們來看一下,假如盤算射中率及key buffer的應用率

Cache射中率:
100 – ( (Key_reads * 100) / Key_read_requests )

Key buffer的應用率
100 – ( (Key_blocks_unused * key_cache_block_size) * 100 / key_buffer_size )在mysql數據庫中,mysql key_buffer_size是對MyISAM表機能影響最年夜的一個參數,上面就將對mysql Key_buffer_size參數的設置停止具體引見,供您參考。

上面一台以MyISAM為重要存儲引擎辦事器的設置裝備擺設:

mysql> show variables like 'key_buffer_size';
+-----------------+------------+
| Variable_name | Value |
+-----------------+------------+
| key_buffer_size | 536870912 |
+-----------------+------------+

分派了512MB內存給mysql key_buffer_size,我們再看一下key_buffer_size的應用情形:
mysql> show global status like 'key_read%';

+------------------------+-------------+
| Variable_name | Value |
+------------------------+-------------+
| Key_read_requests | 27813678764 |
| Key_reads | 6798830 |
+------------------------+-------------+

一共有27813678764個索引讀取要求,有6798830個要求在內存中沒有找到直接從硬盤讀取索引,盤算索引未射中緩存的幾率:

key_cache_miss_rate = Key_reads / Key_read_requests * 100%

好比下面的數據,key_cache_miss_rate為0.0244%,4000個索引讀取要求才有一個直接讀硬盤,曾經很BT了,key_cache_miss_rate在0.1%以下都很好(每1000個要求有一個直接讀硬盤),假如key_cache_miss_rate在0.01%以下的話,key_buffer_size分派的過量,可以恰當削減。
MySQL辦事器還供給了key_blocks_*參數:

mysql> show global status like 'key_blocks_u%';
+------------------------+-------------+
| Variable_name | Value |
+------------------------+-------------+
| Key_blocks_unused | 0 |
| Key_blocks_used | 413543 |
+------------------------+-------------+

Key_blocks_unused表現未應用的緩存簇(blocks)數,Key_blocks_used表現已經用到的最年夜的blocks數,好比這台辦事器,一切的緩存都用到了,要末增長key_buffer_size,要末就是過渡索引了,把緩存占滿了。比擬幻想的設置:

Key_blocks_used / (Key_blocks_unused + Key_blocks_used) * 100% ≈ 80%

key_buffer_size設置留意事項
1.單個key_buffer的年夜小不克不及跨越4G,假如設置跨越4G,就有能夠碰到上面3個bug:

http://bugs.mysql.com/bug.php?id=29446

http://bugs.mysql.com/bug.php?id=29419

http://bugs.mysql.com/bug.php?id=5731

2.建議key_buffer設置為物理內存的1/4(針對MyISAM引擎),乃至是物理內存的30%~40%,假如key_buffer_size設置太年夜,體系就會頻仍的換頁,下降體系機能。由於MySQL應用操作體系的緩存來緩存數據,所以我們得為體系留夠足夠的內存;在許多情形下數據要比索引年夜很多。

3.假如機械機能優勝,可以設置多個key_buffer,分離讓分歧的key_buffer來緩存專門的索引

下面只是對"老手"來講的,我們還可以更深刻地優化key_buffer_size,應用"show status"來檢查"Key_read_requests, Key_reads, Key_write_requests 和Key_writes ",以調劑到更合適你的運用的年夜小,Key_reads/Key_read_requests的年夜小正常情形下得小於0.01

參考材料:

http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_key_buffer_size

優化mysql之key_buffer_size

key_buffer_size
key_buffer_size指定索引緩沖區的年夜小,它決議索引處置的速度,特別是索引讀的速度。經由過程檢討狀況值Key_read_requests和Key_reads,可以曉得key_buffer_size設置能否公道。比例key_reads /key_read_requests應當盡量的低,至多是1:100,1:1000更好(上述狀況值可使用SHOW STATUS LIKE ‘key_read%'取得)。
key_buffer_size只對MyISAM表起感化。即便你不應用MyISAM表,然則外部的暫時磁盤表是MyISAM表,也要應用該值。可使用檢討狀況值created_tmp_disk_tables得知概況。
關於1G內存的機械,假如不應用MyISAM表,推舉值是16M(8-64M)

晉升機能的建議:
1.假如opened_tables太年夜,應當把my.cnf中的table_cache變年夜
2.假如Key_reads太年夜,則應當把my.cnf中key_buffer_size變年夜.可以用Key_reads/Key_read_requests盤算出cache掉敗率
3.假如Handler_read_rnd太年夜,則你寫的SQL語句裡許多查詢都是要掃描全部表,而沒有施展鍵的感化
4.假如Threads_created太年夜,就要增長my.cnf中thread_cache_size的值.可以用Threads_created/Connections盤算cache射中率
5.假如Created_tmp_disk_tables太年夜,就要增長my.cnf中tmp_table_size的值,用基於內存的暫時表取代基於磁盤的 


MySQL優化小案例:key_buffer_size
key_buffer_size是對MyISAM表機能影響最年夜的一個參數,上面一台以MyISAM為重要存儲引擎辦事器的設置裝備擺設:

mysql> SHOW VARIABLES LIKE '%key_buffer_size%';
  
上面檢查key_buffer_size的應用情形:

mysql> SHOW GLOBAL STATUS LIKE '%key_read%';
+-------------------+-----------------+
| Variable_name     | Value           |
+-------------------+-----------------+
| Key_read_requests | 2454354135490   |
| Key_reads         | 23490           |
+-------------------+-----------------+
2 rows in set (0.00 sec)
一共有Key_read_requests個索引要求,一共產生了Key_reads次物理IO

Key_reads/Key_read_requests ≈ 0.1%以下比擬好。

經由比較,針對我的內存是64G的,所以我把Key_buffer_size設置為2048M,感到很多多少了,麼有了內存溢出情形。處理了成績。後續有甚麼情形小編持續彌補。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved