{"id":1339,"date":"2015-12-07T21:13:55","date_gmt":"2015-12-07T13:13:55","guid":{"rendered":"http:\/\/coderbee.net\/?p=1339"},"modified":"2015-12-10T22:33:51","modified_gmt":"2015-12-10T14:33:51","slug":"%e3%80%8a%e9%ab%98%e6%80%a7%e8%83%bd-mysql%e3%80%8b-%e7%ac%ac%e4%ba%94%e7%ab%a0-%e5%88%9b%e5%bb%ba%e9%ab%98%e6%80%a7%e8%83%bd%e7%9a%84%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/db\/20151207\/1339","title":{"rendered":"\u300a\u9ad8\u6027\u80fd MySQL\u300b &#8212; \u7b2c\u4e94\u7ae0 \u521b\u5efa\u9ad8\u6027\u80fd\u7684\u7d22\u5f15"},"content":{"rendered":"<h2>\u7d22\u5f15\u57fa\u7840<\/h2>\n<p>\u7d22\u5f15\u7684\u4f7f\u7528\uff1a\u73b0\u5728\u7d22\u5f15\u4e2d\u627e\u5230\u5bf9\u5e94\u503c\uff0c\u7136\u540e\u6839\u636e\u5339\u914d\u7684\u7d22\u5f15\u8bb0\u5f55\u627e\u5230\u5bf9\u5e94\u7684\u6570\u636e\u884c\u3002<\/p>\n<p>\u5bf9\u4e8e\u6709\u591a\u5217\u7684\u7d22\u5f15\uff0cMySQL \u53ea\u80fd\u9ad8\u6548\u5730\u4f7f\u7528\u7d22\u5f15\u7684\u6700\u5de6\u524d\u7f00\u5217\u3002\u56e0\u6b64\u5217\u7684\u987a\u5e8f\u5f88\u91cd\u8981\u3002<\/p>\n<p>MySQL \u7684\u552f\u4e00\u9650\u5236\u548c\u4e3b\u952e\u9650\u5236\u90fd\u662f\u901a\u8fc7\u7d22\u5f15\u5b9e\u73b0\u3002<\/p>\n<h3>\u7d22\u5f15\u7684\u7c7b\u578b<\/h3>\n<p>MySQL \u4e2d\uff0c\u7d22\u5f15\u662f\u5728\u5b58\u50a8\u5f15\u64ce\u5c42\u800c\u4e0d\u662f\u670d\u52a1\u5668\u5c42\u5b9e\u73b0\u7684\uff0c\u6240\u4ee5\u6ca1\u6709\u7edf\u4e00\u7684\u7d22\u5f15\u6807\u51c6\u3002<\/p>\n<h4>B-Tree \u7d22\u5f15<\/h4>\n<p>B-Tree \u901a\u5e38\u610f\u5473\u7740\u6240\u6709\u7684\u503c\u90fd\u662f\u6309\u987a\u5e8f\u5b58\u50a8\u7684\uff0c\u5e76\u4e14\u6bcf\u4e00\u4e2a\u53f6\u5b50\u9875\u5230\u6839\u7684\u8ddd\u79bb\u76f8\u7b49\uff0c\u9002\u5408\u8303\u56f4\u67e5\u8be2\u3002<\/p>\n<p><img decoding=\"async\" src=\"\/\/coderbee.net\/wp-content\/uploads\/2015\/12\/btree-structure.png\" alt=\"btree-structure\" \/><\/p>\n<p>B-Tree \u7d22\u5f15\u9002\u7528\u4e8e\u5168\u952e\u503c\u3001\u952e\u503c\u8303\u56f4\u6216\u952e\u524d\u7f00\u67e5\u627e\uff0c\u5176\u4e2d\u952e\u524d\u7f00\u67e5\u627e\u53ea\u9002\u7528\u4e8e\u6839\u636e\u6700\u5de6\u524d\u7f00\u7684\u67e5\u627e\u3002<\/p>\n<p>\u7d22\u5f15\u8fd8\u53ef\u4ee5\u7528\u4e8e\u67e5\u8be2\u4e2d\u7684 <code>order by<\/code> \u64cd\u4f5c\u3002<\/p>\n<p>B-Tree \u7d22\u5f15\u7684\u9650\u5236\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u4e0d\u662f\u5b89\u88c5\u7d22\u5f15\u7684\u6700\u5de6\u5217\u5f00\u59cb\u67e5\u627e\uff0c\u5219\u65e0\u6cd5\u4f7f\u7528\u7d22\u5f15\u3002<\/li>\n<li>\u4e0d\u80fd\u8df3\u8fc7\u7d22\u5f15\u4e2d\u7684\u5217\u3002<\/li>\n<li>\u5982\u679c\u67e5\u8be2\u4e2d\u6709\u67d0\u4e2a\u5217\u7684\u8303\u56f4\u67e5\u8be2\uff0c\u5219\u5176\u53f3\u8fb9\u6240\u6709\u5217\u90fd\u65e0\u6cd5\u4f7f\u7528\u7d22\u5f15\u4f18\u5316\u67e5\u627e\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u9650\u5236\u662f MySQL \u4f18\u5316\u5668\u548c\u5b58\u50a8\u5f15\u64ce\u4f7f\u7528\u7d22\u5f15\u7684\u65b9\u5f0f\u5bfc\u81f4\u7684\u3002<\/p>\n<p><!--more--><\/p>\n<h4>\u54c8\u5e0c\u7d22\u5f15<\/h4>\n<p>\u54c8\u5e0c\u7d22\u5f15\uff08hash index\uff09\u57fa\u4e8e\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u53ea\u6709\u7cbe\u786e\u5339\u914d\u7d22\u5f15\u6240\u6709\u5217\u7684\u67e5\u8be2\u624d\u6709\u6548\u3002\u5bf9\u4e8e\u6bcf\u4e00\u884c\u6570\u636e\uff0c\u5b58\u50a8\u5f15\u64ce\u90fd\u4f1a\u5bf9\u6240\u6709\u7684\u7d22\u5f15\u5217\u8ba1\u7b97\u4e00\u4e2a\u54c8\u5e0c\u7801\uff0c\u54c8\u5e0c\u7801\u662f\u4e00\u4e2a\u8f83\u5c0f\u7684\u503c\uff0c\u5e76\u4e14\u4e0d\u540c\u952e\u503c\u7684\u884c\u8ba1\u7b97\u51fa\u6765\u7684\u54c8\u5e0c\u7801\u4e5f\u4e0d\u4e00\u6837\u3002\u54c8\u5e0c\u7d22\u5f15\u5c06\u6240\u6709\u7684\u54c8\u5e0c\u7801\u5b58\u50a8\u5728\u7d22\u5f15\u4e2d\uff0c\u540c\u65f6\u5728\u54c8\u5e0c\u8868\u4e2d\u4fdd\u5b58\u6307\u5411\u6bcf\u4e2a\u6570\u636e\u884c\u7684\u6307\u9488\u3002<\/p>\n<p>\u54c8\u5e0c\u7d22\u5f15\u53ea\u9700\u5b58\u50a8\u5bf9\u5e94\u7684\u54c8\u5e0c\u503c\uff0c\u6240\u4ee5\u7d22\u5f15\u7ed3\u6784\u5341\u5206\u7d27\u51d1\uff0c\u4f7f\u67e5\u627e\u5f88\u5feb\u3002<\/p>\n<p>\u54c8\u5e0c\u7d22\u5f15\u7684\u9650\u5236\uff1a<\/p>\n<ul>\n<li>\u54c8\u5e0c\u7d22\u5f15\u53ea\u5305\u542b\u54c8\u5e0c\u503c\u548c\u884c\u6307\u9488\uff0c\u800c\u4e0d\u5b58\u50a8\u5b57\u6bb5\u503c\uff0c\u6240\u4ee5\u4e0d\u80fd\u4f7f\u7528\u7d22\u5f15\u4e2d\u7684\u503c\u6765\u907f\u514d\u8bfb\u53d6\u884c\u3002<\/li>\n<li>\u54c8\u5e0c\u7d22\u5f15\u6570\u636e\u5e76\u4e0d\u662f\u6309\u7167\u7d22\u5f15\u503c\u987a\u5e8f\u5b58\u50a8\u7684\uff0c\u6240\u4ee5\u4e5f\u5c31\u65e0\u6cd5\u7528\u4e8e\u6392\u5e8f\u3002<\/li>\n<li>\u54c8\u5e0c\u7d22\u5f15\u4e5f\u4e0d\u652f\u6301\u90e8\u5206\u7d22\u5f15\u5217\u5339\u914d\u67e5\u627e\uff0c\u56e0\u4e3a\u54c8\u5e0c\u7d22\u5f15\u59cb\u7ec8\u662f\u4f7f\u7528\u7d22\u5f15\u5217\u7684\u5168\u90e8\u5185\u5bb9\u6765\u8ba1\u7b97\u54c8\u5e0c\u503c\u7684\u3002<\/li>\n<li>\u54c8\u5e0c\u7d22\u5f15\u53ea\u652f\u6301\u7b49\u503c\u6bd4\u8f83\u67e5\u8be2\u3002<\/li>\n<li>\u8bbf\u95ee\u54c8\u5e0c\u7d22\u5f15\u7684\u6570\u636e\u975e\u5e38\u5feb\uff0c\u9664\u975e\u6709\u5f88\u591a\u54c8\u5e0c\u51b2\u7a81\u3002<\/li>\n<li>\u5982\u679c\u54c8\u5e0c\u51b2\u7a81\u5f88\u591a\u7684\u8bdd\uff0c\u4e00\u4e9b\u7d22\u5f15\u7ef4\u62a4\u64cd\u4f5c\u7684\u4ee3\u4ef7\u4f1a\u5f88\u9ad8\u3002\uff08\u54c8\u5e0c\u51b2\u7a81\u65f6\uff0c\u76f8\u540c\u54c8\u5e0c\u503c\u7684\u884c\u6307\u9488\u4f1a\u5b58\u50a8\u5728\u4e00\u6761\u94fe\u8868\u91cc\uff0c\u7d22\u5f15\u7684\u7ef4\u62a4\u64cd\u4f5c\u9700\u8981\u904d\u5386\u94fe\u8868\u6765\u5b8c\u6210\u3002\uff09<\/li>\n<\/ul>\n<p>InnoDB \u5f15\u64ce\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u529f\u80fd\u53eb\u505a \u201d\u81ea\u9002\u5e94\u54c8\u5e0c\u7d22\u5f15\uff08adaptive hash index\uff09\u201c\u3002\u5f53 InnoDB \u6ce8\u610f\u5230\u67d0\u4e9b\u7d22\u5f15\u503c\u88ab\u4f7f\u7528\u5f97\u975e\u5e38\u9891\u7e41\u65f6\uff0c\u5b83\u4f1a\u5728\u5185\u5b58\u4e2d\u57fa\u4e8e B-Tree \u7d22\u5f15\u4e4b\u4e0a\u518d\u521b\u5efa\u4e00\u4e2a\u54c8\u5e0c\u7d22\u5f15\uff0c\u8fd9\u8ba9 B-Tree \u7d22\u5f15\u4e5f\u5177\u6709\u54c8\u5e0c\u7d22\u5f15\u7684\u4e00\u4e9b\u4f18\u70b9\uff0c\u6bd4\u5982\u5feb\u901f\u7684\u54c8\u5e0c\u67e5\u627e\u3002<\/p>\n<h2>\u7d22\u5f15\u7684\u4f18\u70b9<\/h2>\n<ol>\n<li>\u7d22\u5f15\u5927\u5927\u51cf\u5c11\u4e86\u670d\u52a1\u5668\u9700\u8981\u626b\u63cf\u7684\u6570\u636e\u91cf\u3002<\/li>\n<li>\u7d22\u5f15\u53ef\u4ee5\u5e2e\u52a9\u670d\u52a1\u5668\u907f\u514d\u6392\u5e8f\u548c\u4e34\u65f6\u8868\u3002<\/li>\n<li>\u7d22\u5f15\u53ef\u4ee5\u5c06\u968f\u673a <code>I\/O<\/code> \u53d8\u4e3a\u987a\u5e8f <code>I\/O<\/code> \u3002<\/li>\n<\/ol>\n<p>\u8bc4\u4ef7\u4e00\u4e2a\u7d22\u5f15\u662f\u5426\u9002\u5408\u67d0\u4e2a\u67e5\u8be2\u7684\u201d\u4e09\u661f\u7cfb\u7edf\u201c\uff1a\u7d22\u5f15\u5c06\u76f8\u5173\u7684\u8bb0\u5f55\u653e\u5230\u4e00\u8d77\u5219\u83b7\u5f97\u4e00\u661f\uff1b\u5982\u679c\u7d22\u5f15\u4e2d\u7684\u6570\u636e\u987a\u5e8f\u548c\u67e5\u627e\u4e2d\u7684\u6392\u5217\u987a\u5e8f\u4e00\u81f4\u5219\u83b7\u5f97\u4e8c\u661f\uff1b\u5982\u679c\u7d22\u5f15\u4e2d\u7684\u5217\u5305\u542b\u4e86\u67e5\u8be2\u4e2d\u9700\u8981\u7684\u5168\u90e8\u5217\u5219\u83b7\u5f97\u4e09\u661f\u3002<\/p>\n<h2>\u9ad8\u6027\u80fd\u7684\u7d22\u5f15\u7b56\u7565<\/h2>\n<p>\u6b63\u786e\u5730\u521b\u5efa\u548c\u4f7f\u7528\u7d22\u5f15\u662f\u5b9e\u73b0\u9ad8\u6027\u80fd\u67e5\u8be2\u7684\u57fa\u7840\u3002<\/p>\n<p>\u7d22\u5f15\u7684\u9009\u62e9\u6027\u662f\u6307\uff1a\u4e0d\u91cd\u590d\u7684\u7d22\u5f15\u503c\u7684\u6570\u91cf\uff08\u4e5f\u79f0\u4e3a\u57fa\u6570\uff0ccardinality\uff09\u548c\u6570\u636e\u8868\u7684\u8bb0\u5f55\u603b\u6570\u7684\u6bd4\u503c\u3002\u7d22\u5f15\u7684\u9009\u62e9\u6027\u8d8a\u641e\u5219\u67e5\u8be2\u6548\u7387\u8d8a\u9ad8\u3002\u552f\u4e00\u6027\u7d22\u5f15\u7684\u9009\u62e9\u6027\u662f 1\uff0c\u662f\u6700\u597d\u7684\u7d22\u5f15\u9009\u62e9\u6027\uff0c\u6027\u80fd\u6700\u9ad8\u3002<\/p>\n<ul>\n<li>\n<p>\u72ec\u7acb\u7684\u5217\uff1a\u6307\u7d22\u5f15\u5217\u4e0d\u80fd\u662f\u8868\u8fbe\u5f0f\u7684\u4e00\u90e8\u5206\uff0c\u4e5f\u4e0d\u80fd\u662f\u51fd\u6570\u7684\u53c2\u6570\u3002\u59cb\u7ec8\u5c06\u7d22\u5f15\u5217\u5355\u72ec\u653e\u5728\u6bd4\u8f83\u7b26\u53f7\u7684\u4e00\u4fa7\u3002<\/p>\n<\/li>\n<li>\n<p>\u524d\u7f00\u7d22\u5f15\u548c\u7d22\u5f15\u9009\u62e9\u6027\uff1a\u5bf9\u4e8e\u5f88\u957f\u7684\u5b57\u7b26\u5217\uff0c\u901a\u5e38\u53ef\u4ee5\u7d22\u5f15\u5f00\u59cb\u7684\u90e8\u5206\u5b57\u7b26\uff0c\u8fd9\u6837\u53ef\u4ee5\u8282\u7ea6\u7d22\u5f15\u7a7a\u95f4\uff0c\u63d0\u4f9b\u7d22\u5f15\u6548\u7387\uff0c\u4f46\u4f1a\u964d\u4f4e\u7d22\u5f15\u7684\u9009\u62e9\u6027\u3002\u524d\u7f00\u7684\u201d\u57fa\u6570\u201c\u5e94\u8be5\u63a5\u8fd1\u4e8e\u5b8c\u6574\u5217\u7684\u201d\u57fa\u6570\u201c\u3002MySQL \u65e0\u6cd5\u4f7f\u7528\u524d\u7f00\u7d22\u5f15\u505a order by \u548c group by\uff0c\u4e5f\u65e0\u6cd5\u4f7f\u7528\u524d\u7f00\u7d22\u5f15\u505a\u8986\u76d6\u626b\u63cf\u3002\u521b\u5efa\u524d\u7f00\u7d22\u5f15\uff1a <code>alter  table  table_name  add  key(col_name(prefix_length));<\/code><\/p>\n<\/li>\n<li>\n<p>\u591a\u5217\u7d22\u5f15\uff1aMySQL 5.0 \u53ca\u66f4\u65b0\u7684\u7248\u672c\u5f15\u5165\u4e86\u7d22\u5f15\u5408\u5e76\u7b56\u7565\uff08index merge\uff09\uff0c\u67e5\u8be2\u80fd\u591f\u540c\u65f6\u4f7f\u7528\u8fd9\u4e24\u4e2a\u5355\u5217\u7d22\u5f15\u8fdb\u884c\u626b\u63cf\uff0c\u5e76\u5c06\u7ed3\u679c\u8fdb\u884c\u5408\u5e76\u3002\u8fd9\u79cd\u7b97\u6cd5\u6709\u4e09\u4e2a\u53d8\u79cd\uff1aOR \u6761\u4ef6\u7684\u8054\u5408\uff08union\uff09\uff0c AND \u6761\u4ef6\u7684\u76f8\u4ea4\uff08intersection\uff09\uff0c\u7ec4\u5408\u524d\u4e24\u79cd\u60c5\u51b5\u7684\u8054\u5408\u53ca\u76f8\u4ea4\u3002<\/p>\n<\/li>\n<li>\n<p>\u9009\u62e9\u5408\u9002\u7684\u7d22\u5f15\u5217\u987a\u5e8f\uff1a\u5728\u4e00\u4e2a\u591a\u5217\u7684 B-Tree \u7d22\u5f15\u4e2d\uff0c\u7d22\u5f15\u5217\u7684\u987a\u5e8f\u610f\u5473\u7740\u7d22\u5f15\u9996\u5148\u6309\u7167\u6700\u5de6\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u5176\u6b21\u662f\u7b2c\u4e8c\u5217\uff0c\u7b49\u7b49\u3002\u7d22\u5f15\u53ef\u4ee5\u5b89\u88c5\u5347\u5e8f\u6216\u8005\u964d\u5e8f\u8fdb\u884c\u626b\u63cf\uff0c\u4ee5\u6ee1\u8db3\u7cbe\u786e\u7b26\u5408\u5217\u987a\u5e8f\u7684 <code>order by, group by, distinct<\/code>  \u7b49\u5b50\u53e5\u7684\u67e5\u8be2\u9700\u6c42\u3002\u8003\u8651\u7d22\u5f15\u5217\u7684\u987a\u5e8f\u65f6\uff0c\u5c06\u9009\u62e9\u6027\u6700\u9ad8\u7684\u5217\u653e\u5230\u7d22\u5f15\u5217\u6700\u524d\u5217\uff0c\u4f46\u901a\u5e38\u4e0d\u5982\u907f\u514d\u968f\u673a IO \u548c\u6392\u5e8f\u91cd\u8981\u3002\u6027\u80fd\u4e0d\u53ea\u662f\u4f9d\u8d56\u4e8e\u6240\u6709\u7d22\u5f15\u5217\u7684\u9009\u62e9\u6027\uff0c\u4e5f\u548c\u503c\u7684\u5206\u5e03\u6709\u5173\u3002\uff08<strong>\u9700\u8981\u603b\u548c\u8003\u8651\u9009\u62e9\u6027\u3001\u57fa\u6570\u3001\u6392\u5e8f\u3001\u5206\u7ec4\u3001\u8303\u56f4\u6761\u4ef6\u3001\u503c\u5206\u5e03\u7b49\u56e0\u7d20<\/strong>\uff09<\/p>\n<\/li>\n<\/ul>\n<h3>\u805a\u65cf\u7d22\u5f15<\/h3>\n<p>\u805a\u65cf\u7d22\u5f15\uff08Oracle \u91cc\u5bf9\u5e94\u7684\u662f \u7d22\u5f15\u7ec4\u7ec7\u8868\uff0cindex-organized table, IOT\uff09\u5e76\u4e0d\u662f\u4e00\u79cd\u5355\u72ec\u7684\u7d22\u5f15\u7c7b\u578b\uff0c\u800c\u662f\u4e00\u79cd\u6570\u636e\u5b58\u50a8\u65b9\u5f0f\u3002<\/p>\n<p>\u5f53\u8868\u6709\u805a\u65cf\u7d22\u5f15\u65f6\uff0c\u5b83\u7684\u6570\u636e\u884c\u5b9e\u9645\u4e0a\u662f\u5b58\u653e\u5728\u7d22\u5f15\u7684\u53f6\u5b50\u9875\u4e2d\u3002\u805a\u65cf\u8868\u793a\u6570\u636e\u884c\u548c\u76f8\u90bb\u7684\u952e\u503c\u7d27\u51d1\u5730\u5b58\u50a8\u5728\u4e00\u8d77\u3002\u805a\u65cf\u7d22\u5f15\u7684\u53f6\u5b50\u9875\u5305\u542b\u4e86\u884c\u7684\u5168\u90e8\u6570\u636e\uff0c\u4f46\u662f\u8282\u70b9\u9875\u53ea\u5305\u542b\u4e86\u7d22\u5f15\u5217\u3002<\/p>\n<p>\u805a\u65cf\u7d22\u5f15\u7684\u4f18\u70b9\uff1a<\/p>\n<blockquote>\n<ul>\n<li>\u53ef\u4ee5\u628a\u76f8\u5173\u7684\u6570\u636e\u4fdd\u5b58\u5728\u4e00\u8d77\u3002\uff08\u5229\u7528\u6570\u636e\u7684\u4e34\u8fd1\u6027\uff09<\/li>\n<li>\u6570\u636e\u8bbf\u95ee\u66f4\u5feb\u3002\u7531\u4e8e\u7d22\u5f15\u548c\u6570\u636e\u90fd\u5728\u540c\u4e00\u4e2a B-Tree \u4e2d\uff0c\u4ece\u805a\u65cf\u7d22\u5f15\u83b7\u53d6\u6570\u636e\u6bd4\u975e\u805a\u65cf\u7d22\u5f15\u8981\u5feb\u3002<\/li>\n<li>\u4f7f\u7528\u8986\u76d6\u7d22\u5f15\u626b\u63cf\u7684\u67e5\u8be2\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u9875\u8282\u70b9\u4e2d\u7684\u4e3b\u952e\u503c\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<p>\u805a\u65cf\u7d22\u5f15\u7684\u7f3a\u70b9\uff1a<\/p>\n<blockquote>\n<ul>\n<li>\u63d2\u5165\u901f\u5ea6\u4e25\u91cd\u4f9d\u8d56\u4e8e\u63d2\u5165\u987a\u5e8f\u3002\u6309\u7167\u805a\u65cf\u5217\u7684\u987a\u5e8f\u63d2\u5165\u662f\u6700\u5feb\u7684\u65b9\u5f0f\u3002<\/li>\n<li>\u66f4\u65b0\u805a\u65cf\u7d22\u5f15\u5217\u7684\u4ee3\u4ef7\u5f88\u9ad8\uff0c\u56e0\u4e3a InnoDB \u5c06\u5f3a\u5236\u6bcf\u4e2a\u88ab\u66f4\u65b0\u7684\u884c\u79fb\u52a8\u5230\u65b0\u7684\u4f4d\u7f6e\u3002<\/li>\n<li>\u57fa\u4e8e\u805a\u65cf\u7d22\u5f15\u7684\u8868\u5728\u63d2\u5165\u65b0\u884c\uff0c\u6216\u8005\u4e3b\u952e\u88ab\u66f4\u65b0\u5bfc\u81f4\u9700\u8981\u79fb\u52a8\u884c\u7684\u65f6\u5019\uff0c\u53ef\u80fd\u9762\u4e34\u9875\u5206\u88c2\uff08page split\uff09\u7684\u95ee\u9898\u3002<\/li>\n<li>\u805a\u65cf\u7d22\u5f15\u53ef\u80fd\u5bfc\u81f4\u5168\u8868\u626b\u63cf\u53d8\u6162\uff0c\u5c24\u5176\u662f\u884c\u6bd4\u8f83\u7a00\u758f\uff0c\u6216\u8005\u7531\u4e8e\u9875\u5206\u88c2\u5bfc\u81f4\u6570\u636e\u5b58\u50a8\u4e0d\u8fde\u7eed\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<h4>\u4e8c\u7ea7\u7d22\u5f15<\/h4>\n<p>MySQL \u4e2d\u6bcf\u4e2a\u8868\u90fd\u6709\u4e00\u4e2a\u805a\u7c07\u7d22\u5f15\uff08clustered index \uff09\uff0c\u9664\u6b64\u4e4b\u5916\u7684\u8868\u4e0a\u7684\u6bcf\u4e2a\u975e\u805a\u7c07\u7d22\u5f15\u90fd\u662f\u4e8c\u7ea7\u7d22\u5f15\uff0c\u53c8\u53eb\u8f85\u52a9\u7d22\u5f15\uff08secondary indexes\uff09\u3002<\/p>\n<p>\u4ee5 InnoDB \u6765\u8bf4\uff0c\u6bcf\u4e2a InnoDB \u8868\u5177\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u7d22\u5f15\u79f0\u4e3a\u805a\u96c6\u7d22\u5f15\u3002\u5982\u679c\u60a8\u7684\u8868\u4e0a\u5b9a\u4e49\u6709\u4e3b\u952e\uff0c\u8be5\u4e3b\u952e\u7d22\u5f15\u662f\u805a\u96c6\u7d22\u5f15\u3002\u5982\u679c\u4f60\u4e0d\u5b9a\u4e49\u4e3a\u60a8\u7684\u8868\u7684\u4e3b\u952e \u65f6\uff0cMySQL \u53d6\u7b2c\u4e00\u4e2a\u552f\u4e00\u7d22\u5f15\uff08unique\uff09\u800c\u4e14\u53ea\u542b\u975e\u7a7a\u5217\uff08NOT NULL\uff09\u4f5c\u4e3a\u4e3b\u952e\uff0cInnoDB \u4f7f\u7528\u5b83\u4f5c\u4e3a\u805a\u96c6\u7d22\u5f15\u3002\u5982\u679c\u6ca1\u6709\u8fd9\u6837\u7684\u5217\uff0cInnoDB \u5c31\u81ea\u5df1\u4ea7\u751f\u4e00\u4e2a\u8fd9\u6837\u7684 ID \u503c\uff0c\u5b83\u6709\u516d\u4e2a\u5b57\u8282\uff0c\u800c\u4e14\u662f\u9690\u85cf\u7684\uff0c\u4f7f\u5176\u4f5c \u4e3a\u805a\u7c07\u7d22\u5f15\u3002<\/p>\n<pre><code class=\"sql\">create  table  layout_test (\n     col1  int  not  null,\n     col2  int  not  null,\n     primary  key  (col1),\n     key (col2)\n);\n<\/code><\/pre>\n<p>\u5bf9\u5e94\u7684\uff08\u4e3b\u952e\u3001\uff09\u8868 \u5206\u5e03\uff1a<\/p>\n<p><img decoding=\"async\" src=\"\/\/coderbee.net\/wp-content\/uploads\/2015\/12\/innodb-pk-layout.png\" alt=\"innodb-pk-layout\" \/><\/p>\n<p>\u5176\u4e8c\u7ea7\u7d22\u5f15\u5206\u5e03\u5982\u4e0b\uff1a<br \/>\n<img decoding=\"async\" src=\"\/\/coderbee.net\/wp-content\/uploads\/2015\/12\/innodb-secdonary-index-layout.png\" alt=\"innodb-secdonary-index-layout\" \/><\/p>\n<p>InnoDB \u4e8c\u7ea7\u7d22\u5f15\u7684\u53f6\u5b50\u8282\u70b9\u5b58\u50a8\u7684\u4e0d\u662f\u201d\u884c\u6307\u9488\u201c\uff0c\u800c\u662f\u4e3b\u952e\uff0c\u5e76\u4ee5\u6b64\u4f5c\u4e3a\u6307\u5411\u884c\u7684\u201d\u6307\u9488\u201c\u3002\u8fd9\u6837\u7684\u7b56\u7565\u51cf\u5c11\u4e86\u5f53\u51fa\u73b0\u884c\u79fb\u52a8\u6216\u6570\u636e\u9875\u5206\u88c2\u65f6\u4e8c\u7ea7\u7d22\u5f15\u7684\u7ef4\u62a4\u5de5\u4f5c\u3002<\/p>\n<p>\u805a\u65cf\u548c\u975e\u805a\u65cf\u8868\u5bf9\u6bd4\u56fe\uff1a<br \/>\n<img decoding=\"async\" src=\"\/\/coderbee.net\/wp-content\/uploads\/2015\/12\/cluster-vs-noncluster.png\" alt=\"cluster-vs-noncluster\" \/><\/p>\n<p>\u7531\u4e8e\u4e3b\u952e\u7684\u503c\u662f\u987a\u5e8f\u7684\uff0cInnoDB \u628a\u6bcf\u4e00\u6761\u8bb0\u5f55\u90fd\u5b58\u50a8\u5728\u4e0a\u4e00\u6761\u8bb0\u5f55\u7684\u540e\u9762\uff0c\u5f53\u8fbe\u5230\u9875\u7684\u6700\u5927\u586b\u5145\u56e0\u5b50\u65f6\uff08InnoDB \u9ed8\u8ba4\u7684\u6700\u5927\u586b\u5145\u56e0\u5b50\u662f\u9875\u5927\u5c0f\u7684 15\/16\uff0c\u7559\u51fa\u90e8\u5206\u7a7a\u95f4\u7528\u4e8e\u4ee5\u540e\u4fee\u6539\uff09\uff0c\u4e0b\u4e00\u6761\u8bb0\u5f55\u5c31\u4f1a\u5199\u5165\u65b0\u7684\u9875\u4e2d\u3002<\/p>\n<p>\u6700\u597d\u907f\u514d\u968f\u673a\u7684\uff08\u4e0d\u8fde\u7eed\u4e14\u503c\u7684\u5206\u5e03\u8303\u56f4\u975e\u5e38\u5927\uff09\u805a\u65cf\u7d22\u5f15\uff0c\u7279\u522b\u662f\u5bf9\u4e8e I\/O \u5bc6\u96c6\u578b\u7684\u5e94\u7528\u3002\u4f1a\u5bfc\u81f4\u7684\u4e00\u4e9b\u7f3a\u70b9\uff1a<\/p>\n<blockquote>\n<ul>\n<li>\u5199\u5165\u7684\u76ee\u6807\u9875\u53ef\u80fd\u5df2\u7ecf\u5237\u5230\u78c1\u76d8\u4e0a\u5e76\u4ece\u7f13\u5b58\u4e2d\u79fb\u9664\uff0c\u6216\u8005\u662f\u8fd8\u6ca1\u6709\u88ab\u52a0\u8f7d\u5230\u7f13\u5b58\u4e2d\uff0cInnoDB \u5728\u63d2\u5165\u94b1\u4e0d\u5f97\u4e0d\u5148\u627e\u5230\u5e76\u4ece\u78c1\u76d8\u8bfb\u53d6\u76ee\u6807\u9875\u5230\u5185\u5b58\u4e2d\u3002\u8fd9\u5c06\u5bfc\u81f4\u5927\u91cf\u7684\u968f\u673a I\/O\u3002<\/li>\n<li>\u56e0\u4e3a\u5199\u5165\u662f\u4e71\u5e8f\u7684\uff0cInnoDB \u4e0d\u5f97\u4e0d\u9891\u7e41\u5730\u505a\u9875\u5206\u88c2\u64cd\u4f5c\uff0c\u4ee5\u4fbf\u4e3a\u65b0\u7684\u884c\u5206\u914d\u7a7a\u95f4\u3002<\/li>\n<li>\u7531\u4e8e\u9891\u7e41\u7684\u9875\u5206\u88c2\uff0c\u9875\u4f1a\u53d8\u5f97\u7a00\u758f\u5e76\u88ab\u4e0d\u89c4\u5219\u5730\u586b\u5145\uff0c\u7d22\u5f15\u6700\u7ec8\u6570\u636e\u4f1a\u6709\u788e\u7247\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<p>\u987a\u5e8f\u7684\u4e3b\u952e\u4e5f\u53ef\u80fd\u9020\u6210\u4e0d\u597d\u7684\u7ed3\u679c\uff1a\u5bf9\u4e8e\u9ad8\u5e76\u53d1\u5de5\u4f5c\u8d1f\u8f7d\uff0c\u4e3b\u952e\u7684\u4e0a\u754c\u4f1a\u6210\u4e3a\u201d\u70ed\u70b9\u201c\uff0c\u56e0\u4e3a\u6240\u6709\u7684\u63d2\u5165\u90fd\u53d1\u751f\u5728\u8fd9\u91cc\uff0c\u6240\u4ee5\u5e76\u53d1\u63d2\u5165\u53ef\u80fd\u5bfc\u81f4\u95f4\u9699\u9501\u7ade\u4e89\uff1b\u53e6\u4e00\u4e2a\u70ed\u70b9\u53ef\u80fd\u662f AUTO_INCREMENT \u9501\u673a\u5236\u3002<\/p>\n<h3>\u7d22\u5f15\u8986\u76d6\u3001\u4f7f\u7528\u7d22\u5f15\u626b\u63cf\u505a\u6392\u5e8f<\/h3>\n<p>\u7d22\u5f15\u8986\u76d6\u662f\u6307\uff1a\u4e00\u4e2a\u7d22\u5f15\u5305\u542b\u6240\u6709\u9700\u8981\u67e5\u8be2\u7684\u5b57\u6bb5\u7684\u503c\u3002<\/p>\n<p>\u5982\u679c\u7d22\u5f15\u4e0d\u80fd\u8986\u76d6\u67e5\u8be2\u6240\u9700\u7684\u5168\u90e8\u5217\uff0c\u90a3\u5c31\u4e0d\u5f97\u4e0d\u6bcf\u626b\u63cf\u4e00\u6761\u7d22\u5f15\u8bb0\u5f55\u5c31\u56de\u8868\u67e5\u8be2\u4e00\u6b21\u5bf9\u5e94\u7684\u884c\u3002<\/p>\n<p>\u53ea\u6709\u5f53\u7d22\u5f15\u5217\u7684\u987a\u5e8f\u4e0e order by \u5b50\u53e5\u7684\u987a\u5e8f\u5b8c\u5168\u4e00\u81f4\uff0c\u5e76\u4e14\u6240\u6709\u5217\u7684\u6392\u5e8f\u65b9\u5411\uff08\u53cd\u5e8f\u6216\u6b63\u5e8f\uff09\u90fd\u4e00\u6837\u65f6\uff0cMySQL \u624d\u80fd\u591f\u4f7f\u7528\u7d22\u5f15\u6765\u5bf9\u7ed3\u679c\u505a\u6392\u5e8f\u3002<\/p>\n<h3>\u7d22\u5f15\u548c\u9501<\/h3>\n<p>\u7d22\u5f15\u53ef\u4ee5\u8ba9\u67e5\u8be2\u9501\u5b9a\u66f4\u5c11\u7684\u884c\u3002 InnoDB \u53ea\u6709\u5728\u8bbf\u95ee\u884c\u7684\u65f6\u5019\u624d\u4f1a\u5bf9\u5176\u52a0\u9501\uff0c\u800c\u7d22\u5f15\u80fd\u591f\u51cf\u5c11 InnoDB \u8bbf\u95ee\u7684\u884c\u6570\uff0c\u4ece\u800c\u51cf\u5c11\u9501\u7684\u6570\u91cf\uff08\u53ea\u6709\u5f53 InnoDB \u5728\u5b58\u50a8\u5f15\u64ce\u5c42\u80fd\u591f\u8fc7\u6ee4\u6389\u6240\u6709\u4e0d\u9700\u8981\u7684\u884c\u65f6\u624d\u6709\u6548\uff09\u3002<\/p>\n<p>\u5c3d\u53ef\u80fd\u5c06\u9700\u8981\u505a\u8303\u56f4\u67e5\u8be2\u7684\u5217\u653e\u5230\u7d22\u5f15\u7684\u540e\u9762\uff0c\u4ee5\u4fbf\u4f18\u5316\u5668\u80fd\u4f7f\u7528\u5c3d\u53ef\u80fd\u591a\u7684\u7d22\u5f15\u5217\u3002<\/p>\n<p>\u5ef6\u8fdf\u5173\u8054\uff1a\u901a\u8fc7\u4f7f\u7528\u8986\u76d6\u7d22\u5f15\u67e5\u8be2\u8fd4\u56de\u9700\u8981\u7684\u4e3b\u952e\uff0c\u518d\u6839\u636e\u8fd9\u4e9b\u4e3b\u952e\u5173\u8054\u539f\u8868\u83b7\u5f97\u9700\u8981\u7684\u884c\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7d22\u5f15\u57fa\u7840 \u7d22\u5f15\u7684\u4f7f\u7528\uff1a\u73b0\u5728\u7d22\u5f15\u4e2d\u627e\u5230\u5bf9\u5e94\u503c\uff0c\u7136\u540e\u6839\u636e\u5339\u914d\u7684\u7d22\u5f15\u8bb0\u5f55\u627e\u5230\u5bf9\u5e94\u7684\u6570\u636e &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/db\/20151207\/1339\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[149,215],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1339"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1339"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1339\/revisions"}],"predecessor-version":[{"id":1346,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1339\/revisions\/1346"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}