{"id":5146,"date":"2024-11-14T08:00:39","date_gmt":"2024-11-14T00:00:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5146\/"},"modified":"2024-11-14T08:00:39","modified_gmt":"2024-11-14T00:00:39","slug":"mysql%e6%89%a7%e8%a1%8c%e8%ae%a1%e5%88%92%e6%80%8e%e4%b9%88%e6%9f%a5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5146\/","title":{"rendered":"mysql\u6267\u884c\u8ba1\u5212\u600e\u4e48\u67e5"},"content":{"rendered":"<p><strong>\u5982\u4f55\u67e5\u770b  \u6267\u884c\u8ba1\u5212<\/strong><\/p>\n<p>\u6267\u884c\u8ba1\u5212\u662f MySQL \u4f18\u5316\u5668\u5728\u6267\u884c\u67e5\u8be2\u4e4b\u524d\u4e3a\u67e5\u8be2\u751f\u6210\u7684\u6b65\u9aa4\u5e8f\u5217\u3002\u901a\u8fc7\u67e5\u770b\u6267\u884c\u8ba1\u5212\uff0c\u60a8\u53ef\u4ee5\u4e86\u89e3 MySQL \u5982\u4f55\u5904\u7406\u67e5\u8be2\uff0c\u5e76\u8bc6\u522b\u53ef\u80fd\u5f71\u54cd\u67e5\u8be2\u6027\u80fd\u7684\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n<p>\u67e5\u770b\u6267\u884c\u8ba1\u5212\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>EXPLAIN \u5173\u952e\u5b57<\/strong><\/p>\n<p>EXPLAIN \u5173\u952e\u5b57\u5141\u8bb8\u60a8\u67e5\u770b\u67e5\u8be2\u7684\u6267\u884c\u8ba1\u5212\u800c\u4e0d\u5b9e\u9645\u6267\u884c\u67e5\u8be2\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>EXPLAIN [FORMAT {JSON | TREE | TRADITIONAL}] \u67e5\u8be2;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>EXPLAIN SELECT * FROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>SHOWPLAN<\/strong><\/p>\n<p>SHOWPLAN \u8bed\u53e5\u7c7b\u4f3c\u4e8e EXPLAIN\uff0c\u4f46\u5b83\u66f4\u8be6\u7ec6\uff0c\u5e76\u4e14\u4ee5\u8868\u683c\u7684\u5f62\u5f0f\u663e\u793a\u6267\u884c\u8ba1\u5212\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SHOWPLAN FOR \u67e5\u8be2;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>performance_schema.setup_instruments<\/strong> \u8868<\/p>\n<p>performance_schema.setup_instruments \u8868\u5b58\u50a8\u4e86\u5f53\u524d\u8bed\u53e5\u7684\u6267\u884c\u8ba1\u5212\u3002\u8981\u8bbf\u95ee\u6b64\u4fe1\u606f\uff0c\u8bf7\u6267\u884c\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT digest, cardinality FROM performance_schema.setup_instruments\nWHERE SCHEMA_NAME = 'schema_name'\nAND NAME = 'query_name';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b65\u9aa4\u5c55\u5f00<\/strong><\/p>\n<p>\u67e5\u770b\u6267\u884c\u8ba1\u5212\u540e\uff0c\u60a8\u53ef\u4ee5\u5c55\u5f00\u6b65\u9aa4\u4ee5\u83b7\u53d6\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e EXPLAIN \u8f93\u51fa\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>EXPLAIN FORMAT=TREE SELECT * FROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u4ee5\u6811\u72b6\u7ed3\u6784\u663e\u793a\u6267\u884c\u8ba1\u5212\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u6709\u5176\u5b50\u6b65\u9aa4\u3002<\/p>\n<p><strong>\u7406\u89e3\u6267\u884c\u8ba1\u5212<\/strong><\/p>\n<p>\u6267\u884c\u8ba1\u5212\u7684\u8f93\u51fa\u5305\u62ec\u4ee5\u4e0b\u4fe1\u606f\uff1a<\/p>\n<ul>\n<li> <strong>id:<\/strong> \u6b65\u9aa4\u7684 ID\u3002<\/li>\n<li> <strong>select_type:<\/strong> \u6b65\u9aa4\u7684\u7c7b\u578b\uff0c\u4f8b\u5982 SIMPLE\u3001DEPENDENT SUBQUERY\u3002<\/li>\n<li> <strong>table:<\/strong> \u6b63\u5728\u8bbf\u95ee\u7684\u8868\u3002<\/li>\n<li> <strong>type:<\/strong> \u8bbf\u95ee\u7c7b\u578b\u7684\u8bbf\u95ee\u7c7b\u578b\uff0c\u4f8b\u5982 ALL\u3001INDEX\u3001RANGE\u3002<\/li>\n<li> <strong>possible_keys:<\/strong> \u53ef\u7528\u4e8e\u8bbf\u95ee\u8868\u7684\u53ef\u80fd\u7684\u7d22\u5f15\u3002<\/li>\n<li> <strong>key:<\/strong> \u5b9e\u9645\u7528\u4e8e\u8bbf\u95ee\u8868\u7684\u7d22\u5f15\u3002<\/li>\n<li> <strong>rows:<\/strong> \u4f30\u8ba1\u6b65\u9aa4\u8fd4\u56de\u7684\u884c\u6570\u3002<\/li>\n<li> <strong>Extra:<\/strong> \u6709\u5173\u6b65\u9aa4\u7684\u5176\u4ed6\u4fe1\u606f\uff0c\u4f8b\u5982\u4f7f\u7528\u7684\u7d22\u5f15\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u7406\u89e3\u6267\u884c\u8ba1\u5212\uff0c\u60a8\u53ef\u4ee5\u8bc6\u522b\u67e5\u8be2\u4e2d\u53ef\u80fd\u5b58\u5728\u7684\u95ee\u9898\uff0c\u4f8b\u5982\u7f3a\u5c11\u7d22\u5f15\u3001\u4e0d\u5408\u9002\u7684\u8fde\u63a5\u987a\u5e8f\u6216\u4e0d\u5fc5\u8981\u7684\u5b50\u67e5\u8be2\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u4fe1\u606f\u6765\u4f18\u5316\u67e5\u8be2\u5e76\u63d0\u9ad8\u5176\u6027\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6267\u884c\u8ba1\u5212\u600e\u4e48\u67e5\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>\u5982\u4f55\u67e5\u770b \u6267\u884c\u8ba1\u5212 \u6267\u884c\u8ba1\u5212\u662f MySQL \u4f18\u5316\u5668\u5728\u6267\u884c\u67e5\u8be2\u4e4b\u524d\u4e3a\u67e5\u8be2\u751f\u6210\u7684\u6b65\u9aa4\u5e8f\u5217\u3002\u901a\u8fc7\u67e5\u770b\u6267\u884c\u8ba1\u5212\uff0c\u60a8\u53ef\u4ee5\u4e86\u89e3 MySQL \u5982\u4f55\u5904\u7406\u67e5\u8be2\uff0c\u5e76\u8bc6\u522b\u53ef\u80fd\u5f71\u54cd\u67e5\u8be2\u6027\u80fd\u7684\u6f5c\u5728\u95ee\u9898\u3002 \u67e5\u770b\u6267\u884c\u8ba1\u5212\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a EXPLAIN \u5173\u952e\u5b57 EXPLAIN \u5173\u952e\u5b57\u5141\u8bb8\u60a8\u67e5\u770b\u67e5\u8be2\u7684\u6267\u884c\u8ba1\u5212\u800c\u4e0d\u5b9e\u9645\u6267\u884c\u67e5\u8be2\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a EXPLAIN [FORMAT {JSON | TREE | TRADITIONAL}] \u67e5\u8be2; \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff1a EXPLAIN SELECT * FROM table_name; \u767b\u5f55\u540e\u590d\u5236 SHOWPLAN SHOWPLAN \u8bed\u53e5\u7c7b\u4f3c\u4e8e EXPLAIN\uff0c\u4f46\u5b83\u66f4\u8be6\u7ec6\uff0c\u5e76\u4e14\u4ee5\u8868\u683c\u7684\u5f62\u5f0f\u663e\u793a\u6267\u884c\u8ba1\u5212\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a SHOWPLAN FOR \u67e5\u8be2; \u767b\u5f55\u540e\u590d\u5236 performance_schema.setup_instruments \u8868 performance_schema.setup_instruments \u8868\u5b58\u50a8\u4e86\u5f53\u524d\u8bed\u53e5\u7684\u6267\u884c\u8ba1\u5212\u3002\u8981\u8bbf\u95ee\u6b64\u4fe1\u606f\uff0c\u8bf7\u6267\u884c\u4ee5\u4e0b\u67e5\u8be2\uff1a SELECT digest, cardinality FROM performance_schema.setup_instruments WHERE SCHEMA_NAME = &#8216;schema_name&#8217; AND NAME = &#8216;query_name&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u6b65\u9aa4\u5c55\u5f00 [&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-5146","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5146","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=5146"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5146\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}