{"id":6312,"date":"2024-11-14T08:04:42","date_gmt":"2024-11-14T00:04:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6312\/"},"modified":"2024-11-14T08:04:42","modified_gmt":"2024-11-14T00:04:42","slug":"mysql%e7%b4%a2%e5%bc%95%e6%80%8e%e4%b9%88%e5%bb%ba%e7%ab%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6312\/","title":{"rendered":"mysql\u7d22\u5f15\u600e\u4e48\u5efa\u7acb"},"content":{"rendered":"<blockquote><p>\n  \u5efa\u7acb mysql \u7d22\u5f15\u7684\u6b65\u9aa4\uff1a\u786e\u5b9a\u8981\u7d22\u5f15\u7684\u5217\u3002\u9009\u62e9\u7d22\u5f15\u7c7b\u578b\uff1ab-tree \u7d22\u5f15\u3001\u54c8\u5e0c\u7d22\u5f15\u3001\u5168\u6587\u672c\u7d22\u5f15\u3002\u521b\u5efa\u7d22\u5f15\uff1acreate index index_name on table_name (column_name);\u4f7f\u7528\u4f18\u5316\u5668\u81ea\u52a8\u9009\u62e9\u7d22\u5f15\u3002\u7ef4\u62a4\u7d22\u5f15\uff1amysql \u4f1a\u81ea\u52a8\u66f4\u65b0\u7d22\u5f15\uff0c\u6216\u53ef\u4f7f\u7528 optimize table \u624b\u52a8\u4f18\u5316\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202407\/31\/2024073122195064683.jpg\" class=\"aligncenter\" title=\"mysql\u7d22\u5f15\u600e\u4e48\u5efa\u7acb\u63d2\u56fe\" alt=\"mysql\u7d22\u5f15\u600e\u4e48\u5efa\u7acb\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5efa\u7acb MySQL \u7d22\u5f15<\/strong><\/p>\n<p>\u5728 MySQL \u6570\u636e\u5e93\u4e2d\uff0c\u7d22\u5f15\u662f\u7528\u4e8e\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u7684\u6570\u636e\u7ed3\u6784\u3002\u901a\u8fc7\u5efa\u7acb\u7d22\u5f15\uff0c\u53ef\u4ee5\u5feb\u901f\u67e5\u627e\u7279\u5b9a\u6570\u636e\uff0c\u800c\u65e0\u9700\u626b\u63cf\u6574\u4e2a\u8868\u3002\u4e0b\u9762\u662f\u5efa\u7acb MySQL \u7d22\u5f15\u7684\u6b65\u9aa4\uff1a<\/p>\n<p><strong>1. \u786e\u5b9a\u8981\u7d22\u5f15\u7684\u5217<\/strong><\/p>\n<p>\u9009\u62e9\u8981\u7d22\u5f15\u7684\u5217\u65f6\uff0c\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u56e0\u7d20\uff1a<\/p>\n<ul>\n<li>\u7ecf\u5e38\u7528\u4e8e WHERE \u5b50\u53e5\u7684\u5217<\/li>\n<li>\u6570\u636e\u5206\u5e03\u4e0d\u5747\u5300\u7684\u5217<\/li>\n<li>\u7ecf\u5e38\u7528\u4e8e\u8fde\u63a5\u7684\u5217<\/li>\n<\/ul>\n<p><strong>2. \u9009\u62e9\u7d22\u5f15\u7c7b\u578b<\/strong><\/p>\n<p>MySQL \u652f\u6301\u591a\u79cd\u7d22\u5f15\u7c7b\u578b\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> <strong>B-Tree \u7d22\u5f15\uff1a<\/strong>\u6700\u5e38\u7528\u7684\u7d22\u5f15\u7c7b\u578b\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u3002<\/li>\n<li> <strong>\u54c8\u5e0c\u7d22\u5f15\uff1a<\/strong>\u9488\u5bf9\u76f8\u7b49\u6027\u67e5\u8be2\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u4f46\u4e0d\u80fd\u7528\u4e8e\u8303\u56f4\u67e5\u8be2\u3002<\/li>\n<li> <strong>\u5168\u6587\u672c\u7d22\u5f15\uff1a<\/strong>\u7528\u4e8e\u5728\u6587\u672c\u5b57\u6bb5\u4e2d\u8fdb\u884c\u5168\u6587\u641c\u7d22\u3002<\/li>\n<\/ul>\n<p><strong>3. \u521b\u5efa\u7d22\u5f15<\/strong><\/p>\n<p>\u4f7f\u7528 CREATE INDEX \u8bed\u53e5\u521b\u5efa\u7d22\u5f15\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>CREATE INDEX index_name ON table_name (column_name);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u8981\u4e3a users \u8868\u7684 username \u5217\u521b\u5efa B-Tree \u7d22\u5f15\uff1a<\/p>\n<pre>CREATE INDEX idx_username ON users (username);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u4f18\u5316\u5668<\/strong><\/p>\n<p>MySQL \u6709\u4e00\u4e2a\u4f18\u5316\u5668\uff0c\u53ef\u4ee5\u81ea\u52a8\u9009\u62e9\u8981\u4f7f\u7528\u7684\u7d22\u5f15\u3002\u5982\u679c MySQL \u65e0\u6cd5\u4e3a\u67e5\u8be2\u9009\u62e9\u5408\u9002\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u624b\u52a8\u521b\u5efa\u7d22\u5f15\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p><strong>5. \u7ef4\u62a4\u7d22\u5f15<\/strong><\/p>\n<p>\u968f\u7740\u6570\u636e\u7684\u66f4\u65b0\uff0c\u7d22\u5f15\u9700\u8981\u8fdb\u884c\u7ef4\u62a4\u3002MySQL \u4f1a\u81ea\u52a8\u66f4\u65b0\u7d22\u5f15\uff0c\u4f46\u4e5f\u53ef\u4ee5\u4f7f\u7528 OPTIMIZE TABLE \u8bed\u53e5\u624b\u52a8\u4f18\u5316\u7d22\u5f15\u3002<\/p>\n<p><strong>\u4f18\u5316\u7d22\u5f15\u7684\u63d0\u793a<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u5305\u542b\u552f\u4e00\u503c\u7684\u7d22\u5f15\uff0c\u4ee5\u907f\u514d\u7d22\u5f15\u81a8\u80c0\u3002<\/li>\n<li>\u907f\u514d\u521b\u5efa\u8fc7\u591a\u7684\u7d22\u5f15\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u589e\u52a0\u7ef4\u62a4\u7d22\u5f15\u7684\u5f00\u9500\u3002<\/li>\n<li>\u6839\u636e\u67e5\u8be2\u6a21\u5f0f\u5b9a\u671f\u68c0\u67e5\u548c\u8c03\u6574\u7d22\u5f15\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u7d22\u5f15\u600e\u4e48\u5efa\u7acb\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5efa\u7acb mysql \u7d22\u5f15\u7684\u6b65\u9aa4\uff1a\u786e\u5b9a\u8981\u7d22\u5f15\u7684\u5217\u3002\u9009\u62e9\u7d22\u5f15\u7c7b\u578b\uff1ab-tree \u7d22\u5f15\u3001\u54c8\u5e0c\u7d22\u5f15\u3001\u5168\u6587\u672c\u7d22\u5f15\u3002\u521b\u5efa\u7d22\u5f15\uff1acreate index index_name on table_name (column_name);\u4f7f\u7528\u4f18\u5316\u5668\u81ea\u52a8\u9009\u62e9\u7d22\u5f15\u3002\u7ef4\u62a4\u7d22\u5f15\uff1amysql \u4f1a\u81ea\u52a8\u66f4\u65b0\u7d22\u5f15\uff0c\u6216\u53ef\u4f7f\u7528 optimize table \u624b\u52a8\u4f18\u5316\u3002 \u5982\u4f55\u5efa\u7acb MySQL \u7d22\u5f15 \u5728 MySQL \u6570\u636e\u5e93\u4e2d\uff0c\u7d22\u5f15\u662f\u7528\u4e8e\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\u7684\u6570\u636e\u7ed3\u6784\u3002\u901a\u8fc7\u5efa\u7acb\u7d22\u5f15\uff0c\u53ef\u4ee5\u5feb\u901f\u67e5\u627e\u7279\u5b9a\u6570\u636e\uff0c\u800c\u65e0\u9700\u626b\u63cf\u6574\u4e2a\u8868\u3002\u4e0b\u9762\u662f\u5efa\u7acb MySQL \u7d22\u5f15\u7684\u6b65\u9aa4\uff1a 1. \u786e\u5b9a\u8981\u7d22\u5f15\u7684\u5217 \u9009\u62e9\u8981\u7d22\u5f15\u7684\u5217\u65f6\uff0c\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u56e0\u7d20\uff1a \u7ecf\u5e38\u7528\u4e8e WHERE \u5b50\u53e5\u7684\u5217 \u6570\u636e\u5206\u5e03\u4e0d\u5747\u5300\u7684\u5217 \u7ecf\u5e38\u7528\u4e8e\u8fde\u63a5\u7684\u5217 2. \u9009\u62e9\u7d22\u5f15\u7c7b\u578b MySQL \u652f\u6301\u591a\u79cd\u7d22\u5f15\u7c7b\u578b\uff0c\u5305\u62ec\uff1a B-Tree \u7d22\u5f15\uff1a\u6700\u5e38\u7528\u7684\u7d22\u5f15\u7c7b\u578b\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\u3002 \u54c8\u5e0c\u7d22\u5f15\uff1a\u9488\u5bf9\u76f8\u7b49\u6027\u67e5\u8be2\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u4f46\u4e0d\u80fd\u7528\u4e8e\u8303\u56f4\u67e5\u8be2\u3002 \u5168\u6587\u672c\u7d22\u5f15\uff1a\u7528\u4e8e\u5728\u6587\u672c\u5b57\u6bb5\u4e2d\u8fdb\u884c\u5168\u6587\u641c\u7d22\u3002 3. \u521b\u5efa\u7d22\u5f15 \u4f7f\u7528 CREATE INDEX \u8bed\u53e5\u521b\u5efa\u7d22\u5f15\uff0c\u5982\u4e0b\u6240\u793a\uff1a CREATE INDEX index_name ON table_name (column_name); \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff0c\u8981\u4e3a users \u8868\u7684 username \u5217\u521b\u5efa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6312","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6312"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6312\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}