{"id":441732,"date":"2025-05-28T10:05:07","date_gmt":"2025-05-28T02:05:07","guid":{"rendered":"https:\/\/www.idc.net\/help\/?p=441732"},"modified":"2025-05-28T10:05:07","modified_gmt":"2025-05-28T02:05:07","slug":"mysql%e8%81%94%e5%90%88%e7%b4%a2%e5%bc%95%e8%af%a6%e8%a7%a3%e5%8f%8a%e4%bd%bf%e7%94%a8%e4%bc%98%e5%8c%96%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/441732\/","title":{"rendered":"MySQL\u8054\u5408\u7d22\u5f15\u8be6\u89e3\u53ca\u4f7f\u7528\u4f18\u5316\u5b9e\u8df5"},"content":{"rendered":"<p>&nbsp;<\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>\u5728\u6570\u636e\u5e93\u8c03\u4f18\u8fc7\u7a0b\u4e2d\uff0c\u5408\u7406\u8bbe\u8ba1\u5e76\u4f7f\u7528\u7d22\u5f15\u5bf9\u63d0\u5347\u67e5\u8be2\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002MySQL\u4e2d\u652f\u6301\u591a\u79cd\u7d22\u5f15\u7c7b\u578b\uff0c\u5305\u62ec\u666e\u901a\u7d22\u5f15\u3001\u552f\u4e00\u7d22\u5f15\u3001\u4e3b\u952e\u7d22\u5f15\u3001\u5168\u6587\u7d22\u5f15\u548c\u8054\u5408\u7d22\u5f15\u7b49\u3002\u672c\u6587\u5c06\u91cd\u70b9\u89e3\u6790\u8054\u5408\u7d22\u5f15\u7684\u539f\u7406\u4e0e\u5b9e\u6218\u7528\u6cd5\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u9ad8\u6548\u7ba1\u7406\u6570\u636e\u67e5\u8be2\uff0c\u63d0\u5347\u7cfb\u7edf\u54cd\u5e94\u901f\u5ea6\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u4f9d\u8d56\u9ad8\u6027\u80fd\u6570\u636e\u5e93\u670d\u52a1\u7684\u9999\u6e2fvps\u548c\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u5e94\u7528\u573a\u666f\u3002<\/p>\n<h2>\u5e38\u89c1\u7d22\u5f15\u7c7b\u578b\u6982\u89c8<\/h2>\n<p>\u5728\u6df1\u5165\u8054\u5408\u7d22\u5f15\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u6765\u4e86\u89e3\u4e00\u4e0bMySQL\u4e2d\u7684\u51e0\u79cd\u5e38\u89c1\u7d22\u5f15\uff1a<\/p>\n<ul>\n<li><strong>\u666e\u901a\u7d22\u5f15\uff08INDEX\uff09<\/strong>\uff1a\u6700\u57fa\u7840\u7684\u7d22\u5f15\u7c7b\u578b\uff0c\u65e0\u552f\u4e00\u6027\u9650\u5236\u3002<\/li>\n<li><strong>\u552f\u4e00\u7d22\u5f15\uff08UNIQUE\uff09<\/strong>\uff1a\u4e0e\u666e\u901a\u7d22\u5f15\u7c7b\u4f3c\uff0c\u4f46\u8981\u6c42\u5217\u503c\u5fc5\u987b\u552f\u4e00\uff0c\u53ef\u5305\u542bNULL\u3002<\/li>\n<li><strong>\u4e3b\u952e\u7d22\u5f15\uff08PRIMARY KEY\uff09<\/strong>\uff1a\u4e00\u79cd\u7279\u6b8a\u7684\u552f\u4e00\u7d22\u5f15\uff0c\u5217\u503c\u4e0d\u80fd\u4e3aNULL\uff0c\u4e14\u8868\u4e2d\u53ea\u80fd\u6709\u4e00\u4e2a\u4e3b\u952e\u3002<\/li>\n<li><strong>\u5168\u6587\u7d22\u5f15\uff08FULLTEXT\uff09<\/strong>\uff1a\u4e3b\u8981\u7528\u4e8e\u6587\u672c\u5185\u5bb9\u7684\u5173\u952e\u8bcd\u68c0\u7d22\uff0c\u9002\u7528\u4e8eMyISAM\u548c5.6\u4ee5\u4e0a\u7684InnoDB\u8868\u3002<\/li>\n<li><strong>\u8054\u5408\u7d22\u5f15\uff08Composite Index\uff09<\/strong>\uff1a\u7531\u591a\u4e2a\u5217\u7ec4\u6210\u7684\u590d\u5408\u7d22\u5f15\uff0c\u9002\u7528\u4e8e\u591a\u6761\u4ef6\u67e5\u8be2\u573a\u666f\u3002<\/li>\n<\/ul>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u8054\u5408\u7d22\u5f15\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u590d\u6742\u67e5\u8be2\u6761\u4ef6\u4e0b\u7684\u6570\u636e\u63d0\u53d6\uff0c\u7279\u522b\u662f\u5728\u90e8\u7f72\u4e8e<strong>\u9999\u6e2f\u72ec\u7acb\u670d\u52a1\u5668<\/strong>\u7684\u4e1a\u52a1\u7cfb\u7edf\u4e2d\uff0c\u5176\u6027\u80fd\u8868\u73b0\u5c24\u4e3a\u5173\u952e\u3002<\/p>\n<h2>\u8054\u5408\u7d22\u5f15\u7684\u7ed3\u6784\u4e0e\u539f\u7406<\/h2>\n<p>\u8054\u5408\u7d22\u5f15\u662f\u57fa\u4e8e\u591a\u4e2a\u5217\u7ec4\u5408\u5efa\u7acb\u7684\u7d22\u5f15\uff0c\u5176\u5728\u67e5\u8be2\u6548\u7387\u4e0a\u5f80\u5f80\u4f18\u4e8e\u591a\u4e2a\u5355\u5217\u7d22\u5f15\u7684\u7ec4\u5408\u4f7f\u7528\u3002MySQL\u5728\u89e3\u6790\u67e5\u8be2\u8bed\u53e5\u65f6\uff0c\u4f1a\u5c1d\u8bd5\u4f7f\u7528\u7b26\u5408\u201c\u6700\u5de6\u524d\u7f00\u201d\u539f\u5219\u7684\u5b57\u6bb5\u8fdb\u884c\u7d22\u5f15\u5339\u914d\u3002\u8fd9\u610f\u5473\u7740\u7d22\u5f15\u7684\u5339\u914d\u4ece\u6700\u5de6\u8fb9\u5f00\u59cb\uff0c\u76f4\u5230\u9047\u5230\u4e0d\u5339\u914d\u7684\u5b57\u6bb5\u4e3a\u6b62\u3002<\/p>\n<h3>\u521b\u5efa\u8054\u5408\u7d22\u5f15\u7684\u793a\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u8868\u7ed3\u6784\u4e0e\u8054\u5408\u7d22\u5f15\u521b\u5efa\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE TABLE t (\r\n  c1 CHAR(1) NOT NULL,\r\n  c2 CHAR(1) NOT NULL,\r\n  c3 CHAR(1) NOT NULL,\r\n  c4 CHAR(1) NOT NULL,\r\n  c5 CHAR(1) NOT NULL\r\n) ENGINE=MyISAM DEFAULT CHARSET=utf8;\r\n\r\nALTER TABLE t ADD INDEX idx_c1234 (c1, c2, c3, c4);\r\n<\/code><\/pre>\n<p>\u63d2\u5165\u6d4b\u8bd5\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-sql\">INSERT INTO t VALUES\r\n('1','1','1','1','1'),\r\n('2','2','2','2','2'),\r\n('3','3','3','3','3'),\r\n('4','4','4','4','4'),\r\n('5','5','5','5','5');\r\n<\/code><\/pre>\n<h3>\u8054\u5408\u7d22\u5f15\u7684\u67e5\u8be2\u5b9e\u8df5<\/h3>\n<ol>\n<li><strong>\u5b8c\u5168\u5339\u914d\u987a\u5e8f\u6216\u4e71\u5e8f\u7686\u53ef\u751f\u6548\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-sql\">EXPLAIN SELECT * FROM t WHERE c1='1' AND c2='1' AND c3='1' AND c4='1';\r\nEXPLAIN SELECT * FROM t WHERE c3='1' AND c4='1' AND c1='1' AND c2='1';\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>\u672a\u4f7f\u7528\u6700\u5de6\u524d\u7f00\u5b57\u6bb5\uff0c\u4e0d\u89e6\u53d1\u7d22\u5f15\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-sql\">EXPLAIN SELECT * FROM t WHERE c2='1' AND c3='1' AND c4='1';\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li><strong>\u90e8\u5206\u6700\u5de6\u5b57\u6bb5\u5339\u914d\uff0c\u53ea\u80fd\u5229\u7528\u90e8\u5206\u7d22\u5f15\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"language-sql\">EXPLAIN SELECT * FROM t WHERE c1='1' AND c4='1';   -- \u4f7f\u7528c1\u7d22\u5f15\r\nEXPLAIN SELECT * FROM t WHERE c1='1' AND c2='1';   -- \u4f7f\u7528c1\u548cc2\u7d22\u5f15\r\n<\/code><\/pre>\n<ol start=\"4\">\n<li><strong>\u6a21\u7cca\u67e5\u8be2\u4e0e\u8303\u56f4\u67e5\u8be2\u7684\u5f71\u54cd\uff1a<\/strong><\/li>\n<\/ol>\n<ul>\n<li>\u6a21\u7cca\u5339\u914d\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-sql\">EXPLAIN SELECT * FROM t WHERE c1 LIKE '3%';\r\nEXPLAIN SELECT * FROM t WHERE c1 LIKE '3%' AND c2='1' AND c3='1' AND c4='1';\r\n<\/code><\/pre>\n<ul>\n<li>\u8303\u56f4\u67e5\u8be2\uff08between\uff09\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-sql\">EXPLAIN SELECT * FROM t WHERE c1 BETWEEN '1' AND '3' AND c2='1' AND c3='1' AND c4='1';\r\n<\/code><\/pre>\n<ul>\n<li>\u8303\u56f4\u67e5\u8be2\uff08&gt; &lt;\uff09\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-sql\">EXPLAIN SELECT * FROM t WHERE c1 &gt; '3' AND c2='1' AND c3='1' AND c4='1';\r\nEXPLAIN SELECT * FROM t WHERE c1='1' AND c2 &gt; '1' AND c3='1' AND c4='1';\r\n<\/code><\/pre>\n<h2>\u6700\u5de6\u524d\u7f00\u539f\u5219\u8981\u70b9\u603b\u7ed3<\/h2>\n<ul>\n<li>\u8054\u5408\u7d22\u5f15\u53ea\u80fd\u4ece\u5de6\u5230\u53f3\u4f9d\u6b21\u4f7f\u7528\u5b57\u6bb5\u8fdb\u884c\u5339\u914d\u3002<\/li>\n<li>\u4e00\u65e6\u9047\u5230\u8303\u56f4\u67e5\u8be2\u6216\u6a21\u7cca\u5339\u914d\uff0c\u540e\u7eed\u5b57\u6bb5\u4e0d\u518d\u53c2\u4e0e\u7d22\u5f15\u3002<\/li>\n<li>\u67e5\u8be2\u5b57\u6bb5\u7684\u987a\u5e8f\u53ef\u6253\u4e71\uff0c\u4f46\u5fc5\u987b\u5305\u542b\u6700\u5de6\u524d\u7f00\u8d77\u59cb\u5b57\u6bb5\u3002<\/li>\n<\/ul>\n<p>\u5728\u5b9e\u9645\u90e8\u7f72\u7f51\u7ad9\u6216\u5e94\u7528\u7cfb\u7edf\u81f3\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u65f6\uff0c\u7ed3\u5408\u5408\u9002\u7684\u7d22\u5f15\u7b56\u7565\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u6570\u636e\u5e93\u6027\u80fd\uff0c\u907f\u514d\u56e0\u5168\u8868\u626b\u63cf\u5bfc\u81f4\u7684\u9ad8\u8d1f\u8f7d\u95ee\u9898\u3002<\/p>\n<h2>\u5e94\u7528\u5efa\u8bae\u4e0e\u7ed3\u8bed<\/h2>\n<p>\u4e3a\u4e86\u4fdd\u969c\u9ad8\u5e76\u53d1\u4e0b\u7684\u6570\u636e\u8bbf\u95ee\u6548\u7387\uff0c\u5efa\u8bae\u5728\u4e1a\u52a1\u7cfb\u7edf\u8bbe\u8ba1\u521d\u671f\u5c31\u8003\u8651\u7d22\u5f15\u7b56\u7565\u7684\u89c4\u5212\uff0c\u5c24\u5176\u662f\u9488\u5bf9\u90e8\u7f72\u5728<a href=\"https:\/\/idc.net\/\"><strong>\u9999\u6e2fvps<\/strong><\/a>\u6216<strong>\u9999\u6e2f\u72ec\u7acb\u670d\u52a1\u5668<\/strong>\u4e0a\u7684\u670d\u52a1\u3002\u901a\u8fc7\u79d1\u5b66\u4f7f\u7528\u8054\u5408\u7d22\u5f15\uff0c\u53ef\u4ee5\u6709\u6548\u964d\u4f4e\u67e5\u8be2\u5ef6\u8fdf\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u524d\u8a00 \u5728\u6570\u636e\u5e93\u8c03\u4f18\u8fc7\u7a0b\u4e2d\uff0c\u5408\u7406\u8bbe\u8ba1\u5e76\u4f7f\u7528\u7d22\u5f15\u5bf9\u63d0\u5347\u67e5\u8be2\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002MySQL\u4e2d\u652f\u6301\u591a\u79cd\u7d22\u5f15\u7c7b\u578b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7038],"tags":[],"class_list":["post-441732","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/441732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=441732"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/441732\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=441732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=441732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=441732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}