{"id":6898,"date":"2017-08-14T17:26:19","date_gmt":"2017-08-14T09:26:19","guid":{"rendered":"http:\/\/rmohan.com\/?p=6898"},"modified":"2017-08-14T17:26:19","modified_gmt":"2017-08-14T09:26:19","slug":"mysql-innodb-engine-b-tree-index-simple-finishing-instructions","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6898","title":{"rendered":"MySQL InnoDB engine B + tree index simple finishing instructions"},"content":{"rendered":"<p>There are several types of InnoDB engine tables in MySQL (the following index, no special instructions, refers to the InnoDB engine table index.)<br \/>\n0 = Secondary Index, 2-level index,<br \/>\n1 = Clustered Index, clustered index<br \/>\n2 = Unique Index, Unique Index<br \/>\n3 = Primary Index, Primary Key Index<br \/>\n32 = Full-text Index, Full Index<br \/>\n64 = Spatial Index<br \/>\n128 = A secondary index that includes a virtual generated column.<\/p>\n<p>The second-level index, the clustered index, the unique index, the primary key index, the second-level computed column index, and the logical storage structure, are all stored in the B + tree to store<br \/>\nthis point with the B + tree index logical storage structure of the other database Not too different, the above several indexes are divided from the logical point of view.<\/p>\n<p>If the physical storage from the point of view, MySQL in these types of index can be divided into clustered index and secondary index (or non-clustered index)<br \/>\nAmong them, the primary key index and clustered index, can be classified as clustered index, Unique index, secondary computed column index data nonclustered index.<\/p>\n<p><strong>The clustered index in MySQL<\/strong><\/p>\n<p>MySQL clustered index is based on the primary key, the entire table of data, logically organized into a B + tree, so a table can only be a clustered index.<br \/>\nThe non-leaf node stores the clustered index key value, and the leaf node stores the data itself in the table, and the leaf node and the leaf node are connected together by a two-way list.<\/p>\n<p>Know the MySQL clustering index before the first look at the MySQL primary key generation mechanism.<br \/>\nMySQL InnoDB must have a primary key,<br \/>\nif the time specified in the construction of the primary key, then the primary key is the main key of the table (sounds so awkward, mainly with the primary key is not specified, the automatic generation of the main key for comparison)<br \/>\nIf the primary key is not specified, the storage engine will automatically create a primary key column for the table.<br \/>\n1) For the table that specifies the primary key, the primary key is the primary key index.<br \/>\n2) For tables that do not specify a primary key, If there is a unique index (one or more) that is not empty, the (first) non-null unique constraint is the primary key.<br \/>\n3) For a table that does not specify a primary key and there is no unique constraint, a primary key is generated by default. The index is &#8220;clustered index&#8221;, in<br \/>\nfact, the former &#8220;primary key index&#8221; and the latter &#8220;clustered index&#8221;, the physical storage can be attributed to clustered index<\/p>\n<p>1, explicit primary key index (clustered index)<\/p>\n<p>In the following screenshot, the test_index_type_1 is created<br \/>\nwhen the primary key is specified in the table, the primary key defaults to the primary key index. The index type is 3 (from the physical storage point of view, the index is<br \/>\ncreated\u00a0)\u00a0Level index, the index type is 0 (from the physical storage point of view, is non-clustered index)<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444891.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6899\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444891.png\" alt=\"\" width=\"780\" height=\"382\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444891.png 780w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444891-300x147.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444891-768x376.png 768w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444891-150x73.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444891-400x196.png 400w\" sizes=\"(max-width: 780px) 100vw, 780px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>2, non-empty unique constraints generated by the primary key index (clustered index)<\/p>\n<p>The following screenshot, created test_index_type_2,<br \/>\ndid not specify the primary key when the table was\u00a0created\u00a0, but specified a unique non-null constraint, then the field would be used as the primary key, and the resulting index type would be 3 (from the physical storage point of view index)<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444892-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6900\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444892-2.png\" alt=\"\" width=\"758\" height=\"294\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444892-2.png 758w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444892-2-300x116.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444892-2-150x58.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444892-2-400x155.png 400w\" sizes=\"(max-width: 758px) 100vw, 758px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>3, the system default primary key generated clustered index (clustered index)<\/p>\n<p>In the following screenshot, the test_index_type_3 table was created,<br \/>\nthe primary key was not specified at the time of the table, and the unique non-null constraint was specified. The InnoDB engine automatically generates a 6-byte pointer. The generated index type is a clustered index, (From the physical storage point of view, is the clustered index)<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444894-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6901\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444894-4.png\" alt=\"\" width=\"729\" height=\"317\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444894-4.png 729w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444894-4-300x130.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444894-4-150x65.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444894-4-400x174.png 400w\" sizes=\"(max-width: 729px) 100vw, 729px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Nonclustered index<\/strong><\/p>\n<p>The non-clustered index, the nonclustered index is also the structure of the B + tree to store the data.<br \/>\nThe difference from the clustered index is that the leaf node of the nonclustered cable stores only the key value of the index + the key value of the clustered index, but does not include All non-index key values.<\/p>\n<p>1, unique index generated by the unique index (non-clustered index)<\/p>\n<p>The following screenshot, created the test_index_type_4 table,<br \/>\nspecify the id as unique, then the id will automatically create a unique index.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444895-5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6903\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444895-5.png\" alt=\"\" width=\"729\" height=\"378\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444895-5.png 729w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444895-5-300x156.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444895-5-150x78.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444895-5-400x207.png 400w\" sizes=\"(max-width: 729px) 100vw, 729px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>2, the manual creation of a unique index (non-clustered index) The<br \/>\nfollowing screenshot, created test_index_type5 table,<br \/>\nmanually create a unique index, then the index type is unique index<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444895-5-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6904\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444895-5-1.png\" alt=\"\" width=\"729\" height=\"378\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444895-5-1.png 729w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444895-5-1-300x156.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444895-5-1-150x78.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444895-5-1-400x207.png 400w\" sizes=\"(max-width: 729px) 100vw, 729px\" \/><\/a> <a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444896-6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6905\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444896-6.png\" alt=\"\" width=\"706\" height=\"402\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444896-6.png 706w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444896-6-300x171.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444896-6-150x85.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444896-6-400x228.png 400w\" sizes=\"(max-width: 706px) 100vw, 706px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>3, manually created secondary index (non-clustered index) The<br \/>\nfollowing screenshot, created test_index_type6 table,<br \/>\nthat manually create an index (not specified unique), then the index type for the secondary index<\/p>\n<p>&nbsp;<\/p>\n<p>4, the calculation of the column index, in the calculation of the column manually create the index (non-clustered index) The<br \/>\nfollowing screenshot, created test_index_type7 table,<\/p>\n<p>test_index_type7 there is a calculation column, after the completion of the calculation column in the calculation of the index,<\/p>\n<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444897-7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6906\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444897-7.png\" alt=\"\" width=\"728\" height=\"404\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444897-7.png 728w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444897-7-300x166.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444897-7-150x83.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444897-7-400x222.png 400w\" sizes=\"(max-width: 728px) 100vw, 728px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>to sum up:<\/p>\n<p>Overall, MySQL several types of B + tree index is still relatively easy to understand, with the SQL Server index is also similar.<br \/>\nMySQL&#8217;s InnoDB engine table, the primary key index, non-null unique constraint generated by the clustered index, clustered index, from the physical storage point of view are data clustered index.<br \/>\nPrimary key index, non-empty unique constraint generated clustered index, clustered index, the three have a distinct feature, all require that the column is non-empty and unique.<br \/>\nThe other is that MySQL can not explicitly create a clustered index, that is, create clustered index.<\/p>\n<p>This is very different from SQL Server,<br \/>\n1, in SQL Server, if you do not specify the primary key, or specify the primary key is not specified but nonclustered, then the table is for the heap table, the system will not add the default field as a clustered index<br \/>\n2 , SQL Server primary key can only be the primary key, you can not clustered index (by default the primary key is clustered index).<\/p>\n<p>The clustered index can be specified on any column, either a non-primary key column, a non-unique, nullable, repeatable column, such as<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444898-8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-6907\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2017\/08\/170731091444898-8.png\" alt=\"\" width=\"720\" height=\"270\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444898-8.png 720w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444898-8-300x113.png 300w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444898-8-150x56.png 150w, https:\/\/mohan.sg\/wp-content\/uploads\/2017\/08\/170731091444898-8-400x150.png 400w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are several types of InnoDB engine tables in MySQL (the following index, no special instructions, refers to the InnoDB engine table index.) 0 = Secondary Index, 2-level index, 1 = Clustered Index, clustered index 2 = Unique Index, Unique Index 3 = Primary Index, Primary Key Index 32 = Full-text Index, Full Index 64 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72,16],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6898"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6898"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6898\/revisions"}],"predecessor-version":[{"id":6908,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6898\/revisions\/6908"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}