{"id":74365,"date":"2024-10-15T22:23:16","date_gmt":"2024-10-15T14:23:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/74365\/"},"modified":"2024-10-15T22:23:16","modified_gmt":"2024-10-15T14:23:16","slug":"mysql-explain%e8%a9%b3%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/74365\/","title":{"rendered":"MySQL EXPLAIN\u8a73\u89e3"},"content":{"rendered":"<h1 id=\"mysql-explain%e8%a9%b3%e8%a7%a3-irwLdWULHl\">MySQL EXPLAIN\u8a73\u89e3<\/h1>\n<p>\u5728\u4f7f\u7528MySQL\u9032\u884c\u6578\u64da\u5eab\u67e5\u8a62\u6642\uff0c\u6027\u80fd\u512a\u5316\u662f\u6bcf\u4f4d\u958b\u767c\u8005\u548c\u6578\u64da\u5eab\u7ba1\u7406\u54e1\u5fc5\u9808\u9762\u5c0d\u7684\u91cd\u8981\u8ab2\u984c\u3002MySQL\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\u2014\u2014EXPLAIN\uff0c\u5e6b\u52a9\u7528\u6236\u5206\u6790\u67e5\u8a62\u7684\u57f7\u884c\u8a08\u5283\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMySQL EXPLAIN\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u5176\u91cd\u8981\u6027\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afexplain%ef%bc%9f-irwLdWULHl\">\u4ec0\u9ebc\u662fEXPLAIN\uff1f<\/h2>\n<p>EXPLAIN\u662f\u4e00\u500bMySQL\u547d\u4ee4\uff0c\u7528\u65bc\u986f\u793a\u67e5\u8a62\u7684\u57f7\u884c\u8a08\u5283\u3002\u901a\u904e\u9019\u500b\u547d\u4ee4\uff0c\u7528\u6236\u53ef\u4ee5\u4e86\u89e3MySQL\u5982\u4f55\u57f7\u884c\u7279\u5b9a\u7684SQL\u67e5\u8a62\uff0c\u5305\u62ec\u4f7f\u7528\u7684\u7d22\u5f15\u3001\u8868\u7684\u8a2a\u554f\u9806\u5e8f\u4ee5\u53ca\u5176\u4ed6\u6027\u80fd\u76f8\u95dc\u7684\u4fe1\u606f\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8explain-irwLdWULHl\">\u5982\u4f55\u4f7f\u7528EXPLAIN<\/h2>\n<p>\u4f7f\u7528EXPLAIN\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u5728\u67e5\u8a62\u8a9e\u53e5\u524d\u52a0\u4e0aEXPLAIN\u95dc\u9375\u5b57\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>EXPLAIN SELECT * FROM users WHERE age &gt; 30;<\/code><\/pre>\n<p>\u57f7\u884c\u4e0a\u8ff0\u547d\u4ee4\u5f8c\uff0cMySQL\u5c07\u8fd4\u56de\u4e00\u500b\u8868\u683c\uff0c\u986f\u793a\u67e5\u8a62\u7684\u57f7\u884c\u8a08\u5283\u3002\u9019\u500b\u8868\u683c\u5305\u542b\u591a\u500b\u91cd\u8981\u7684\u5217\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u95dc\u9375\u5217\u7684\u89e3\u91cb\uff1a<\/p>\n<ul>\n<li><strong>id\uff1a<\/strong>\u67e5\u8a62\u7684\u8b58\u5225\u78bc\uff0c\u5c0d\u65bc\u8907\u96dc\u67e5\u8a62\uff0c\u9019\u500b\u503c\u53ef\u4ee5\u5e6b\u52a9\u8b58\u5225\u4e0d\u540c\u7684\u67e5\u8a62\u90e8\u5206\u3002<\/li>\n<li><strong>select_type\uff1a<\/strong>\u67e5\u8a62\u7684\u985e\u578b\uff0c\u4f8b\u5982\u7c21\u55ae\u67e5\u8a62\u3001\u806f\u63a5\u67e5\u8a62\u7b49\u3002<\/li>\n<li><strong>table\uff1a<\/strong>\u6b63\u5728\u8a2a\u554f\u7684\u8868\u540d\u3002<\/li>\n<li><strong>type\uff1a<\/strong>\u8a2a\u554f\u985e\u578b\uff0c\u986f\u793aMySQL\u5982\u4f55\u8a2a\u554f\u8868\u3002\u5e38\u898b\u7684\u985e\u578b\u5305\u62ec\u5168\u8868\u6383\u63cf\uff08ALL\uff09\u3001\u7d22\u5f15\u6383\u63cf\uff08INDEX\uff09\u3001\u7bc4\u570d\u6383\u63cf\uff08RANGE\uff09\u7b49\u3002<\/li>\n<li><strong>possible_keys\uff1a<\/strong>\u53ef\u80fd\u4f7f\u7528\u7684\u7d22\u5f15\u3002<\/li>\n<li><strong>key\uff1a<\/strong>\u5be6\u969b\u4f7f\u7528\u7684\u7d22\u5f15\u3002<\/li>\n<li><strong>rows\uff1a<\/strong>\u9810\u8a08\u9700\u8981\u6383\u63cf\u7684\u884c\u6578\u3002<\/li>\n<li><strong>Extra\uff1a<\/strong>\u5176\u4ed6\u4fe1\u606f\uff0c\u4f8b\u5982\u662f\u5426\u4f7f\u7528\u4e86\u81e8\u6642\u8868\u6216\u6587\u4ef6\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<h2 id=\"explain%e7%9a%84%e5%af%a6%e9%9a%9b%e6%87%89%e7%94%a8-irwLdWULHl\">EXPLAIN\u7684\u5be6\u969b\u61c9\u7528<\/h2>\n<p>\u70ba\u4e86\u66f4\u597d\u5730\u7406\u89e3EXPLAIN\u7684\u4f5c\u7528\uff0c\u8b93\u6211\u5011\u770b\u4e00\u500b\u5177\u9ad4\u7684\u4f8b\u5b50\u3002\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba\u201corders\u201d\u7684\u8868\uff0c\u4e26\u4e14\u6211\u5011\u60f3\u67e5\u8a62\u6240\u6709\u57282023\u5e74\u4e0b\u55ae\u7684\u7528\u6236\uff1a<\/p>\n<pre><code>EXPLAIN SELECT * FROM orders WHERE order_date &gt; '2023-01-01';<\/code><\/pre>\n<p>\u57f7\u884c\u5f8c\uff0c\u5047\u8a2d\u8fd4\u56de\u7684\u7d50\u679c\u986f\u793atype\u70ba\u201cALL\u201d\uff0c\u9019\u610f\u5473\u8457MySQL\u5c07\u9032\u884c\u5168\u8868\u6383\u63cf\uff0c\u9019\u901a\u5e38\u662f\u6027\u80fd\u4e0d\u4f73\u7684\u6307\u6a19\u3002\u70ba\u4e86\u512a\u5316\u67e5\u8a62\uff0c\u6211\u5011\u53ef\u4ee5\u8003\u616e\u5728\u201corder_date\u201d\u5217\u4e0a\u5275\u5efa\u7d22\u5f15\uff1a<\/p>\n<pre><code>CREATE INDEX idx_order_date ON orders(order_date);<\/code><\/pre>\n<p>\u518d\u6b21\u57f7\u884cEXPLAIN\u547d\u4ee4\uff0c\u6aa2\u67e5\u67e5\u8a62\u7684\u6027\u80fd\u662f\u5426\u6709\u6240\u6539\u5584\u3002<\/p>\n<h2 id=\"explain%e7%9a%84%e9%80%b2%e9%9a%8e%e7%94%a8%e6%b3%95-irwLdWULHl\">EXPLAIN\u7684\u9032\u968e\u7528\u6cd5<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684EXPLAIN\u547d\u4ee4\uff0cMySQL\u9084\u63d0\u4f9b\u4e86EXPLAIN ANALYZE\uff0c\u9019\u662f\u4e00\u500b\u66f4\u70ba\u8a73\u7d30\u7684\u5206\u6790\u5de5\u5177\u3002EXPLAIN ANALYZE\u4e0d\u50c5\u986f\u793a\u57f7\u884c\u8a08\u5283\uff0c\u9084\u6703\u5be6\u969b\u57f7\u884c\u67e5\u8a62\u4e26\u63d0\u4f9b\u57f7\u884c\u6642\u9593\u7b49\u6027\u80fd\u6578\u64da\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>EXPLAIN ANALYZE SELECT * FROM orders WHERE order_date &gt; '2023-01-01';<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6df1\u5165\u5730\u4e86\u89e3\u67e5\u8a62\u7684\u6027\u80fd\u74f6\u9838\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-irwLdWULHl\">\u7e3d\u7d50<\/h2>\n<p>MySQL EXPLAIN\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u548c\u6578\u64da\u5eab\u7ba1\u7406\u54e1\u5206\u6790\u548c\u512a\u5316\u67e5\u8a62\u6027\u80fd\u3002\u901a\u904e\u7406\u89e3EXPLAIN\u7684\u8f38\u51fa\u7d50\u679c\uff0c\u4f7f\u7528\u8005\u53ef\u4ee5\u91dd\u5c0d\u6027\u5730\u9032\u884c\u7d22\u5f15\u512a\u5316\u548c\u67e5\u8a62\u91cd\u69cb\uff0c\u5f9e\u800c\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u6574\u9ad4\u6027\u80fd\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\uff0c\u638c\u63e1EXPLAIN\u7684\u4f7f\u7528\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u61c9\u7528\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Discover the intricacies of MySQL EXPLAIN with detailed insights, optimizing query performance and understanding execution plans effectively.<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-74365","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/74365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=74365"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/74365\/revisions"}],"predecessor-version":[{"id":74366,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/74365\/revisions\/74366"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=74365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=74365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=74365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}