{"id":6424,"date":"2024-11-14T09:36:47","date_gmt":"2024-11-14T01:36:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6424\/"},"modified":"2024-11-14T09:36:47","modified_gmt":"2024-11-14T01:36:47","slug":"mysql-explain%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6424\/","title":{"rendered":"mysql explain\u7684\u7528\u6cd5"},"content":{"rendered":"<blockquote><p>\n  explain\u547d\u4ee4\u7528\u4e8e\u5206\u6790mysql\u67e5\u8be2\u7684\u6267\u884c\u8ba1\u5212\uff0c\u627e\u51fa\u6027\u80fd\u4f4e\u4e0b\u7684\u539f\u56e0\u3002\u5176\u8f93\u51fa\u5305\u542b\uff1a\u8868\u522b\u540d\u67e5\u8be2\u7c7b\u578b\u8868\u540d\u8fde\u63a5\u7c7b\u578b\u7d22\u5f15\u4f7f\u7528\u60c5\u51b5\u4f30\u8ba1\u8fd4\u56de\u884c\u6570\u8fc7\u6ee4\u767e\u5206\u6bd4\u5176\u4ed6\u4fe1\u606f\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/02\/2024080207460833046.jpg\" class=\"aligncenter\" title=\"mysql explain\u7684\u7528\u6cd5\u63d2\u56fe\" alt=\"mysql explain\u7684\u7528\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL EXPLAIN\u7528\u6cd5<\/strong><\/p>\n<p>EXPLAIN\u662fMySQL\u4e2d\u4e00\u4e2a\u91cd\u8981\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5206\u6790SQL\u67e5\u8be2\u7684\u6267\u884c\u8ba1\u5212\u3002\u901a\u8fc7\u4f7f\u7528EXPLAIN\uff0c\u53ef\u4ee5\u4e86\u89e3\u67e5\u8be2\u662f\u5982\u4f55\u6267\u884c\u7684\uff0c\u5e76\u53ef\u4ee5\u627e\u51fa\u67e5\u8be2\u6027\u80fd\u4f4e\u4e0b\u7684\u539f\u56e0\u3002<\/p>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<p>\u4f7f\u7528EXPLAIN\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>EXPLAIN [EXTENDED] [FORMAT=JSON] query;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>EXTENDED\uff1a<\/strong>\u6269\u5c55\u8f93\u51fa\uff0c\u663e\u793a\u66f4\u591a\u4fe1\u606f\u3002<\/li>\n<li> <strong>FORMAT=JSON\uff1a<\/strong>\u5c06\u7ed3\u679c\u8f93\u51fa\u4e3aJSON\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<p><strong>\u8f93\u51fa<\/strong><\/p>\n<p>EXPLAIN\u7684\u8f93\u51fa\u5305\u542b\u4ee5\u4e0b\u4fe1\u606f\uff1a<\/p>\n<ul>\n<li> <strong>id\uff1a<\/strong>\u67e5\u8be2\u4e2d\u8868\u7684\u522b\u540d\u3002<\/li>\n<li> <strong>select_type\uff1a<\/strong>\u67e5\u8be2\u7c7b\u578b\uff0c\u5982SIMPLE\u3001UNION\u7b49\u3002<\/li>\n<li> <strong>table\uff1a<\/strong>\u8868\u540d\u3002<\/li>\n<li> <strong>partitions\uff1a<\/strong>\u5206\u533a\u4fe1\u606f\u3002<\/li>\n<li> <strong>type\uff1a<\/strong>\u8fde\u63a5\u7c7b\u578b\uff0c\u5982index\u3001ALL\u7b49\u3002<\/li>\n<li> <strong>possible_keys\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528\u4f46\u672a\u4f7f\u7528\u7684\u7d22\u5f15\u3002<\/li>\n<li> <strong>key\uff1a<\/strong>\u5b9e\u9645\u4f7f\u7528\u7684\u7d22\u5f15\uff08\u5982\u679c\u5b58\u5728\uff09\u3002<\/li>\n<li> <strong>key_len\uff1a<\/strong>\u7d22\u5f15\u957f\u5ea6\u3002<\/li>\n<li> <strong>ref\uff1a<\/strong>\u7528\u4e8e\u8fde\u63a5\u8868\u7684\u884c\u3002<\/li>\n<li> <strong>rows\uff1a<\/strong>\u4f30\u8ba1\u8fd4\u56de\u7684\u884c\u6570\u3002<\/li>\n<li> <strong>filtered\uff1a<\/strong>\u8fc7\u6ee4\u6389\u7684\u767e\u5206\u6bd4\u3002<\/li>\n<li> <strong>Extra\uff1a<\/strong>\u5176\u4ed6\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u4f7f\u7528<\/strong><\/p>\n<p>\u4e3a\u4e86\u4f7f\u7528EXPLAIN\uff0c\u53ea\u9700\u5728\u67e5\u8be2\u524d\u6dfb\u52a0EXPLAIN\u547d\u4ee4\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>EXPLAIN SELECT * FROM users WHERE name = 'John';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2aEXPLAIN\u8f93\u51fa\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>mysql&gt; EXPLAIN SELECT * FROM users WHERE name = 'John';\n+----+-------------+-------+------+---------------+----------+-------+------+------+-----------------------------+\n| id | select_type | table | type | possible_keys | key      | key_len | ref  | rows | Extra                       |\n+----+-------------+-------+------+---------------+----------+-------+------+------+-----------------------------+\n|  1 | SIMPLE      | users | ref  | name          | name     | 767     | const |    1 | Using where; Using index |\n+----+-------------+-------+------+---------------+----------+-------+------+------+-----------------------------+<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ece\u8f93\u51fa\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff1a<\/p>\n<ul>\n<li>\u8be5\u67e5\u8be2\u4f7f\u7528\u4e86\u7d22\u5f15\uff08<strong>type=ref<\/strong>\uff09\u3002<\/li>\n<li>\u4f7f\u7528\u4e86name\u5217\u7684\u7d22\u5f15\uff08<strong>key=name<\/strong>\uff09\u3002<\/li>\n<li>\u7d22\u5f15\u957f\u5ea6\u4e3a767\u5b57\u8282\uff08<strong>key_len=767<\/strong>\uff09\u3002<\/li>\n<li>\u4f30\u8ba1\u8fd4\u56de1\u884c\uff08<strong>rows=1<\/strong>\uff09\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u5b9a\u67e5\u8be2\u7684\u6267\u884c\u8ba1\u5212\u662f\u9ad8\u6548\u7684\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f explain\u7684\u7528\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>explain\u547d\u4ee4\u7528\u4e8e\u5206\u6790mysql\u67e5\u8be2\u7684\u6267\u884c\u8ba1\u5212\uff0c\u627e\u51fa\u6027\u80fd\u4f4e\u4e0b\u7684\u539f\u56e0\u3002\u5176\u8f93\u51fa\u5305\u542b\uff1a\u8868\u522b\u540d\u67e5\u8be2\u7c7b\u578b\u8868\u540d\u8fde\u63a5\u7c7b\u578b\u7d22\u5f15\u4f7f\u7528\u60c5\u51b5\u4f30\u8ba1\u8fd4\u56de\u884c\u6570\u8fc7\u6ee4\u767e\u5206\u6bd4\u5176\u4ed6\u4fe1\u606f MySQL EXPLAIN\u7528\u6cd5 EXPLAIN\u662fMySQL\u4e2d\u4e00\u4e2a\u91cd\u8981\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5206\u6790SQL\u67e5\u8be2\u7684\u6267\u884c\u8ba1\u5212\u3002\u901a\u8fc7\u4f7f\u7528EXPLAIN\uff0c\u53ef\u4ee5\u4e86\u89e3\u67e5\u8be2\u662f\u5982\u4f55\u6267\u884c\u7684\uff0c\u5e76\u53ef\u4ee5\u627e\u51fa\u67e5\u8be2\u6027\u80fd\u4f4e\u4e0b\u7684\u539f\u56e0\u3002 \u7528\u6cd5 \u4f7f\u7528EXPLAIN\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a EXPLAIN [EXTENDED] [FORMAT=JSON] query; \u767b\u5f55\u540e\u590d\u5236 EXTENDED\uff1a\u6269\u5c55\u8f93\u51fa\uff0c\u663e\u793a\u66f4\u591a\u4fe1\u606f\u3002 FORMAT=JSON\uff1a\u5c06\u7ed3\u679c\u8f93\u51fa\u4e3aJSON\u683c\u5f0f\u3002 \u8f93\u51fa EXPLAIN\u7684\u8f93\u51fa\u5305\u542b\u4ee5\u4e0b\u4fe1\u606f\uff1a id\uff1a\u67e5\u8be2\u4e2d\u8868\u7684\u522b\u540d\u3002 select_type\uff1a\u67e5\u8be2\u7c7b\u578b\uff0c\u5982SIMPLE\u3001UNION\u7b49\u3002 table\uff1a\u8868\u540d\u3002 partitions\uff1a\u5206\u533a\u4fe1\u606f\u3002 type\uff1a\u8fde\u63a5\u7c7b\u578b\uff0c\u5982index\u3001ALL\u7b49\u3002 possible_keys\uff1a\u53ef\u4ee5\u4f7f\u7528\u4f46\u672a\u4f7f\u7528\u7684\u7d22\u5f15\u3002 key\uff1a\u5b9e\u9645\u4f7f\u7528\u7684\u7d22\u5f15\uff08\u5982\u679c\u5b58\u5728\uff09\u3002 key_len\uff1a\u7d22\u5f15\u957f\u5ea6\u3002 ref\uff1a\u7528\u4e8e\u8fde\u63a5\u8868\u7684\u884c\u3002 rows\uff1a\u4f30\u8ba1\u8fd4\u56de\u7684\u884c\u6570\u3002 filtered\uff1a\u8fc7\u6ee4\u6389\u7684\u767e\u5206\u6bd4\u3002 Extra\uff1a\u5176\u4ed6\u4fe1\u606f\u3002 \u5982\u4f55\u4f7f\u7528 \u4e3a\u4e86\u4f7f\u7528EXPLAIN\uff0c\u53ea\u9700\u5728\u67e5\u8be2\u524d\u6dfb\u52a0EXPLAIN\u547d\u4ee4\u5373\u53ef\u3002\u4f8b\u5982\uff1a EXPLAIN SELECT * FROM users WHERE name = &#8216;John&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u793a\u4f8b \u4ee5\u4e0b\u662f\u4e00\u4e2aEXPLAIN\u8f93\u51fa\u7684\u793a\u4f8b\uff1a mysql&gt; EXPLAIN SELECT * FROM users WHERE name = &#8216;John&#8217;; +&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+ | id | [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6424","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6424"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6424\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}