{"id":2675,"date":"2023-08-31T00:07:04","date_gmt":"2023-08-30T16:07:04","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2675"},"modified":"2023-08-31T00:07:04","modified_gmt":"2023-08-30T16:07:04","slug":"2675","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2675.html","title":{"rendered":"mysql\u7d22\u5f15\u5206\u7c7b\u4ee5\u53ca\u521b\u5efa\u4f7f\u7528"},"content":{"rendered":"
MySQL\u7d22\u5f15\u7684\u5efa\u7acb\u5bf9\u4e8eMySQL\u7684\u9ad8\u6548\u8fd0\u884c\u662f\u5f88\u91cd\u8981\u7684\uff0c\u7d22\u5f15\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8MySQL\u7684\u68c0\u7d22\u901f\u5ea6\u3002<\/p>\n
\u6253\u4e2a\u6bd4\u65b9\uff0c\u5982\u679c\u5408\u7406\u7684\u8bbe\u8ba1\u4e14\u4f7f\u7528\u7d22\u5f15\u7684MySQL\u662f\u4e00\u8f86\u5170\u535a\u57fa\u5c3c\u7684\u8bdd\uff0c\u90a3\u4e48\u6ca1\u6709\u8bbe\u8ba1\u548c\u4f7f\u7528\u7d22\u5f15\u7684MySQL\u5c31\u662f\u4e00\u4e2a\u4eba\u529b\u4e09\u8f6e\u8f66\u3002<\/p>\n
\u7d22\u5f15\u5206\u5355\u5217\u7d22\u5f15\u548c\u7ec4\u5408\u7d22\u5f15\u3002\u5355\u5217\u7d22\u5f15\uff0c\u5373\u4e00\u4e2a\u7d22\u5f15\u53ea\u5305\u542b\u5355\u4e2a\u5217\uff0c\u4e00\u4e2a\u8868\u53ef\u4ee5\u6709\u591a\u4e2a\u5355\u5217\u7d22\u5f15\uff0c\u4f46\u8fd9\u4e0d\u662f\u7ec4\u5408\u7d22\u5f15\u3002\u7ec4\u5408\u7d22\u5f15\uff0c\u5373\u4e00\u4e2a\u7d22\u5f15\u5305\u542b\u591a\u4e2a\u5217\u3002<\/p>\n
\u521b\u5efa\u7d22\u5f15\u65f6\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u8be5\u7d22\u5f15\u662f\u5e94\u7528\u5728 SQL \u67e5\u8be2\u8bed\u53e5\u7684\u6761\u4ef6(\u4e00\u822c\u4f5c\u4e3a WHERE \u5b50\u53e5\u7684\u6761\u4ef6)\u3002<\/p>\n
\u5b9e\u9645\u4e0a\uff0c\u7d22\u5f15\u4e5f\u662f\u4e00\u5f20\u8868\uff0c\u8be5\u8868\u4fdd\u5b58\u4e86\u4e3b\u952e\u4e0e\u7d22\u5f15\u5b57\u6bb5\uff0c\u5e76\u6307\u5411\u5b9e\u4f53\u8868\u7684\u8bb0\u5f55\u3002<\/p>\n
\u4e0a\u9762\u90fd\u5728\u8bf4\u4f7f\u7528\u7d22\u5f15\u7684\u597d\u5904\uff0c\u4f46\u8fc7\u591a\u7684\u4f7f\u7528\u7d22\u5f15\u5c06\u4f1a\u9020\u6210\u6ee5\u7528\u3002\u56e0\u6b64\u7d22\u5f15\u4e5f\u4f1a\u6709\u5b83\u7684\u7f3a\u70b9\uff1a\u867d\u7136\u7d22\u5f15\u5927\u5927\u63d0\u9ad8\u4e86\u67e5\u8be2\u901f\u5ea6\uff0c\u540c\u65f6\u5374\u4f1a\u964d\u4f4e\u66f4\u65b0\u8868\u7684\u901f\u5ea6\uff0c\u5982\u5bf9\u8868\u8fdb\u884cINSERT\u3001UPDATE\u548cDELETE\u3002\u56e0\u4e3a\u66f4\u65b0\u8868\u65f6\uff0cMySQL\u4e0d\u4ec5\u8981\u4fdd\u5b58\u6570\u636e\uff0c\u8fd8\u8981\u4fdd\u5b58\u4e00\u4e0b\u7d22\u5f15\u6587\u4ef6\u3002<\/p>\n
\u5efa\u7acb\u7d22\u5f15\u4f1a\u5360\u7528\u78c1\u76d8\u7a7a\u95f4\u7684\u7d22\u5f15\u6587\u4ef6\u3002<\/p>\n
Mysql\u76ee\u524d\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u7d22\u5f15\u7c7b\u578b\uff1aFULLTEXT\uff0cHASH\uff0cBTREE\uff0cRTREE\u3002<\/p>\n
\u5373\u4e3a\u5168\u6587\u7d22\u5f15\uff0c\u76ee\u524d\u53ea\u6709MyISAM\u5f15\u64ce\u652f\u6301\u3002\u5176\u53ef\u4ee5\u5728CREATE TABLE \uff0cALTER TABLE \uff0cCREATE INDEX \u4f7f\u7528\uff0c\u4e0d\u8fc7\u76ee\u524d\u53ea\u6709 CHAR\u3001VARCHAR \uff0cTEXT \u5217\u4e0a\u53ef\u4ee5\u521b\u5efa\u5168\u6587\u7d22\u5f15\u3002<\/p>\n
\u5168\u6587\u7d22\u5f15\u5e76\u4e0d\u662f\u548cMyISAM\u4e00\u8d77\u8bde\u751f\u7684\uff0c\u5b83\u7684\u51fa\u73b0\u662f\u4e3a\u4e86\u89e3\u51b3WHERE name LIKE \u201c%word%"\u8fd9\u7c7b\u9488\u5bf9\u6587\u672c\u7684\u6a21\u7cca\u67e5\u8be2\u6548\u7387\u8f83\u4f4e\u7684\u95ee\u9898\u3002<\/p>\n
\u7531\u4e8eHASH\u7684\u552f\u4e00\uff08\u51e0\u4e4e100%\u7684\u552f\u4e00\uff09\u53ca\u7c7b\u4f3c\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\uff0c\u5f88\u9002\u5408\u4f5c\u4e3a\u7d22\u5f15\u3002<\/p>\n
HASH\u7d22\u5f15\u53ef\u4ee5\u4e00\u6b21\u5b9a\u4f4d\uff0c\u4e0d\u9700\u8981\u50cf\u6811\u5f62\u7d22\u5f15\u90a3\u6837\u9010\u5c42\u67e5\u627e,\u56e0\u6b64\u5177\u6709\u6781\u9ad8\u7684\u6548\u7387\u3002\u4f46\u662f\uff0c\u8fd9\u79cd\u9ad8\u6548\u662f\u6709\u6761\u4ef6\u7684\uff0c\u5373\u53ea\u5728\u201c=\u201d\u548c\u201cin\u201d\u6761\u4ef6\u4e0b\u9ad8\u6548\uff0c\u5bf9\u4e8e\u8303\u56f4\u67e5\u8be2\u3001\u6392\u5e8f\u53ca\u7ec4\u5408\u7d22\u5f15\u4ecd\u7136\u6548\u7387\u4e0d\u9ad8\u3002<\/p>\n
BTREE\u7d22\u5f15\u5c31\u662f\u4e00\u79cd\u5c06\u7d22\u5f15\u503c\u6309\u4e00\u5b9a\u7684\u7b97\u6cd5\uff0c\u5b58\u5165\u4e00\u4e2a\u6811\u5f62\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff08\u4e8c\u53c9\u6811\uff09\uff0c\u6bcf\u6b21\u67e5\u8be2\u90fd\u662f\u4ece\u6811\u7684\u5165\u53e3root\u5f00\u59cb\uff0c\u4f9d\u6b21\u904d\u5386node\uff0c\u83b7\u53d6leaf\u3002\u8fd9\u662fMySQL\u91cc\u9ed8\u8ba4\u548c\u6700\u5e38\u7528\u7684\u7d22\u5f15\u7c7b\u578b\u3002<\/p>\n
RTREE\u5728MySQL\u5f88\u5c11\u4f7f\u7528\uff0c\u4ec5\u652f\u6301geometry\u6570\u636e\u7c7b\u578b\uff0c\u652f\u6301\u8be5\u7c7b\u578b\u7684\u5b58\u50a8\u5f15\u64ce\u53ea\u6709MyISAM\u3001BDb\u3001InnoDb\u3001NDb\u3001Archive\u51e0\u79cd\u3002<\/p>\n
\u76f8\u5bf9\u4e8eBTREE\uff0cRTREE\u7684\u4f18\u52bf\u5728\u4e8e\u8303\u56f4\u67e5\u627e\u3002<\/p>\n
<\/p>\n
MySQL\u7d22\u5f15\u6309\u5b57\u6bb5\u7279\u6027\u5206\u7c7b\u53ef\u5206\u4e3a\uff1a\u4e3b\u952e\u7d22\u5f15(PRIMARY KEY)\u3001\u552f\u4e00\u7d22\u5f15(UNIQUE)\u3001\u666e\u901a\u7d22\u5f15(INDEX)\u3001\u5168\u6587\u7d22\u5f15(FULLTEXT)\u3002<\/p>\n
\u4e3b\u952e\u7d22\u5f15(PRIMARY KEY)
\u5efa\u7acb\u5728\u4e3b\u952e\u4e0a\u7684\u7d22\u5f15\u88ab\u79f0\u4e3a\u4e3b\u952e\u7d22\u5f15\uff0c\u4e00\u5f20\u6570\u636e\u8868\u53ea\u80fd\u6709\u4e00\u4e2a\u4e3b\u952e\u7d22\u5f15\uff0c\u7d22\u5f15\u5217\u503c\u4e0d\u5141\u8bb8\u6709\u7a7a\u503c\uff0c\u901a\u5e38\u5728\u521b\u5efa\u8868\u65f6\u4e00\u8d77\u521b\u5efa\u3002<\/p>\n<\/li>\n
\u552f\u4e00\u7d22\u5f15(UNIQUE)
\u5efa\u7acb\u5728UNIQUE\u5b57\u6bb5\u4e0a\u7684\u7d22\u5f15\u88ab\u79f0\u4e3a\u552f\u4e00\u7d22\u5f15\uff0c\u4e00\u5f20\u8868\u53ef\u4ee5\u6709\u591a\u4e2a\u552f\u4e00\u7d22\u5f15\uff0c\u7d22\u5f15\u5217\u503c\u5141\u8bb8\u4e3a\u7a7a\uff0c\u5217\u503c\u4e2d\u51fa\u73b0\u591a\u4e2a\u7a7a\u503c\u4e0d\u4f1a\u53d1\u751f\u91cd\u590d\u51b2\u7a81\u3002<\/p>\n<\/li>\n
\u666e\u901a\u7d22\u5f15(INDEX)
\u5efa\u7acb\u5728\u666e\u901a\u5b57\u6bb5\u4e0a\u7684\u7d22\u5f15\u88ab\u79f0\u4e3a\u666e\u901a\u7d22\u5f15\u3002<\/p>\n<\/li>\n
\u5168\u6587\u7d22\u5f15(FULLTEXT)
MyISAM \u5b58\u50a8\u5f15\u64ce\u652f\u6301Full-text\u7d22\u5f15\uff0c\u7528\u4e8e\u67e5\u627e\u6587\u672c\u4e2d\u7684\u5173\u952e\u8bcd\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u6bd4\u8f83\u662f\u5426\u76f8\u7b49\u3002Full-text\u7d22\u5f15\u4e00\u822c\u4f7f\u7528\u5012\u6392\u7d22\u5f15\u5b9e\u73b0\uff0c\u5b83\u8bb0\u5f55\u7740\u5173\u952e\u8bcd\u5230\u5176\u6240\u5728\u6587\u6863\u7684\u6620\u5c04\u3002<\/p>\n<\/li>\n<\/ol>\n
<\/p>\n
<\/p>\n
\u666e\u901a\u7d22\u5f15\uff1a\u4ec5\u52a0\u901f\u67e5\u8be2<\/p>\n<\/li>\n
\u552f\u4e00\u7d22\u5f15\uff1a\u52a0\u901f\u67e5\u8be2 + \u5217\u503c\u552f\u4e00\uff08\u53ef\u4ee5\u6709null\uff09<\/p>\n<\/li>\n
\u4e3b\u952e\u7d22\u5f15\uff1a\u52a0\u901f\u67e5\u8be2 + \u5217\u503c\u552f\u4e00\uff08\u4e0d\u53ef\u4ee5\u6709null\uff09+ \u8868\u4e2d\u53ea\u6709\u4e00\u4e2a<\/p>\n<\/li>\n
\u7ec4\u5408\u7d22\u5f15\uff1a\u591a\u5217\u503c\u7ec4\u6210\u4e00\u4e2a\u7d22\u5f15\uff0c\u4e13\u95e8\u7528\u4e8e\u7ec4\u5408\u641c\u7d22\uff0c\u5176\u6548\u7387\u5927\u4e8e\u7d22\u5f15\u5408\u5e76<\/p>\n<\/li>\n
\u5168\u6587\u7d22\u5f15\uff1a\u5bf9\u6587\u672c\u7684\u5185\u5bb9\u8fdb\u884c\u5206\u8bcd\uff0c\u8fdb\u884c\u641c\u7d22 <\/p>\n<\/li>\n<\/ul>\n
ps.<\/p>\n
\u7d22\u5f15\u5408\u5e76\uff0c\u4f7f\u7528\u591a\u4e2a\u5355\u5217\u7d22\u5f15\u7ec4\u5408\u641c\u7d22 <\/p>\n <\/p>\n <\/p>\n 1\u3001unique|fulltext|spatial\u4e3a\u53ef\u9009\u53c2\u6570\uff0c\u5206\u522b\u8868\u793a\u552f\u4e00\u7d22\u5f15\u3001\u5168\u6587\u7d22\u5f15\u548c\u7a7a\u95f4\u7d22\u5f15\uff1b<\/p>\n 2\u3001index\u548ckey\u4e3a\u540c\u4e49\u8bcd\uff0c\u4e24\u8005\u4f5c\u7528\u76f8\u540c\uff0c\u7528\u6765\u6307\u5b9a\u521b\u5efa\u7d22\u5f15<\/p>\n 3\u3001col_name\u4e3a\u9700\u8981\u521b\u5efa\u7d22\u5f15\u7684\u5b57\u6bb5\u5217\uff0c\u8be5\u5217\u5fc5\u987b\u4ece\u6570\u636e\u8868\u4e2d\u8be5\u5b9a\u4e49\u7684\u591a\u4e2a\u5217\u4e2d\u9009\u62e9\uff1b<\/p>\n 4\u3001index_name\u6307\u5b9a\u7d22\u5f15\u7684\u540d\u79f0\uff0c\u4e3a\u53ef\u9009\u53c2\u6570\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0cMYSQL\u9ed8\u8ba4col_name\u4e3a\u7d22\u5f15\u503c\uff1b<\/p>\n 5\u3001length\u4e3a\u53ef\u9009\u53c2\u6570\uff0c\u8868\u793a\u7d22\u5f15\u7684\u957f\u5ea6\uff0c\u53ea\u6709\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5b57\u6bb5\u624d\u80fd\u6307\u5b9a\u7d22\u5f15\u957f\u5ea6\uff1b<\/p>\n 6\u3001asc\u6216desc\u6307\u5b9a\u5347\u5e8f\u6216\u964d\u5e8f\u7684\u7d22\u5f15\u503c\u5b58\u50a8<\/p>\n <\/p>\n \u4e3b\u952e\u7d22\u5f15\uff1a<\/p>\n <\/p>\n \u552f\u4e00\u7d22\u5f15\uff1a<\/p>\n \u7d22\u5f15\u5217\u7684\u503c\u5fc5\u987b\u552f\u4e00\uff0c\u4f46\u5141\u8bb8\u6709\u7a7a\u503c\u3002\u5982\u679c\u662f\u7ec4\u5408\u7d22\u5f15\uff0c\u5219\u5217\u503c\u7684\u7ec4\u5408\u5fc5\u987b\u552f\u4e00<\/p>\n<\/li>\n<\/ul>\n <\/p>\n \u666e\u901a\u7d22\u5f15-\u5355\u5217\u7d22\u5f15\uff1a<\/strong><\/p>\n \u5373\u7d22\u5f15\u521b\u5efa\u5728\u5355\u4e2a\u5217\u4e0a<\/p>\n<\/li>\n<\/ul>\n <\/p>\n <\/p>\n <\/p>\n \u666e\u901a\u7d22\u5f15-\u591a\u5217\u7d22\u5f15\uff1a<\/p>\n \u521b\u5efa\u591a\u5217\u7d22\u5f15\u65f6\u8bb0\u5f97\u9075\u5faa\u6700\u5de6\u5339\u914d\u539f\u5219\uff0c\u628a\u533a\u5206\u5ea6\u8f83\u9ad8\u7684\u5b57\u6bb5\u653e\u524d\u9762<\/p>\n<\/li>\n<\/ul>\n <\/p>\n <\/p>\n \u6848\u4f8b\uff1a<\/p>\n
<\/em>\u8986\u76d6\u7d22\u5f15\uff0cselect\u7684\u6570\u636e\u5217\u53ea\u7528\u4ece\u7d22\u5f15\u4e2d\u5c31\u80fd\u591f\u53d6\u5f97\uff0c\u4e0d\u5fc5\u8bfb\u53d6\u6570\u636e\u884c\uff0c\u6362\u53e5\u8bdd\u8bf4\u67e5\u8be2\u5217\u8981\u88ab\u6240\u5efa\u7684\u7d22\u5f15\u8986\u76d6<\/p>\n<\/a>\u56db\u3001\u64cd\u4f5c\u7d22\u5f15<\/h3>\n
\/\/\u521b\u5efa\u7d22\u5f15\u8bed\u6cd5CREATE TABLE table_name[col_name data type]\r\n[unique|fulltext|spatial][index|key][index_name](col_name[length])[asc|desc]<\/pre>\n
# \u67e5\u770b\u521b\u5efa\u7684\u7d22\u5f15\u5173\u952e\u8bcdPRIMARY KEY (`id`)<\/pre>\n
# \u521b\u5efa\u552f\u4e00\u7d22\u5f15mysql> alter table t1 add unique idx_u1(`u1`); \r\n# \u67e5\u770b\u521b\u5efa\u7684\u7d22\u5f15\u5173\u952e\u8bcdUNIQUE KEY `idx_u1` (`u1`)<\/pre>\n
# \u521b\u5efa\u5355\u5217\u7d22\u5f15mysql> alter table t1 add index idx_u1(`u1`); \r\n# \u67e5\u770b\u521b\u5efa\u7684\u7d22\u5f15\u5173\u952e\u8bcdKEY `idx_u1` (`u1`)<\/pre>\n
# \u521b\u5efa\u591a\u5217\u7d22\u5f15mysql> alter table t1 add index idx_u1_u2(`u1`,`u2`); \r\n# \u67e5\u770b\u521b\u5efa\u7684\u591a\u5217\u7d22\u5f15\u5173\u952e\u8bcdKEY `idx_u1_u2` (`u1`,`u2`)<\/pre>\n