{"id":3967,"date":"2012-11-20T23:50:43","date_gmt":"2012-11-20T15:50:43","guid":{"rendered":"http:\/\/www.orczhou.com\/?p=3967"},"modified":"2012-11-20T23:53:06","modified_gmt":"2012-11-20T15:53:06","slug":"mysql-source-code-data-structure-about-index","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2012\/11\/mysql-source-code-data-structure-about-index\/","title":{"rendered":"MySQL\u6e90\u7801\uff1a\u7d22\u5f15\u76f8\u5173\u7684\u6570\u636e\u7ed3\u6784(\u524d\u7bc7)"},"content":{"rendered":"<style>\n#content h3{\n  color:red;\n  font-size:14px;\n}\n#content h4{\n  color:blue;\n  font-size:14px;\n}\n#content blockquote{\n  font-family:\"Courier New\",Courier,monospace;\n  white-space:pre;\n}\n<\/style>\n<p>\u5f88\u67af\u71e5\u7684\uff0c\u914d\u9996\u80cc\u666f\u97f3\u4e50\u5427\uff1a<\/p>\n<p><embed src=\"http:\/\/www.xiami.com\/widget\/0_2127770\/singlePlayer.swf\" type=\"application\/x-shockwave-flash\" width=\"257\" height=\"33\" wmode=\"transparent\"><\/embed><\/p>\n<p>\u672c\u6587\u5c06\u5c1d\u8bd5\u4ecb\u7ecdMySQL\u7d22\u5f15\u5b58\u50a8\u76f8\u5173\u7684\u6570\u636e\u7ed3\u6784\u3002\u7a0b\u5e8f=\u6570\u636e\u7ed3\u6784+\u7b97\u6cd5\uff0c\u4e86\u89e3\u6570\u636e\u7ed3\u6784\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4e86\u89e3MySQL\u6e90\u7801\u4e2d\u5982\u4f55\u4f7f\u7528\u7d22\u5f15\uff0c\u5982\u4f55\u9009\u62e9\u81ea\u5df1\u7684\u6267\u884c\u8ba1\u5212\u3002<\/p>\n\n<h3>1. MySQL\u5982\u4f55\u63cf\u8ff0\u67d0\u4e2a\u6570\u636e\u8868\u7684\u7d22\u5f15<\/h3>\n<p>MySQL\u4f7f\u7528TABLE\u5bf9\u8c61\u6765\u63cf\u8ff0\u4e00\u4e2a\u6570\u636e\u8868\uff0c\u90a3\u4e48\u6570\u636e\u8868\u7684\u7d22\u5f15\u662f\u5982\u4f55\u63cf\u8ff0\uff0c\u7d22\u5f15\u7684\u7edf\u8ba1\u4fe1\u606f\u53c8\u662f\u5982\u4f55\u5b58\u50a8\u7684\u5462? \u4f8b\u5982\u6211\u4eec\u6709\u5982\u4e0b\u6570\u636e\u8868\uff1a<\/p>\n<pre><blockquote>CREATE TABLE `users` (\r\n  `id` int(11) NOT NULL,\r\n  `nick` varchar(32) DEFAULT NULL,\r\n  `reg_date` datetime DEFAULT NULL,\r\n  PRIMARY KEY (`id`),\r\n  KEY `IND_NICK` (`nick`),\r\n  KEY `IND_REGDATE` (`reg_date`)\r\n) <\/blockquote><\/pre>\n<p>\u8be5\u8868\u6709\u7d22\u5f15\uff0cPRIMARY KEY\u3001IND_NICK\u3001IND_REGDATE\uff0c\u6211\u4eec\u6765\u770b\u770bMySQL\u5185\u90e8\u662f\u5982\u4f55\u5b58\u50a8\u8fd9\u4e09\u4e2a\u7d22\u5f15\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u7d22\u5f15\u7684\u7edf\u8ba1\u4fe1\u606f\u7684\u3002\u4e0b\u56fe\uff0c\u63cf\u8ff0\u4e86\u5b58\u50a8\u4e00\u4e2a\u6570\u636e\u8868\u7d22\u5f15\u7684\u4e3b\u8981\u7ed3\u6784\uff1a<\/p>\n<p><a href=\"http:\/\/www.flickr.com\/photos\/26825745@N06\/8197844509\/\" title=\"indexoftable-s by orczhou, on Flickr\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/farm9.staticflickr.com\/8069\/8197844509_a0ddd6540d_z.jpg\" width=\"600\" height=\"185\" alt=\"indexoftable-s\"><\/a><!--more--><!--more--><\/p>\n<li>MySQL\u4f7f\u7528TABLE\u5bf9\u8c61\u63cf\u8ff0\u4e00\u4e2a\u6570\u636e\u8868\uff0c\u4ed6\u7684\u6210\u5458key_info\uff08\u7c7b\u578b\u4e3aKEY\uff09\u63cf\u8ff0\u8fd9\u4e2a\u8868\u7684\u5168\u90e8\u7d22\u5f15<\/li>\n<li>key_info\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u6bcf\u4e00\u4e2a\u5143\u7d20\u662f\u4e00\u4e2aKEY(vim -t KEY\u67e5\u770b)\u5bf9\u8c61\uff0c\u4ee3\u8868\u4e86\u4e00\u4e2a\u7d22\u5f15\uff0c\u987a\u5e8f\u7684\uff0c\u6574\u4e2a\u6570\u7ec4\u4ee3\u8868\u4e86\u8fd9\u4e2a\u8868\u7684\u5168\u90e8\u7d22\u5f15<\/li>\n<li>key_info\uff08KEY\u5bf9\u8c61\uff09\u7684\u6210\u5458key_part\uff0c\u662f\u6307\u5411KEY_PART_INFO\u6570\u7ec4\u7684\u6307\u9488\uff0c\u8be5\u6570\u7ec4\u63cf\u8ff0\u4e86\u67d0\u4e2a\u7d22\u5f15\u6240\u6709\u7684\u7d22\u5f15\u5217\u4fe1\u606f\uff0c\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u4ee3\u8868\u4e86\u4e00\u4e2a\u7d22\u5f15\u5217<\/li>\n<li>\u6700\u540e\uff0ckey_part\u7684\u6210\u5458field\u6307\u5411\u8be5\u7d22\u5f15\u5217\u5bf9\u5e94\u7684\u6570\u636e\u8868\u5b57\u6bb5<\/li>\n<h3>2. GDB\u6253\u5370\u89c2\u5bdf\u7d22\u5f15\u4fe1\u606f<\/h3>\n<h4>2.1 \u6253\u5370\u7d22\u5f15\u57fa\u672c\u4fe1\u606f<\/h4>\n<p>\u67d0\u4e2a\u7d22\u5f15\uff08\u4f8b\u5982IND_NICK\uff09\u7684\u57fa\u672c\u4fe1\u606f\u662f\u5b58\u50a8\u5728KEY\u7ed3\u6784(table->key_info\u6570\u7ec4)\u4e2d\uff0c\u8fd9\u91cc\uff0c\u6211\u4eec\u6765\u6253\u5370\u7d22\u5f15IND_NICK\u7684\u57fa\u672c\u4fe1\u606f\u6765\u89c2\u5bdfKEY\u7ed3\u6784\uff1a<\/p>\n<pre><blockquote>(gdb) p s->table->key_info[1]\r\n$26 = {\r\n  key_length = 67, \r\n  flags = 104, \r\n  key_parts = 1, \r\n  extra_length = 3, \r\n  usable_key_parts = 1, \r\n  block_size = 1024, \r\n  algorithm = HA_KEY_ALG_UNDEF, \r\n  {\r\n    parser = 0x0, \r\n    parser_name = 0x0\r\n  }, \r\n  key_part = 0x7f6514020860, \r\n  name = 0x7f651401fc11 \"IND_NICK\", \r\n  rec_per_key = 0x7f651401fa68, \r\n  handler = {\r\n    bdb_return_if_eq = 0\r\n  }, \r\n  table = 0x7f6514023d30\r\n}<\/blockquote><\/pre>\n<p>key_length = 67\uff1a \u8868\u793a\u8fd9\u4e2a\u7d22\u5f15\u5165\u53e3\u957f\u4e3a67(\u4e0d\u5305\u62ecrowid),32*2 + 1(NULL) + VARCHAR\u53d8\u6210\u4fe1\u606f<\/p>\n<p>flags = 104  \u8fd9\u662f\u4e00\u4e2a\u7d22\u5f15\u4fe1\u606f\u7684\u6807\u5fd7\u4f4d\uff0c\u5b58\u50a8\u4e86\u8fd9\u4e2a\u7d22\u5f15\u7684\u4e00\u4e9b\u91cd\u8981\u5c5e\u6027\uff0c\u4f8b\u5982\u662f\u4e0d\u662f\u552f\u4e00\u7d22\u5f15\uff0c\u7d22\u5f15\u4e2d\u6709\u6ca1\u6709\u53d8\u957f\u5b57\u6bb5\uff0c\u662f\u4e0d\u662f\u4e00\u4e2a\u5168\u6587\u7d22\u5f15\u7b49\uff0c104\u8868\u793a\u4e3a\u4e8c\u8fdb\u5236\uff0c\u5373\u662f1101000\uff0c\u4e5f\u7b49\u4e8e8+32+64\uff0c\u4e5f\u5c31\u662fHA_VAR_LENGTH_KEY|HA_BINARY_PACK_KEY|HA_NULL_PART_KEY<\/p>\n<p>\u66f4\u591aflags\u7c7b\u578b\uff1a<\/p>\n<pre><blockquote>#define HA_NOSAME                1      \/* Set if not dupplicated records *\/\r\n#define HA_PACK_KEY              2      \/* Pack string key to previous key *\/\r\n#define HA_SPACE_PACK_USED       4      \/* Test for if SPACE_PACK used *\/\r\n#define HA_VAR_LENGTH_KEY        8\r\n#define HA_AUTO_KEY              16\r\n#define HA_BINARY_PACK_KEY       32     \/* Packing of all keys to prev key *\/\r\n#define HA_NULL_PART_KEY         64\r\n#define HA_FULLTEXT             128     \/* For full-text search *\/\r\n#define HA_UNIQUE_CHECK         256     \/* Check the key for uniqueness *\/\r\n#define HA_SPATIAL              1024    \/* For spatial search *\/\r\n#define HA_NULL_ARE_EQUAL       2048    \/* NULL in key are cmp as equal *\/\r\n#define HA_GENERATED_KEY        8192    \/* Automaticly generated key *\/<\/blockquote><\/pre>\n<p>key_part = 0x7f6514020860 \u8fd9\u4e2a\u6307\u9488\u6307\u5411\u4e00\u4e2aKEY_PART_INFO\u6570\u7ec4\uff0c\u5b58\u50a8\u4e86\u7d22\u5f15\u5404\u4e2a\u5217\u7684\u4fe1\u606f\u3002<\/p>\n<h4>2.2 \u6253\u5370\u7d22\u5f15\u67d0\u4e00\u5217\u7684\u57fa\u672c\u4fe1\u606f<\/h4>\n<pre><blockquote>(gdb) p s->table->key_info[1]->key_part[0]\r\n$29 = {\r\n  field = 0x7f6514020588, \r\n  offset = 5, \r\n  null_offset = 0, \r\n  length = 64, \r\n  store_length = 67, \r\n  key_type = 32768, \r\n  fieldnr = 2, \r\n  key_part_flag = 24, \r\n  type = 15 '\\017', \r\n  null_bit = 1 '\\001'\r\n}<\/blockquote><\/pre>\n<p>field = 0x7f6514020588 \u6307\u5411\u8fd9\u4e2a\u7d22\u5f15\u5217\u4f7f\u7528\u6570\u636e\u8868\u7684\u90a3\u4e00\u5217<\/p>\n<p>offset = 5 \u6539\u7d22\u5f15\u5217\u4ece\u7d22\u5f15\u4e32\u7684\u7b2c\u4e00\u4e2a\u504f\u79fb\u5f00\u59cb<\/p>\n<p>null_offset = 0 null\u4fe1\u606f\u5b58\u50a8\u504f\u79fb<\/p>\n<p>length = 64,  \u7d22\u5f15\u4e32\u957f\u5ea6<\/p>\n<p>store_length = 67, \u6574\u4e2a\u7d22\u5f15\u5165\u53e3\u957f\u5ea6<\/p>\n<p>&#8230;<\/p>\n<h4>2.3 \u6253\u5370\u7d22\u5f15\u67d0\u5217\u8be6\u60c5<\/h4>\n<pre><blockquote>(gdb) p *s->table->key_info[1]->key_part[0]->field\r\n$36 = {\r\n  ...\r\n  table = 0x7f6514023d30, \r\n  ...\r\n  field_name = 0x7f651401fcf4 \"nick\", \r\n  ...\r\n  key_start = {\r\n    map = 2\r\n  }, \r\n  part_of_key = {\r\n    map = 2\r\n  }, \r\n  part_of_key_not_clustered = {\r\n    map = 2\r\n  }, \r\n  part_of_sortkey = {\r\n    map = 2\r\n  }, \r\n  ...\r\n}<\/blockquote><\/pre>\n<p>table = 0x7f6514023d30 \u6307\u5411\u8be5\u5217\u6240\u5728\u7684\u6570\u636e\u8868<\/p>\n<p>field_name = 0x7f651401fcf4 &#8220;nick&#8221; \u5217\u540d<\/p>\n<p>key_start = 2 \u5305\u542b\u8fd9\u4e2a\u5b57\u6bb5\u4e14\u4e3a\u7d22\u5f15\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u7684\u7d22\u5f15\u7f16\u53f7\uff0c2\u7684\u4e8c\u8fdb\u5236\u662f10\uff0c\u4e5f\u5c31\u662f\u7b2c\u4e8c\u7d22\u5f15\u4ee5\u8be5\u5b57\u6bb5\u4e3a\u8d77\u59cb<\/p>\n<h3>3. MySQL\u6e90\u7801\u4e2d\u5982\u4f55\u4f7f\u7528\u7d22\u5f15\u4fe1\u606f<\/h3>\n<p>\u4e0b\u9762\u6458\u6284\u51fd\u6570store_create_info(\u663e\u793aSHOW CREATE TABLE\u547d\u4ee4)\u663e\u793a\u7d22\u5f15\u4fe1\u606f\u90e8\u5206\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><blockquote>1351   for (uint i=0 ; i < share->keys ; i++,key_info++)\r\n1352   {\r\n1353     KEY_PART_INFO *key_part= key_info->key_part;\r\n...\r\n1357     if (i == primary_key && !strcmp(key_info->name, primary_key_name))\r\n1358     {\r\n1359       found_primary=1;\r\n1364       packet->append(STRING_WITH_LEN(\"PRIMARY KEY\"));\r\n1365     }\r\n1366     else if (key_info->flags & HA_NOSAME)\r\n1367       packet->append(STRING_WITH_LEN(\"UNIQUE KEY \"));\r\n...\r\n1372     else\r\n1373       packet->append(STRING_WITH_LEN(\"KEY \"));\r\n...\r\n1380     for (uint j=0 ; j < key_info->key_parts ; j++,key_part++)\r\n1381     {\r\n...\r\n1401     }\r\n1402     packet->append(')');\r\n1403     store_key_options(thd, packet, table, key_info);\r\n1404     if (key_info->parser)\r\n1405     {\r\n...\r\n1410     }\r\n1411   }<\/blockquote><\/pre>\n<p>\u5c0f\u7ed3\uff1a<\/p>\n<p>(1) \u53ef\u4ee5\u770b\u5230MySQL\u5982\u4f55\u904d\u5386\u4e00\u4e2a\u8868\u7684\u6240\u6709\u7d22\u5f15<\/p>\n<pre><blockquote>key_info= table->key_info;\r\nfor (uint i=0 ; i < share->keys ; i++,key_info++)\r\n{\r\n  ...\r\n}<\/blockquote><\/pre>\n<p>(2) \u53ef\u4ee5\u770b\u5230MySQL\u5982\u4f55\u6839\u636ekey_info->flags\u5b57\u6bb5\u5982\u4f55\u533a\u5206\u552f\u4e00\u7d22\u5f15\uff0c\u5168\u6587\u7d22\u5f15\u7b49<\/p>\n<p>(3) \u53ef\u4ee5\u770b\u5230MySQL\u5982\u4f55\u904d\u5386\u4e00\u4e2a\u7d22\u5f15\u7684\u6240\u6709\u5b57\u6bb5\uff1a<\/p>\n<pre><blockquote>KEY_PART_INFO *key_part= key_info->key_part;\r\nfor (uint j=0 ; j < key_info->key_parts ; j++,key_part++)\r\n{\r\n  ...\r\n}<\/blockquote><\/pre>\n<p>(4) \u67e5\u770b\u5b8c\u6574\u7684\u8fd9\u4e2a\u51fd\u6570\u8fd8\u53ef\u4ee5\u770b\u5230MySQL\u5982\u4f55\u8bfb\u53d6\u6570\u636e\u8868\u7684\u5404\u4e2acolumn<\/p>\n<p>[\u5168\u6587\u5b8c]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f88\u67af\u71e5\u7684\uff0c\u914d\u9996\u80cc\u666f\u97f3\u4e50\u5427\uff1a \u672c\u6587\u5c06\u5c1d\u8bd5\u4ecb\u7ecdMySQL\u7d22\u5f15\u5b58\u50a8\u76f8\u5173\u7684\u6570\u636e\u7ed3\u6784\u3002\u7a0b\u5e8f=\u6570\u636e\u7ed3\u6784+\u7b97\u6cd5\uff0c\u4e86\u89e3\u6570\u636e\u7ed3\u6784\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4e86\u89e3MySQL\u6e90\u7801\u4e2d\u5982\u4f55\u4f7f\u7528\u7d22\u5f15\uff0c\u5982\u4f55\u9009\u62e9\u81ea\u5df1\u7684\u6267\u884c\u8ba1\u5212\u3002 1. MySQL\u5982\u4f55\u63cf\u8ff0\u67d0\u4e2a\u6570\u636e\u8868\u7684\u7d22\u5f15 MySQL\u4f7f\u7528TABLE\u5bf9\u8c61\u6765\u63cf\u8ff0\u4e00\u4e2a\u6570\u636e\u8868\uff0c\u90a3\u4e48\u6570\u636e\u8868\u7684\u7d22\u5f15\u662f\u5982\u4f55\u63cf\u8ff0\uff0c\u7d22\u5f15\u7684\u7edf\u8ba1\u4fe1\u606f\u53c8\u662f\u5982\u4f55\u5b58\u50a8\u7684\u5462? \u4f8b\u5982\u6211\u4eec\u6709\u5982\u4e0b\u6570\u636e\u8868\uff1a CREATE TABLE `users` ( `id` int(11) NOT NULL, `nick` varchar(32) DEFAULT NULL, `reg_date` datetime DEFAULT NULL, PRIMARY KEY (`id`), KEY `IND_NICK` (`nick`), KEY `IND_REGDATE` (`reg_date`) ) \u8be5\u8868\u6709\u7d22\u5f15\uff0cPRIMARY KEY\u3001IND_NICK\u3001IND_REGDATE\uff0c\u6211\u4eec\u6765\u770b\u770bMySQL\u5185\u90e8\u662f\u5982\u4f55\u5b58\u50a8\u8fd9\u4e09\u4e2a\u7d22\u5f15\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u7d22\u5f15\u7684\u7edf\u8ba1\u4fe1\u606f\u7684\u3002\u4e0b\u56fe\uff0c\u63cf\u8ff0\u4e86\u5b58\u50a8\u4e00\u4e2a\u6570\u636e\u8868\u7d22\u5f15\u7684\u4e3b\u8981\u7ed3\u6784\uff1a<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[6,5],"tags":[101,118,58],"class_list":["post-3967","post","type-post","status-publish","format-standard","hentry","category-mysql","category-code-staff","tag-index","tag-mysql","tag-source-code"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/3967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=3967"}],"version-history":[{"count":30,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/3967\/revisions"}],"predecessor-version":[{"id":4065,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/3967\/revisions\/4065"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=3967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=3967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=3967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}