{"id":1327,"date":"2012-01-13T14:29:45","date_gmt":"2012-01-13T06:29:45","guid":{"rendered":"http:\/\/doophp.sinaapp.com\/?p=1327"},"modified":"2012-01-14T11:25:43","modified_gmt":"2012-01-14T03:25:43","slug":"myisam-key-buffer","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/mysql\/myisam-key-buffer.html","title":{"rendered":"\u8be6\u89e3MyISAM Key Cache"},"content":{"rendered":"

\u201cCache\u4e3a\u738b\u201d\uff0c\u65e0\u6240\u4e0d\u5728\u3002\u4e3a\u4e86\u6700\u5c0f\u5316\u78c1\u76d8I\/O\uff0cMyISAM\u5c06\u6700\u9891\u7e41\u8bbf\u95ee\u7684\u7d22\u5f15\u5757\uff08\u201cindex block\u201d\uff09\u90fd\u653e\u5728\u5185\u5b58\u4e2d\uff0c\u8fd9\u6837\u7684\u5185\u5b58\u7f13\u51b2\u533a\u6211\u4eec\u79f0\u4e4b\u4e3aKey Cache\uff0c\u5b83\u7684\u5927\u5c0f\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570key_buffer_size\u6765\u63a7\u5236\u3002\u5728MyISAM\u7684\u7d22\u5f15\u6587\u4ef6\u4e2d\uff08MYI\uff09\uff0c\u8fde\u7eed\u7684\u5355\u5143\uff08contiguous unit\uff09\u7ec4\u6210\u4e00\u4e2aBlock\uff0cIndex block\u7684\u5927\u5c0f\u7b49\u4e8e\u8be5BTree\u7d22\u5f15\u8282\u70b9\u7684\u5927\u5c0f\u3002Key Cache\u5c31\u662f\u4ee5Block\u4e3a\u5355\u4f4d\u7684\u3002<\/p>\n

1. MyISAM\u5982\u4f55\u4f7f\u7528Key Cache<\/p>\n

\u5f53MySQL\u8bf7\u6c42(\u8bfb\u6216\u5199)MyISAM\u7d22\u5f15\u6587\u4ef6\u4e2d\u67d0\u4e2aIndex Block\u65f6\uff0c\u9996\u5148\u4f1a\u770bKey Cache\u961f\u5217\u4e2d\u662f\u5426\u5df2\u7ecf\u7f13\u5b58\u4e86\u5bf9\u5e94block\u3002\u5982\u679c\u6709\uff0c\u5c31\u76f4\u63a5\u5728Key Cache\u961f\u5217\u4e2d\u8fdb\u884c\u8bfb\u5199\u4e86\uff0c\u4e0d\u518d\u9700\u8981\u8bf7\u6c42\u78c1\u76d8\u3002\u5982\u679c\u662f\u5199\u8bf7\u6c42\uff0c\u90a3\u4e48Key Cache\u4e2d\u7684\u5bf9\u5e94Block\u5c31\u4f1a\u88ab\u6807\u8bb0\u4e3aDirty\uff08\u548c\u78c1\u76d8\u4e0d\u4e00\u81f4\uff09\u3002\u5728MyISAM\u5728Key Cache\u6210\u529f\u8bf7\u6c42\uff08\u8bfb\u5199\uff09\u67d0\u4e2aBlock\u540e\uff0c\u4f1a\u5c06\u8be5Block\u653e\u5230Key Cache\u961f\u5217\u7684\u5934\u90e8\u3002<\/p>\n

\u5982\u679cKey Cache\u4e2d\u6ca1\u6709\u5f85\u8bf7\u6c42\uff08\u8bfb\u6216\u5199\uff09\u7684Block\uff0cMyISAM\u4f1a\u5411\u78c1\u76d8\u8bf7\u6c42\u5bf9\u5e94\u7684Block\uff0c\u5e76\u5c06\u5176\u653e\u5230Key Cache\u7684\u961f\u5217\u5934\u90e8\u3002\u961f\u5217\u5982\u679c\u6ee1\u4e86\uff0c\u4f1a\u5c06\u961f\u5217\u5c3e\u90e8\u7684Block\u5220\u9664\uff0c\u8be5Block\u5982\u679c\u662fDirty\u7684\uff0c\u4f1a\u5c06\u5176Flush\u5230\u78c1\u76d8\u4e0a\u3002\u6211\u4eec\u770b\u5230MyISAM\u7ef4\u62a4\u4e86\u4e00\u4e2aLRU\uff08Least Recently Used\uff09\u7684Key Cache\u961f\u5217\u3002\u961f\u5217\u4e2d\u7684Dirty Block\u4f1a\u5728Block\u88ab\u8e22\u51fa\u961f\u5217\u65f6Flush\u5230\u78c1\u76d8\u4e0a\u3002
\n
\n2. \u56fe\u89e3<\/p>\n

\u4e0b\u56fe\u5c55\u793a\u4e86\u8bbf\u95eeIndex Block\u7684\u8fc7\u7a0b\uff1a\uff08\u9ed1\u8272\u90e8\u5206\u4e3a\u78c1\u76d8\u4e2d\u7684Index\u6587\u4ef6\uff09<\/p>\n

\"\"<\/a><\/p>\n

3. \u5e76\u53d1\u8bbf\u95ee<\/p>\n

Key Cache\u4e2d\u7684index Block\u662f\u53ef\u4ee5\u88ab\u5e76\u53d1\u8bbf\u95ee\u7684\uff08Shared access \uff09\uff0c\u4e0b\u9762\u662f\u4e00\u4e9b\u89c4\u5219\uff1a<\/p>\n

\u591a\u4e2a\u6ca1\u6709\u66f4\u65b0\u64cd\u4f5c\u7684session\u53ef\u4ee5\u5e76\u53d1\u540c\u4e00\u4e2ablock buffer<\/p>\n

\u591a\u4e2asession\u540c\u65f6\u8bbf\u95ee\u67d0\u4e00\u4e2ablock buffer\uff0c\u5982\u679c\u67d0\u4e2asession\u662fupdate\u64cd\u4f5c\uff0c\u5219\u4f18\u5148\u8bbf\u95ee<\/p>\n

\u591a\u4e2asession\u5982\u679c\u90fd\u9700\u8981\u8fdb\u884cblock replacement\uff0c\u662f\u53ef\u4ee5\u5e76\u53d1\u64cd\u4f5c\u3002\uff08\u4eceindex file\u4e2d\u8bfb\u53d6block\u66f4\u65b0\u5230key cache\uff0c\u4f46\u662fkey cache\u5df2\u6ee1\uff0c\u9700\u8981\u5220\u9664\u4e00\u4e9bblock buffer\u7684\u64cd\u4f5c\u53eb\u505ablock replacement\uff09<\/p>\n

4. \u8865\u5145\u8bf4\u660e<\/p>\n

Key cache\u4e2d\u7684Block\u5927\u5c0f\u53ef\u80fd\u548c\u7d22\u5f15\u6587\u4ef6\u4e2d\u7684Index Block\u5927\u5c0f\u4e0d\u540c\uff0c\u53ef\u80fd\u662f\u5927\u4e8e\u3001\u5c0f\u4e8e\u3001\u7b49\u4e8e\u4e2d\u7684\u4efb\u4f55\u4e00\u79cd\uff0c\u4f46\u662f\u4e00\u822c\u90fd\u662f\u6210\u500d\u6570\u5173\u7cfb\u7684\u3002Key Cache\u7684block\u5927\u5c0f\u7531\u53c2\u6570Key_cache_block_size\u63a7\u5236\u3002<\/p>\n

\u5728\u524d\u7bc7<\/a>\u4e2d\u4ecb\u7ecd\u4e86Key Cache\u7684\u57fa\u672c\u673a\u5236\uff0c\u5e76\u4e14\u4ecb\u7ecd\u4e86Key Cache\u7684LRU\u7b97\u6cd5\u3002\u4f5c\u4e3a\u5bf9LRU\u7b97\u6cd5\u7684\u6539\u8fdb\uff0cMyISAM\u8fd8\u63d0\u4f9b\u4e86\u53e6\u4e00\u4e2a\u7f13\u5b58\u7b97\u6cd5\uff1a\u201cMidpoint Insertion Strategy\u201d\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecd\u8be5\u7b97\u6cd5\u7684\u539f\u7406\u548c\u914d\u7f6e\u3002<\/p>\n

1. \u76f8\u5173\u53c2\u6570<\/p>\n

\u8be5\u7b56\u7565\u6d89\u53ca\u7684\u53c2\u6570\u6709\uff1akey_cache_division_limit<\/a>\u3001key_cache_age_threshold<\/a><\/p>\n

2. \u539f\u7406\u4ecb\u7ecd<\/p>\n

(1) \u8be5\u7b56\u7565\u5c06\u524d\u9762\u7684LRU\u961f\u5217\uff08LRU Chain\uff09\u5206\u6210\u4e24\u90e8\u5206\uff0chot sub-chain\u548cwarm sub-chain\u3002\u5e76\u6839\u636e\u53c2\u6570key_cache_division_limit\u5212\u5206\uff0c\u603b\u4fdd\u6301warm sub-chain\u5728\u8fd9\u4e2a\u767e\u5206\u6bd4\u4ee5\u4e0a\u3002\u9ed8\u8ba4\u60c5\u51b5key_cache_division_limit\u662f100\uff0c\u6240\u4ee5\u9ed8\u8ba4\u65f6\u5019\u53ea\u6709warm sub-chain,\u5373LRU Chain\u3002
\n(\u6ce8\uff1aMultiple Key cache\u60c5\u51b5\uff0c\u6bcf\u4e2akey cache\u90fd\u6709\u5bf9\u5e94\u7684key_cache_division_limit\u503c)<\/p>\n

(2) \u5728warm sub-chain\u4e2d\u7684\u67d0\u4e2ablock\u5982\u679c\u88ab\u8bbf\u95ee\uff08Access\uff09\u6b21\u6570\u8d85\u8fc7\u67d0\u4e2a\u503c\u65f6\u5019\uff0c\u5c31\u5c06\u8be5block\u653e\u5230hot sub-chain\u7684\u5e95\u90e8\u3002<\/p>\n

(3) \u5728hot sub-chain\u4e2d\u7684block\u4f1a\u968f\u7740\u6bcf\u4e00\u6b21\u7684hit\u8c03\u6574\u4f4d\u7f6e\uff0chit\u8d8a\u591a\uff0c\u8d8a\u63a5\u8fd1\u5e95\u90e8\u3002\u5728\u9876\u90e8\u505c\u7559\u65f6\u95f4\u8fc7\u957f\u5c31\u4f1a\u88ab\u964d\u7ea7\u5230warm sub-chain\u4e2d\uff0c\u800c\u4e14\u662fwarm sub-chain\u7684\u9876\u90e8\uff08\u5f88\u53ef\u80fd\u5f88\u5feb\u5c31\u4f1a\u88ab\u79fb\u51fakey cache\uff09\u3002<\/p>\n

(4) Hot sub-chain\u4e2d\u7684\u9876\u90e8\u7684block\u505c\u7559\u65f6\u95f4\u8d85\u8fc7\u4e00\u4e2a\u9608\u503c\u540e\u5c31\u4f1a\u88ab\u964d\u7ea7\u5230warm sub-chain\u3002\u8fd9\u4e2a\u9608\u503c\u7531\u53c2\u6570key_cache_age_threshold\u51b3\u5b9a\u3002\u5177\u4f53\u7684\u8ba1\u7b97\u65b9\u6cd5\u662f\uff1a\u8bbeN\u4e3akey cache\u4e2d\u7684block\u4e2a\u6570\uff0c\u5982\u679c\u5728\u6700\u8fd1\u7684\uff08N*key_cache_age_threshold\/100\uff09\u6b21\u8bbf\u95ee\u4e2d\uff0ckey cache\u9876\u90e8\u7684block\u4ecd\u7136\u6ca1\u6709\u88ab\u8bbf\u95ee\u5230\uff0c\u90a3\u4e48\u5c31\u4f1a\u88ab\u79fb\u5230warm sub-chain\u7684\u9876\u90e8\u3002\u6ce8\u610f\uff0cwarm sub-chain \u9876\u90e8\u662f\u6700\u4f18\u5148\u88ab\u66ff\u6362\u6389\u7684\uff0c\u4e5f\u5373key block \u4ecehot sub-chain\u5230warm sub-chain\u5c06\u5f88\u5feb\u88ab\u66ff\u6362<\/em><\/p>\n

(5) \u9ed8\u8ba4\u60c5\u51b5key_cache_division_limit = 100\uff0c\u8fd9\u65f6\u53ea\u6709\u53ea\u6709\u4e00\u4e2aChain\uff0c\u6240\u4ee5\u4e0d\u4f7f\u7528\u8be5\u7b56\u7565\u3002\u5373\u9000\u5316\u7684Midpoint Insertion Strategy\u662fLRU\u7b97\u6cd5\u3002<\/p>\n

3. \u5982\u4f55\u4f7f\u7528Midpoint Insertion Strategy<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6ekey_cache_division_limit<\/a>\u3001key_cache_age_threshold<\/a>\u7684\u503c\u6765\u63a7\u5236\u3002\u53c2\u6570key_cache_division_limit<\/a>\u63a7\u5236\u4e86Key Cache Chain\u4e2dwarm sub-chain\u7684\u767e\u5206\u6bd4\uff0c\u5982\u679c\u4f60\u7684Index Block\u4e2d\u660e\u663e\u670930%\u662f\u975e\u5e38Hot\uff08\u8f83\u4e4b\u5176\u4ed6\u7684Block\u66f4\u52a0\u88ab\u5e38\u5e38\u8bbf\u95ee\uff09\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u8bbe\u7f6e\u4f60\u7684warm sub-chain\u957f\u5ea6\u4e3a70%\uff0c\u5269\u4f5930%\u4f5c\u4e3ahot sub-chain\u3002\u53c2\u6570key_cache_age_threshold<\/a>\u5b9a\u4e49\u4e86warm sub-chain\u4e2d\u7684block\u88ab\u79fb\u9664\u7684\u673a\u5236\uff08\u53c2\u7167\u524d\u6587\u4ecb\u7ecd\uff09\u3002<\/p>\n

\u5728\u524d\u4e24\u7bc7\uff08\u524d\u7bc7<\/a>\u3001\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

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<\/a>)\u3002<\/p>\n

1. \u5982\u4f55\u914d\u7f6eKey Cache\u7684\u5927\u5c0f<\/p>\n

\n
#\u914d\u7f6e\u6587\u4ef6my.cnf
\nkey_buffer_size=50*1024*1024<\/div>\n<\/blockquote>\n

\u53e6\u5916\uff0cKey Cache\u7684\u5927\u5c0f\u53ef\u4ee5\u52a8\u6001\u7684\u6539\u53d8<\/p>\n

2. \u7ed9\u6570\u636e\u8868\u5212\u5206\u5355\u72ec\u7684Key Cache<\/p>\n

\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

\n
SET GLOBAL hot_cache.key_buffer_size=128*1024;
\nCACHE INDEX t1 IN hot_cache;
\nSET GLOBAL hot_cache.key_buffer_size=0;<\/div>\n<\/blockquote>\n

3. \u9884\u5148\u8f7d\u5165\u67d0\u4e9b\u6570\u636e\u8868\u7684\u7d22\u5f15<\/p>\n

\n
LOAD INDEX INTO CACHE t1, t2<\/div>\n<\/blockquote>\n

4. \u5173\u4e8eKey Cache\u7684\u4f7f\u7528\u60c5\u51b5\u89c2\u5bdf Flush\u73b0\u8c61<\/p>\n

\"\"<\/a><\/div>\n

5. \u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879<\/p>\n

\u5185\u5b58\u4e2d\u7f13\u5b58\u7684\u7d22\u5f15\u5757\uff08Key Cache\uff09\uff0c\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

\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

\u9996\u5148\uff0c\u5237\u65b0\u6570\u636e\u8868\uff0c\u5e76\u9501\u4f4f\u6570\u636e\u8868\uff1a\uff08\u6ce8\u610f\uff0c\u8bf7\u4e0d\u8981\u5728\u4e1a\u52a1\u9ad8\u5cf0\u671f\u6267\u884c<\/span><\/strong>\uff09<\/p>\n

\n
FLUSH TABLES WITH READ LOCK;<\/div>\n<\/blockquote>\n

\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u67e5\u770b\u6ca1\u6709\u88abFlush\u7684\u7d22\u5f15\u5757\u6570\u91cf<\/p>\n

\"\"<\/a><\/div>\n

\u6700\u540e\uff0c\u79fb\u52a8\u5bf9\u5e94\u7684\u6587\u4ef6\uff08MYI MYD FRM\uff09\u3002<\/p>\n

\u53c2\u8003<\/p>\n

    \n
  1. MySQL Manual about Key cache<\/a><\/li>\n<\/ol>\n

    Update 2010-03-01:<\/strong><\/p>\n

    \u5728Why 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 “In my opinion, you should choose a key_buffer_size that is large enough to hold your working set — the index blocks that are frequently used.”\uff09\u3002<\/p>\n

    \u539f\u6587\uff1ahttp:\/\/www.orczhou.com\/index.php\/2010\/01\/myisam-key-buffer-1\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

    \u201cCache\u4e3a\u738b\u201d\uff0c\u65e0\u6240\u4e0d\u5728\u3002\u4e3a\u4e86\u6700\u5c0f\u5316\u78c1\u76d8I\/O\uff0cMyISAM\u5c06\u6700\u9891\u7e41\u8bbf\u95ee\u7684\u7d22\u5f15\u5757\uff08\u201cindex block […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[30,29,132,31],"_links":{"self":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1327"}],"collection":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/comments?post=1327"}],"version-history":[{"count":9,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1327\/revisions"}],"predecessor-version":[{"id":1349,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1327\/revisions\/1349"}],"wp:attachment":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/media?parent=1327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/categories?post=1327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/tags?post=1327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}