{"id":1283,"date":"2010-02-02T21:56:43","date_gmt":"2010-02-02T13:56:43","guid":{"rendered":"http:\/\/www.orczhou.com\/?p=1283"},"modified":"2010-03-01T10:09:33","modified_gmt":"2010-03-01T02:09:33","slug":"myisam-key-buffer-3","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2010\/02\/myisam-key-buffer-3\/","title":{"rendered":"\u8be6\u89e3MyISAM Key Cache(\u540e\u7bc7)"},"content":{"rendered":"<p>\u5728\u524d\u4e24\u7bc7\uff08<a href=\"http:\/\/www.orczhou.com\/index.php\/2010\/01\/myisam-key-buffer-1\/\">\u524d\u7bc7<\/a>\u3001<a href=\"http:\/\/www.orczhou.com\/index.php\/2010\/01\/myisam-key-buffer-2\/\">\u4e2d\u7bc7<\/a>\uff09\u4e2d\uff0c\u5206\u522b\u4ecb\u7ecd\u4e86Key Cache\u7684\u57fa\u672c\u539f\u7406\uff08LRU\u548cMidpoint Insertion Strategy\uff09\u3002\u6700\u540e\uff0c\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u76f8\u5173\u7684\u53c2\u6570\u3001\u72b6\u6001\u53c2\u6570\u548c\u547d\u4ee4\u3002<\/p>\n<p>Key Cache\u7684\u914d\u7f6e\u5f88\u7075\u6d3b\uff0c\u53ef\u4ee5\u9488\u5bf9\u5168\u5c40\u914d\u7f6e\uff0c\u8fd8\u53ef\u4ee5\u9488\u5bf9\u67d0\u4e2a\u5355\u72ec\u6570\u636e\u8868\u5206\u914dKey Cache\u7684\u5927\u5c0f\uff1b\u5982\u679c\u4e00\u4e2a\u6570\u636e\u8868\u67d0\u90e8\u5206\u7684\u7d22\u5f15\u5757\u88ab\u8bbf\u95ee\u7684\u975e\u5e38\u9891\u7e41\uff08\u8f83\u4e4b\u5176\u4ed6\u7d22\u5f15\u5757\uff09\uff0c\u90a3\u4e48\u53ef\u4ee5\u914d\u7f6eMidpoint Insertion Strategy\u8fbe\u5230\u6700\u5927\u7684\u5229\u7528\u7387(<a href=\"http:\/\/www.orczhou.com\/index.php\/2010\/01\/myisam-key-buffer-2\/\">\u53c2\u8003<\/a>)\u3002<!--more--><\/p>\n<p><font color=\"red\">1. \u5982\u4f55\u914d\u7f6eKey Cache\u7684\u5927\u5c0f<\/font><\/p>\n<div class=\"mycode\">\n#\u914d\u7f6e\u6587\u4ef6my.cnf<br \/>\nkey_buffer_size=50*1024*1024\n<\/div>\n<p>\u53e6\u5916\uff0cKey Cache\u7684\u5927\u5c0f\u53ef\u4ee5\u52a8\u6001\u7684\u6539\u53d8<\/p>\n<p><font color=\"red\">2. \u7ed9\u6570\u636e\u8868\u5212\u5206\u5355\u72ec\u7684Key Cache<\/font><\/p>\n<p>\u4f8b\u5982\uff1a\u5212\u5206\u4e00\u5757128K\u7684Key buffer\u7a7a\u95f4\uff0c\u6307\u5b9a\u6570\u636e\u8868t1\u7684Key cache\u653e\u5728\u91cc\u9762\u3002\u6700\u540e\u6f14\u793a\u4e86\u5982\u4f55\u5220\u9664\u8fd9\u4e2a\u7279\u5b9a\u7684Key buffer\u7a7a\u95f4\u3002<\/p>\n<div class=\"mycode\">\nSET GLOBAL hot_cache.key_buffer_size=128*1024;<br \/>\nCACHE INDEX t1 IN hot_cache;<br \/>\nSET GLOBAL  hot_cache.key_buffer_size=0;\n<\/div>\n<p><font color=\"red\">3. \u9884\u5148\u8f7d\u5165\u67d0\u4e9b\u6570\u636e\u8868\u7684\u7d22\u5f15<\/font><\/p>\n<div class=\"mycode\">\nLOAD INDEX INTO CACHE t1, t2\n<\/div>\n<p><font color=\"red\">4. \u5173\u4e8eKey Cache\u7684\u4f7f\u7528\u60c5\u51b5\u89c2\u5bdf Flush\u73b0\u8c61<\/font><\/p>\n<pre>\r\n<div class=\"mycode\">\r\nmysql> show status like \"key%\"; \r\n+------------------------+----------+\r\n| Variable_name          | Value    |\r\n+------------------------+----------+\r\n| Key_blocks_not_flushed | 14468    | \r\n| Key_blocks_unused      | 0        | \r\n| Key_blocks_used        | 14497    | \r\n| Key_read_requests      | 30586575 | \r\n| Key_reads              | 157      | \r\n| Key_write_requests     | 7100408  | \r\n| Key_writes             | 1199800  | \r\n+------------------------+----------+\r\nmysql> flush tables;             \uff08<strong><font color=\"red\">\u6ce8\u610f\uff0c\u8bf7\u4e0d\u8981\u5728\u4e1a\u52a1\u9ad8\u5cf0\u671f\u6267\u884c<\/font><\/strong>\uff09\r\n+------------------------+----------+\r\n| Variable_name          | Value    |\r\n+------------------------+----------+\r\n| Key_blocks_not_flushed | 0        |   #\u6240\u6709\u4fee\u6539\u7684block\u90fd\u5df2\u7ecf\u88abflush\u4e86\r\n| Key_blocks_unused      | 0        | \r\n| Key_blocks_used        | 14497    | \r\n| Key_read_requests      | 38333936 | \r\n| Key_reads              | 207      | \r\n| Key_write_requests     | 8819898  | \r\n| Key_writes             | 1255245  | \r\n+------------------------+----------+\r\n<\/div><\/pre>\n<p><font color=\"red\">5. \u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879<\/font><\/p>\n<p>\u5185\u5b58\u4e2d\u7f13\u5b58\u7684\u7d22\u5f15\u5757\uff08Key Cache\uff09\uff0c<strong>\u6709\u65f6\u5019\u5e76\u4e0d\u4f1a\u53ca\u65f6\u5237\u65b0<\/strong>\u5230\u78c1\u76d8\u4e0a\uff0c\u6240\u4ee5\u5bf9\u4e8e\u6b63\u5728\u8fd0\u884c\u7684\u6570\u636e\u8868\u7684\u7d22\u5f15\u6587\u4ef6\uff08MYI\uff09\u4e00\u822c\u90fd\u662f\u4e0d\u5b8c\u6574\u7684\u3002\u5982\u679c\u6b64\u65f6\u62f7\u8d1d\u6216\u8005\u79fb\u52a8\u8fd9\u4e9b\u7d22\u5f15\u6587\u4ef6\u3002\u591a\u534a\u4f1a\u51fa\u73b0\u7d22\u5f15\u6587\u4ef6\u635f\u574f\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7Flush table\u547d\u4ee4\u6765\u5c06Key Cache\u4e2d\u7684block\u90fdflush\u5230\u78c1\u76d8\u4e0a\u3002\u6240\u4ee5\uff0c\u4e00\u822c\u8981\u52a8\u6001\u79fb\u52a8MyISAM\u8868\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p>\u9996\u5148\uff0c\u5237\u65b0\u6570\u636e\u8868\uff0c\u5e76\u9501\u4f4f\u6570\u636e\u8868\uff1a\uff08<strong><font color=\"red\">\u6ce8\u610f\uff0c\u8bf7\u4e0d\u8981\u5728\u4e1a\u52a1\u9ad8\u5cf0\u671f\u6267\u884c<\/font><\/strong>\uff09<\/p>\n<div class=\"mycode\">\nFLUSH TABLES WITH READ LOCK;\n<\/div>\n<p>\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u67e5\u770b\u6ca1\u6709\u88abFlush\u7684\u7d22\u5f15\u5757\u6570\u91cf<\/p>\n<pre>\r\n<div class=\"mycode\">\r\nmysql> show status like \"Key_blocks_not_flushed\"; \r\n+------------------------+----------+\r\n| Variable_name          | Value    |\r\n+------------------------+----------+\r\n| Key_blocks_not_flushed | 0        | \r\n+------------------------+----------+\r\n<\/div><\/pre>\n<p>\u6700\u540e\uff0c\u79fb\u52a8\u5bf9\u5e94\u7684\u6587\u4ef6\uff08MYI MYD FRM\uff09\u3002<\/p>\n<p>\u53c2\u8003<\/p>\n<ol>\n<li><a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/myisam-key-cache.html\">MySQL Manual about Key cache<\/a><\/li>\n<\/ol>\n<p><\/p>\n<p><strong>Update 2010-03-01:<\/strong><\/p>\n<p>\u5728<a href=\"http:\/\/www.mysqlperformanceblog.com\/2010\/02\/28\/why-you-should-ignore-mysqls-key-cache-hit-ratio\/\">Why you should ignore MySQL\u2019s key cache hit ratio<\/a>\u4e2d\uff0cBaron Schwartz\u63d0\u5230\u5f88\u591a\u5173\u4e8eKey Cache\u547d\u4e2d\u7387\u7684\u95ee\u9898\uff1a\u4e00\u822c\u60c5\u51b5\uff0c\u8bf8\u5982\u201c\u547d\u4e2d\u7387\u201d\u8fd9\u6837\u53c2\u6570\u672c\u8eab\u7684\u610f\u4e49\u5e76\u4e0d\u660e\u663e\uff0c\u5386\u53f2\u6570\u636e\u5bf9\u6bd4\u624d\u6bd4\u8f83\u9760\u8c31\uff08\u6587\u4e2d\u63d0\u5230\u7684\u201cKey_reads\/Uptime\u201d\uff09\uff1b\u5173\u4e8eKey_buffer_size\u5927\u5c0f\uff0cBaron Schwartz\u8ba4\u4e3a\u5e76\u6ca1\u6709\u7edf\u4e00\u7684\u6807\u51c6\uff0c\u800c\u662f\u5e94\u8be5\u5177\u4f53\u60c5\u51b5\u5177\u4f53\u5206\u6790\uff0c\u5927\u5c0f\u5c3d\u91cf\u5927\u4e8e\u4f60\u6700\u5e38\u88ab\u8bbf\u95ee\u7684Key block\uff08 &#8220;In my opinion, you should choose a key_buffer_size that is large enough to hold your working set &#8212; the index blocks that are frequently used.&#8221;\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u4e24\u7bc7\uff08\u524d\u7bc7\u3001\u4e2d\u7bc7\uff09\u4e2d\uff0c\u5206\u522b\u4ecb\u7ecd\u4e86Key Cache\u7684\u57fa\u672c\u539f\u7406\uff08LRU\u548cMidpoint Insertion Strategy\uff09\u3002\u6700\u540e\uff0c\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u76f8\u5173\u7684\u53c2\u6570\u3001\u72b6\u6001\u53c2\u6570\u548c\u547d\u4ee4\u3002 Key Cache\u7684\u914d\u7f6e\u5f88\u7075\u6d3b\uff0c\u53ef\u4ee5\u9488\u5bf9\u5168\u5c40\u914d\u7f6e\uff0c\u8fd8\u53ef\u4ee5\u9488\u5bf9\u67d0\u4e2a\u5355\u72ec\u6570\u636e\u8868\u5206\u914dKey Cache\u7684\u5927\u5c0f\uff1b\u5982\u679c\u4e00\u4e2a\u6570\u636e\u8868\u67d0\u90e8\u5206\u7684\u7d22\u5f15\u5757\u88ab\u8bbf\u95ee\u7684\u975e\u5e38\u9891\u7e41\uff08\u8f83\u4e4b\u5176\u4ed6\u7d22\u5f15\u5757\uff09\uff0c\u90a3\u4e48\u53ef\u4ee5\u914d\u7f6eMidpoint Insertion Strategy\u8fbe\u5230\u6700\u5927\u7684\u5229\u7528\u7387(\u53c2\u8003)\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[6],"tags":[44,43],"class_list":["post-1283","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-myisam","tag-myisam-key-buffer"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/1283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=1283"}],"version-history":[{"count":27,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/1283\/revisions"}],"predecessor-version":[{"id":1503,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/1283\/revisions\/1503"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=1283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=1283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=1283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}