{"id":464,"date":"2019-02-25T23:12:32","date_gmt":"2019-02-25T15:12:32","guid":{"rendered":"https:\/\/kaispace.cn\/?p=464"},"modified":"2019-02-25T23:12:32","modified_gmt":"2019-02-25T15:12:32","slug":"%e5%85%b3%e4%ba%8emysql%e4%b9%8b%e7%b4%a2%e5%bc%95%e5%91%bd%e4%b8%ad%e6%83%85%e5%86%b5%e6%88%96explain-type%e6%8e%a2%e8%ae%a8","status":"publish","type":"post","link":"https:\/\/blog.kaispace.cn\/?p=464","title":{"rendered":"\u5173\u4e8eMysql\u4e4b\u7d22\u5f15\u547d\u4e2d\u60c5\u51b5\u6216EXPLAIN TYPE\u63a2\u8ba8"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u770b\u7d22\u5f15\u6ca1\u6709\u547d\u4e2d\u7684\u60c5\u51b5\uff0c\u5728\u81ea\u5df1\u5b9e\u9645\u64cd\u4f5c\u540e\u8fd8\u662f\u53d1\u73b0\u8ddf\u7f51\u4e0a\u535a\u5ba2\u5343\u7bc7\u4e00\u5f8b\u7684\u7ed3\u679c\u6709\u4e00\u4e9b\u4e0d\u540c\uff0c\u7f51\u4e0a\u535a\u5ba2\u8bb2\u7684\u4e0d\u591f\u7ec6\uff0c\u60c5\u51b5\u4e0d\u591f\u5168\u9762\uff0c\u56e0\u6b64\u81ea\u5df1\u52a8\u624b\u5b9e\u64cd\u4e86\u4e0b\u6574\u7406\u4e86\u4e0b\u7ed3\u8bba\u3002\u9664\u4e86\u4e00\u4e9b\u5c0f\u8868\u4e0d\u9002\u7528\uff0c\u5927\u8868\u8d85\u8fc730%\u4e0d\u4f7f\u7528\u65e0\u6cd5\u6d4b\u8bd5\uff0c\u6211\u4ece\u805a\u7c07\u7d22\u5f15\u4ee5\u53ca\u4e8c\u7ea7\u7d22\u5f15\u6982\u5ff5\u5165\u624b\uff0c\u66f4\u52a0\u5177\u4f53\u7684\u4f53\u4f1a\u4e86\u4e0d\u540ctype\u7684\u539f\u56e0\u3002<\/p>\n<blockquote><p>\n  \u6700\u5de6\u524d\u7f00\u5728WHERE\u5185\u65e0\u6240\u8c13\u5148\u540e\uff0c\u4f46\u662fWHERE\u4e00\u5b9a\u5148\u4e8eORDER BY\uff0c\u53ea\u8981WHERE+ORDER BY\u52a0\u8d77\u6765\u6ee1\u8db3\u6700\u5de6\u524d\u7f00\u5c31\u884c\u3002\n<\/p><\/blockquote>\n<ol>\n<li><strong>index(\u7d22\u5f15\u5168\u626b\u63cf,\u53ea\u7528\u5230\u805a\u7c07\u7d22\u5f15\u6216\u4e8c\u7ea7\u7d22\u5f15)\uff1a<\/strong><br \/>\n\u7ed3\u679c\u5217\u53ea\u7528\u5230\u7d22\u5f15\u5185\uff0c\u6761\u4ef6\u5217\u4e5f\u662f\u7d22\u5f15\u5185(\u4e0d\u662f\u8303\u56f4\u67e5\u8be2)\u4f46\u6ca1\u6ee1\u8db3\u6700\u5de6\u524d\u7f00\u3002\u4e0a\u8ff0\u60c5\u51b5\u5373\u4f7f\u5728WHERE column+1=0\u4ea6\u6216\u8005DATE(column)\u90fd\u662f\u4e00\u6837\u7684\u7ed3\u679c,\u4e0d\u4f1a\u7834\u574f\u4f7f\u4e4b\u65e0\u6cd5\u4f7f\u7528\u7d22\u5f15\u3002<\/li>\n<li><strong>ref(\u53ea\u7528\u5230\u4e8c\u7ea7\u7d22\u5f15)\uff1a<\/strong><br \/>\n\u7ed3\u679c\u5217\u65e0\u6240\u8c13\uff0c\u6761\u4ef6\u5217\u662f\u7d22\u5f15\u4ee5\u5185(\u4e0d\u662f\u8303\u56f4\u67e5\u8be2)\u4e14\u6ee1\u8db3\u6700\u5de6\u524d\u7f00\uff0c\u4e0a\u8ff0\u60c5\u51b5\u5728WHERE column+1=0\u4ea6\u6216\u8005DATE(column)\u4f1a\u7834\u574f\u4f7f\u4e4b\u65e0\u6cd5\u4f7f\u7528\u7d22\u5f15\uff0c\u56e0\u4e3a\u7834\u574f\u4e86\u6700\u5de6\u524d\u7f00\u6761\u4ef6\u3002<\/li>\n<\/ol>\n<blockquote><p>\n  \u7834\u574f\u7d22\u5f15\u7684\u60c5\u51b5\uff1a\u8fd0\u7b97\uff0c\u5185\u7f6e\u51fd\u6570\uff0cis null(is not null\u4f1a\u8d70),like &#8216;%'(\u6ce8\u610f\u901a\u914d\u7b26\u5728\u524d),not in,in\n<\/p><\/blockquote>\n<ol start=\"3\">\n<li><strong>range(\u7d22\u5f15\u8303\u56f4\uff0c\u53ea\u7528\u5230\u805a\u7c07\u7d22\u5f15[\u4e3b\u952e]\u6216\u4e8c\u7ea7\u7d22\u5f15[\u552f\u4e00\u7d22\u5f15])\uff1a<\/strong><br \/>\n\u7ed3\u679c\u5217\u65e0\u6240\u8c13\uff0c\u6761\u4ef6\u5217\u662f\u4e3b\u952e\u5217\u6216\u552f\u4e00\u7d22\u5f15\u4ee5\u53ca\u8986\u76d6\u7d22\u5f15\u5168\u4f7f\u7528\u3002<br \/>\n=, &lt;>, >, >=, &lt;, &lt;=, IS NULL, &lt;=>, BETWEEN, LIKE, or IN()<\/p>\n<\/li>\n<li>\n<p><strong>const<\/strong><br \/>\nconst \u53ea\u7528\u4e3b\u952e\u6216\u552f\u4e00\u7d22\u5f15\u4f5c\u4e3a\u6761\u4ef6\u4e14\u53ea\u662f\u7528\u5230=\u6216&lt;=><\/p>\n<\/li>\n<\/ol>\n<h3>\u603b\u7ed3\uff1a<\/h3>\n<h4>\u53ef\u4ee5\u7406\u89e3\u4e3a\u82db\u523b\u7684\u6761\u4ef6\u4e0b\uff0c\u4f1a\u76f8\u5e94\u7ed9\u4e0e\u4e00\u5b9a\u7684\u8865\u507f\u63aa\u65bd\u3002<\/h4>\n<p><strong>index\uff1a<\/strong> \u53d1\u751f\u5728\u4ece<strong>\u7ed3\u679c\u5217<\/strong>\u53ef\u77e5possible_keys\u4f46\u662f\u6761\u4ef6\u5217\u88ab\u7834\u574f\u6216\u8005\u4e0d\u6ee1\u8db3\u6700\u5de6\u524d\u7f00\u3002(\u666e\u901a\u7d22\u5f15\u6216\u4e3b\u952e\u6216\u552f\u4e00\u7d22\u5f15)<\/p>\n<p><strong>ref\uff1a<\/strong> \u53d1\u751f\u5728\u6ee1\u8db3\u6700\u5de6\u524d\u7f00\u4e14\u4e0d\u80fd\u88ab\u7834\u574f\u3002(\u666e\u901a\u7d22\u5f15)<\/p>\n<p><strong>const\uff1a<\/strong> \u53d1\u751f\u5728\u4e3b\u952e\u6216\u8005\u552f\u4e00\u7d22\u5f15\u3002(\u4e3b\u952e\u6216\u8005\u552f\u4e00\u7d22\u5f15)<\/p>\n<p><strong>range\uff1a<\/strong> \u53d1\u751f\u5728\u4e3b\u952e\u6216\u8005\u552f\u4e00\u7d22\u5f15\u6216** \u5b8c\u5168 **\u6ee1\u8db3\u6700\u5de6\u524d\u7f00 \u4e14 \u6761\u4ef6\u5217\u4e0d\u88ab\u7834\u574f\u3002(\u4e3b\u952e\u6216\u8005\u552f\u4e00\u7d22\u5f15\u6216\u666e\u901a\u7d22\u5f15\u5168\u4f7f\u7528)<\/p>\n<p><strong>index\uff1a<\/strong><br \/>\n\u7ed3\u679c\u5217\u548c\u6761\u4ef6\u5217\u90fd\u662f\u540c\u4e2a\u7d22\u5f15\u5185\uff0c\u5c31\u7b49\u4e8e\u5df2\u7ecf\u544a\u77e5\u4e86\u5b58\u50a8\u5f15\u64ce\u6211\u8981\u7528\u4ec0\u4e48\u7d22\u5f15\uff0c\u6240\u4ee5\u653e\u677e\u5bf9\u6761\u4ef6\u5217\u8981\u6c42(\u4f60\u600e\u4e48column+1=1,DATE(column)\u90fd\u6ca1\u5f71\u54cd)(\u5956\u52b1)\uff0c\u56e0\u4e3a\u53ea\u9700\u8981\u5728\u4e8c\u7ea7\u7d22\u5f15\u67e5\u8be2\u5c31\u53ef\u4ee5\u6ee1\u8db3\u6761\u4ef6\uff0c<\/p>\n<p><strong>ref\uff1a<\/strong>\uff1a<br \/>\n\u56e0\u4e3a\u6761\u4ef6\u5217\u662f\u975e\u5e38\u82db\u523b\u7684\u8981\u6c42\u6ee1\u8db3\u6700\u5de6\u524d\u7f00\uff0c\u56e0\u6b64\u5728\u4e8c\u7ea7\u7d22\u5f15\u662f\u53ef\u4ee5\u5b9a\u4f4d\u7684\uff0c\u4e0d\u7528\u7d22\u5f15\u5168\u626b\u63cf\uff0c\u56e0\u6b64\u5728\u627e\u5230\u6570\u636e\u540e\u56de\u805a\u7c07\u7d22\u5f15\u62ff\u5176\u4ed6\u5217\u4e5f\u65e0\u6240\u8c13(\u5956\u52b1)\u3002<\/p>\n<p><strong>range:<\/strong><br \/>\n\u56e0\u4e3a\u8981\u6c42\u662f\u4e3b\u952e\u6216\u8005\u552f\u4e00\u7d22\u5f15\u6216\u8005\u7d22\u5f15\u5b8c\u5168\u6ee1\u8db3\uff01\u6ce8\u610f\u662f\u5b8c\u5168\u6ee1\u8db3\uff01\u56e0\u6b64\u4e5f\u5bf9\u7ed3\u679c\u5217\u653e\u677e\u8981\u6c42\uff0c\u4f46\u5931\u53bb\u7684\u5c31\u662f\u5bf9\u6761\u4ef6\u5217\u7279\u522b\u7684\u654f\u611f\uff0c\u8ddfref\u4e00\u6837\u3002<\/p>\n<h1>\u4ee5\u4e0b\u662f\u5b98\u7f51\u7684type\u89e3\u91ca<\/h1>\n<p><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/explain-output.html#jointype_ref\">EXPLAIN Join Types<\/a><\/p>\n<h1>type:\u6027\u80fd\u6392\u5e8f \u4ece\u4e0a\u5230\u4e0b\u8d8a\u6765\u8d8a\u5dee<\/h1>\n<h2>1. system<\/h2>\n<p>The table has only one row (= system table). This is a special case of the const join type.<\/p>\n<h2>2. const<\/h2>\n<pre><code class=\"language-SQL line-numbers\">SELECT * FROM tbl_name WHERE primary_key=1;\n\nSELECT * FROM tbl_name\n  WHERE primary_key_part1=1 AND primary_key_part2=2;\n<\/code><\/pre>\n<p>The table has at most one matching row, which is read at the start of the query. Because there is only one row, values from the column in this row can be regarded as constants by the rest of the optimizer. const tables are very fast because they are read only once.<\/p>\n<h2>3. eq_ref<\/h2>\n<pre><code class=\"language-SQL line-numbers\">SELECT * FROM ref_table,other_table\n  WHERE ref_table.key_column=other_table.column;\n\nSELECT * FROM ref_table,other_table\n  WHERE ref_table.key_column_part1=other_table.column\n  AND ref_table.key_column_part2=1;\n<\/code><\/pre>\n<p>One row is read from this table for each combination of rows from the previous tables. Other than the system and const types, this is the best possible join type. It is used when all parts of an index are used by the join and the index is a PRIMARY KEY or UNIQUE NOT NULL index.<\/p>\n<p>eq_ref can be used for indexed columns that are compared using the = operator. The comparison value can be a constant or an expression that uses columns from tables that are read before this table.<\/p>\n<h2>4.ref<\/h2>\n<pre><code class=\"language-sql line-numbers\">SELECT * FROM ref_table WHERE key_column=expr;\n\nSELECT * FROM ref_table,other_table\n  WHERE ref_table.key_column=other_table.column;\n\nSELECT * FROM ref_table,other_table\n  WHERE ref_table.key_column_part1=other_table.column\n  AND ref_table.key_column_part2=1;\n<\/code><\/pre>\n<p>All rows with matching index values are read from this table for each combination of rows from the previous tables. ref is used if the join uses only a leftmost prefix of the key or if the key is not a PRIMARY KEY or UNIQUE index (in other words, if the join cannot select a single row based on the key value). If the key that is used matches only a few rows, this is a good join type.<br \/>\nref can be used for indexed columns that are compared using the = or &lt;=> operator.<\/p>\n<h2>5.fulltext<\/h2>\n<p>The join is performed using a FULLTEXT index.<\/p>\n<h2>6.ref_or_null<\/h2>\n<pre><code class=\"language-SQL line-numbers\">SELECT * FROM ref_table\n  WHERE key_column=expr OR key_column IS NULL;\n<\/code><\/pre>\n<p>This join type is like ref, but with the addition that MySQL does an extra search for rows that contain NULL values. This join type optimization is used most often in resolving subqueries.<\/p>\n<h2>7.index_merge<\/h2>\n<p>This join type indicates that the Index Merge optimization is used. In this case, the key column in the output row contains a list of indexes used, and key_len contains a list of the longest key parts for the indexes used.<\/p>\n<h2>8.unique_subquery<\/h2>\n<p>This type replaces eq_ref for some IN subqueries of the following form:<\/p>\n<pre><code class=\"language-sql line-numbers\">value IN (SELECT primary_key FROM single_table WHERE some_expr)```\nunique_subquery is just an index lookup function that replaces the subquery completely for better efficiency.\n\n## 9.index_subquery\n\nThis join type is similar to unique_subquery. It replaces IN subqueries, but it works for nonunique indexes in subqueries of the following form:\n```SQL\nvalue IN (SELECT key_column FROM single_table WHERE some_expr)```\n\nvalue IN (SELECT key_column FROM single_table WHERE some_expr)\n\n## 10.range\n\nOnly rows that are in a given range are retrieved, using an index to select the rows. The key column in the output row indicates which index is used. The key_len contains the longest key part that was used. The ref column is NULL for this type.\n```SQL\nSELECT * FROM tbl_name\n  WHERE key_column = 10;\n\nSELECT * FROM tbl_name\n  WHERE key_column BETWEEN 10 and 20;\n\nSELECT * FROM tbl_name\n  WHERE key_column IN (10,20,30);\n\nSELECT * FROM tbl_name\n  WHERE key_part1 = 10 AND key_part2 IN (10,20,30);\n<\/code><\/pre>\n<p>range can be used when a key column is compared to a constant using any of the =, &lt;>, >, >=, &lt;, &lt;=, IS NULL, &lt;=>, BETWEEN, LIKE, or IN() operators:<\/p>\n<h2>11.index<\/h2>\n<p>The index join type is the same as ALL, except that the index tree is scanned. This occurs two ways:<\/p>\n<ul>\n<li>If the index is a covering index for the queries and can be used to satisfy all data required from the table, only the index tree is scanned. In this case, the Extra column says Using index. An index-only scan usually is faster than ALL because the size of the index usually is smaller than the table data.<\/p>\n<\/li>\n<li>\n<p>A full table scan is performed using reads from the index to look up data rows in index order. Uses index does not appear in the Extra column.<\/p>\n<\/li>\n<\/ul>\n<p>MySQL can use this join type when the query uses only columns that are part of a single index.<\/p>\n<h2>12.ALL<\/h2>\n<p>A full table scan is done for each combination of rows from the previous tables. This is normally not good if the table is the first table not marked const, and usually very bad in all other cases. Normally, you can avoid ALL by adding indexes that enable row retrieval from the table based on constant values or column values from earlier tables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u770b\u7d22\u5f15\u6ca1\u6709\u547d\u4e2d\u7684\u60c5\u51b5\uff0c\u5728\u81ea\u5df1\u5b9e\u9645\u64cd\u4f5c\u540e\u8fd8\u662f\u53d1\u73b0\u8ddf\u7f51\u4e0a\u535a\u5ba2\u5343\u7bc7\u4e00\u5f8b\u7684\u7ed3\u679c\u6709\u4e00\u4e9b\u4e0d\u540c\uff0c\u7f51\u4e0a\u535a\u5ba2\u8bb2\u7684\u4e0d\u591f\u7ec6\uff0c\u60c5&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/464"}],"collection":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=464"}],"version-history":[{"count":0,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=\/wp\/v2\/posts\/464\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kaispace.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}