﻿{"id":365,"date":"2017-07-28T20:08:53","date_gmt":"2017-07-28T12:08:53","guid":{"rendered":"http:\/\/www.chenweikang.top\/?p=365"},"modified":"2017-09-19T11:36:34","modified_gmt":"2017-09-19T03:36:34","slug":"mysql%e6%80%a7%e8%83%bd%e5%88%86%e6%9e%90%e5%8f%8aexplain%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.chenweikang.top\/?p=365","title":{"rendered":"MySQL\u6027\u80fd\u5206\u6790\u53caexplain\u7684\u4f7f\u7528"},"content":{"rendered":"<blockquote>\n<p style=\"text-align: left;\"><strong>MySQL\u6027\u80fd\u5206\u6790<\/strong>\u53ca<strong>explain<\/strong>\u7528\u6cd5\u7684\u77e5\u8bc6\u662f\u672c\u6587\u6211\u4eec\u4e3b\u8981\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\uff0c\u63a5\u4e0b\u6765\u5c31\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e9b\u5b9e\u9645\u7684\u4f8b\u5b50\u6765\u4ecb\u7ecd\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u5e0c\u671b\u80fd\u591f\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/blockquote>\n<p><strong>1.\u4f7f\u7528explain\u8bed\u53e5\u53bb\u67e5\u770b\u5206\u6790\u7ed3\u679c<\/strong><\/p>\n<p>\u5982explain select * from test1 where id=1;\u4f1a\u51fa\u73b0\uff1aid\u00a0 selecttype\u00a0 table\u00a0 type possible_keys\u00a0 key key_len\u00a0 ref rows\u00a0 extra\u5404\u5217\u3002<\/p>\n<p>\u5176\u4e2d\uff0c<\/p>\n<p>type=const\u8868\u793a\u901a\u8fc7\u7d22\u5f15\u4e00\u6b21\u5c31\u627e\u5230\u4e86\uff1b<\/p>\n<p>key=primary\u7684\u8bdd\uff0c\u8868\u793a\u4f7f\u7528\u4e86\u4e3b\u952e\uff1b<\/p>\n<p>type=all,\u8868\u793a\u4e3a\u5168\u8868\u626b\u63cf\uff1b<\/p>\n<p>key=null\u8868\u793a\u6ca1\u7528\u5230\u7d22\u5f15\u3002type=ref,\u56e0\u4e3a\u8fd9\u65f6\u8ba4\u4e3a\u662f\u591a\u4e2a\u5339\u914d\u884c\uff0c\u5728\u8054\u5408\u67e5\u8be2\u4e2d\uff0c\u4e00\u822c\u4e3aREF\u3002<\/p>\n<p><strong>2.MYSQL\u4e2d\u7684\u7ec4\u5408\u7d22\u5f15<\/strong><\/p>\n<p>\u5047\u8bbe\u8868\u6709id,key1,key2,key3,\u628a\u4e09\u8005\u5f62\u6210\u4e00\u4e2a\u7ec4\u5408\u7d22\u5f15\uff0c\u5219<\/p>\n<p>\u5982\uff1a<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">where\u00a0<span class=\"attribute\">key1<\/span>=....<\/li>\n<li>\u00a0\u00a0\u00a0where\u00a0<span class=\"attribute\">key1<\/span>=1\u00a0and\u00a0<span class=\"attribute\">key2<\/span>=2<\/li>\n<li class=\"alt\">\u00a0\u00a0\u00a0where\u00a0<span class=\"attribute\">key1<\/span>=<span class=\"attribute-value\">3<\/span>\u00a0and\u00a0<span class=\"attribute\">key3<\/span>=3\u00a0and\u00a0<span class=\"attribute\">key2<\/span>=2<\/li>\n<\/ol>\n<p>\u6839\u636e\u6700\u5de6\u539f\u5219\uff0c\u8fd9\u4e9b\u90fd\u662f\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u7684\uff0c\u5982from test where key1=1 order by key3\uff0c\u7528explain\u5206\u6790\u7684\u8bdd\uff0c\u53ea\u7528\u5230\u4e86normal_key\u7d22\u5f15\uff0c\u4f46\u53ea\u5bf9where\u5b50\u53e5\u8d77\u4f5c\u7528\uff0c\u800c\u540e\u9762\u7684order by\u9700\u8981\u6392\u5e8f\u3002<\/p>\n<p><strong>3.\u4f7f\u7528\u6162\u67e5\u8be2\u5206\u6790<\/strong><\/p>\n<p>\u5728my.ini\u4e2d\uff1a<\/p>\n<p>long_query_time=1<\/p>\n<p>log-slow-queries=d:\\mysql5\\logs\\mysqlslow.log<\/p>\n<p>\u628a\u8d85\u8fc71\u79d2\u7684\u8bb0\u5f55\u5728\u6162\u67e5\u8be2\u65e5\u5fd7\u4e2d<\/p>\n<p>\u53ef\u4ee5\u7528mysqlsla\u6765\u5206\u6790\u4e4b\u3002\u4e5f\u53ef\u4ee5\u5728mysqlreport\u4e2d\uff0c\u6709\u5982<\/p>\n<p>DMS\u5206\u522b\u5206\u6790\u4e86select ,update,insert,delete,replace\u7b49\u6240\u5360\u7684\u767e\u4efd\u6bd4<\/p>\n<p><strong>4.MYISAM\u548cINNODB\u7684\u9501\u5b9a<\/strong><\/p>\n<p>myisam\u4e2d\uff0c\u6ce8\u610f\u662f\u8868\u9501\u6765\u7684\uff0c\u6bd4\u5982\u5728\u591a\u4e2aUPDATE\u64cd\u4f5c\u540e\uff0c\u518dSELECT\u65f6\uff0c\u4f1a\u53d1\u73b0SELECT\u64cd\u4f5c\u88ab\u9501\u5b9a\u4e86\uff0c\u5fc5\u987b\u7b49\u6240\u6709UPDATE\u64cd\u4f5c\u5b8c\u6bd5\u540e\uff0c\u518d\u80fdSELECT<\/p>\n<p>innodb\u7684\u8bdd\u5219\u4e0d\u540c\u4e86\uff0c\u7528\u7684\u662f\u884c\u9501\uff0c\u4e0d\u5b58\u5728\u4e0a\u9762\u95ee\u9898\u3002<\/p>\n<p><strong>5.MYSQL\u7684\u4e8b\u52a1\u914d\u7f6e\u9879<\/strong><\/p>\n<p>innodb_flush_log_at_trx_commit=1<\/p>\n<p>\u8868\u793a\u4e8b\u52a1\u63d0\u4ea4\u65f6\u7acb\u5373\u628a\u4e8b\u52a1\u65e5\u5fd7\u5199\u5165\u78c1\u76d8\uff0c\u540c\u65f6\u6570\u636e\u548c\u7d22\u5f15\u4e5f\u66f4\u65b0\u3002<\/p>\n<p>innodb_flush_log_at_trx_commit=0<\/p>\n<p>\u4e8b\u52a1\u63d0\u4ea4\u65f6\uff0c\u4e0d\u7acb\u5373\u628a\u4e8b\u52a1\u65e5\u5fd7\u5199\u5165\u78c1\u76d8\uff0c\u6bcf\u96941\u79d2\u5199\u4e00\u6b21<\/p>\n<p>innodb_flush_log_at_trx_commit=2<\/p>\n<p>\u4e8b\u52a1\u63d0\u4ea4\u65f6\uff0c\u7acb\u5373\u5199\u5165\u78c1\u76d8\u6587\u4ef6\uff08\u8fd9\u91cc\u53ea\u662f\u5199\u5165\u5230\u5185\u6838\u7f13\u51b2\u533a\uff0c\u4f46\u4e0d\u7acb\u5373\u5237\u65b0\u5230\u78c1\u76d8\uff0c\u800c\u662f\u6bcf\u96941\u79d2\u5237\u65b0\u5230\u76d8\uff0c\u540c\u65f6\u66f4\u65b0\u6570\u636e\u548c\u7d22\u5f15<\/p>\n<p><strong>explain\u7528\u6cd5<\/strong><\/p>\n<p>EXPLAIN tbl_name\u6216\uff1aEXPLAIN [EXTENDED] SELECT select_options<\/p>\n<p>\u524d\u8005\u53ef\u4ee5\u5f97\u51fa\u4e00\u4e2a\u8868\u7684\u5b57\u6bb5\u7ed3\u6784\u7b49\u7b49\uff0c\u540e\u8005\u4e3b\u8981\u662f\u7ed9\u51fa\u76f8\u5173\u7684\u4e00\u4e9b\u7d22\u5f15\u4fe1\u606f\uff0c\u800c\u4eca\u5929\u8981\u8bb2\u8ff0\u7684\u91cd\u70b9\u662f\u540e\u8005\u3002<\/p>\n<p>\u4e3e\u4f8b<\/p>\n<ol class=\"dp-xml\">\n<li class=\"alt\">mysql<span class=\"tag\">&gt;<\/span>\u00a0explain\u00a0select\u00a0*\u00a0from\u00a0event;<\/li>\n<li>+\u2014-+\u2014\u2014\u2014\u2014-+\u2014\u2014-+\u2014\u2014+\u2014\u2014\u2014\u2014\u2014+\u2014\u2014+\u2014\u2014\u2014+\u2014\u2014+\u2014\u2014+\u2014\u2014-+<\/li>\n<li class=\"alt\">|\u00a0id\u00a0|\u00a0select_type\u00a0|\u00a0table\u00a0|\u00a0type\u00a0|\u00a0possible_keys\u00a0|\u00a0key\u00a0|\u00a0key_len\u00a0|\u00a0ref\u00a0|\u00a0rows\u00a0|\u00a0Extra\u00a0|<\/li>\n<li>+\u2014-+\u2014\u2014\u2014\u2014-+\u2014\u2014-+\u2014\u2014+\u2014\u2014\u2014\u2014\u2014+\u2014\u2014+\u2014\u2014\u2014+\u2014\u2014+\u2014\u2014+\u2014\u2014-+<\/li>\n<li class=\"alt\">|\u00a01\u00a0|\u00a0SIMPLE\u00a0|\u00a0event\u00a0|\u00a0ALL\u00a0|\u00a0NULL\u00a0|\u00a0NULL\u00a0|\u00a0NULL\u00a0|\u00a0NULL\u00a0|\u00a013\u00a0|\u00a0|<\/li>\n<li>+\u2014-+\u2014\u2014\u2014\u2014-+\u2014\u2014-+\u2014\u2014+\u2014\u2014\u2014\u2014\u2014+\u2014\u2014+\u2014\u2014\u2014+\u2014\u2014+\u2014\u2014+\u2014\u2014-+<\/li>\n<li class=\"alt\">1\u00a0row\u00a0in\u00a0set\u00a0(0.00\u00a0sec)<\/li>\n<\/ol>\n<p><strong>\u5404\u4e2a\u5c5e\u6027\u7684\u542b\u4e49<\/strong><\/p>\n<p>id<\/p>\n<p>select\u67e5\u8be2\u7684\u5e8f\u5217\u53f7<\/p>\n<p>select_type<\/p>\n<p>select\u67e5\u8be2\u7684\u7c7b\u578b\uff0c\u4e3b\u8981\u662f\u533a\u522b\u666e\u901a\u67e5\u8be2\u548c\u8054\u5408\u67e5\u8be2\u3001\u5b50\u67e5\u8be2\u4e4b\u7c7b\u7684\u590d\u6742\u67e5\u8be2\u3002<\/p>\n<p>table<\/p>\n<p>\u8f93\u51fa\u7684\u884c\u6240\u5f15\u7528\u7684\u8868\u3002<\/p>\n<p>type<\/p>\n<p>\u8054\u5408\u67e5\u8be2\u6240\u4f7f\u7528\u7684\u7c7b\u578b\u3002<\/p>\n<p>type\u663e\u793a\u7684\u662f\u8bbf\u95ee\u7c7b\u578b\uff0c\u662f\u8f83\u4e3a\u91cd\u8981\u7684\u4e00\u4e2a\u6307\u6807\uff0c\u7ed3\u679c\u503c\u4ece\u597d\u5230\u574f\u4f9d\u6b21\u662f\uff1a<\/p>\n<p>system &gt; const &gt; eq_ref &gt; ref &gt; fulltext &gt; ref_or_null &gt; index_merge &gt; unique_subquery &gt; index_subquery &gt; range &gt; index &gt; ALL<\/p>\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u5f97\u4fdd\u8bc1\u67e5\u8be2\u81f3\u5c11\u8fbe\u5230range\u7ea7\u522b\uff0c\u6700\u597d\u80fd\u8fbe\u5230ref\u3002<\/p>\n<p>possible_keys<\/p>\n<p>\u6307\u51faMySQL\u80fd\u4f7f\u7528\u54ea\u4e2a\u7d22\u5f15\u5728\u8be5\u8868\u4e2d\u627e\u5230\u884c\u3002\u5982\u679c\u662f\u7a7a\u7684\uff0c\u6ca1\u6709\u76f8\u5173\u7684\u7d22\u5f15\u3002\u8fd9\u65f6\u8981\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u901a\u8fc7\u68c0\u9a8cWHERE\u5b50\u53e5\uff0c\u770b\u662f\u5426\u5f15\u7528\u67d0\u4e9b\u5b57\u6bb5\uff0c\u6216\u8005\u68c0\u67e5\u5b57\u6bb5\u4e0d\u662f\u9002\u5408\u7d22\u5f15\u3002<\/p>\n<p>key<\/p>\n<p>\u663e\u793aMySQL\u5b9e\u9645\u51b3\u5b9a\u4f7f\u7528\u7684\u952e\u3002\u5982\u679c\u6ca1\u6709\u7d22\u5f15\u88ab\u9009\u62e9\uff0c\u952e\u662fNULL\u3002<\/p>\n<p>key_len<\/p>\n<p>\u663e\u793aMySQL\u51b3\u5b9a\u4f7f\u7528\u7684\u952e\u957f\u5ea6\u3002\u5982\u679c\u952e\u662fNULL\uff0c\u957f\u5ea6\u5c31\u662fNULL\u3002\u6587\u6863\u63d0\u793a\u7279\u522b\u6ce8\u610f\u8fd9\u4e2a\u503c\u53ef\u4ee5\u5f97\u51fa\u4e00\u4e2a\u591a\u91cd\u4e3b\u952e\u91ccmysql\u5b9e\u9645\u4f7f\u7528\u4e86\u54ea\u4e00\u90e8\u5206\u3002<\/p>\n<p>ref<\/p>\n<p>\u663e\u793a\u54ea\u4e2a\u5b57\u6bb5\u6216\u5e38\u6570\u4e0ekey\u4e00\u8d77\u88ab\u4f7f\u7528\u3002<\/p>\n<p>rows<\/p>\n<p>\u8fd9\u4e2a\u6570\u8868\u793amysql\u8981\u904d\u5386\u591a\u5c11\u6570\u636e\u624d\u80fd\u627e\u5230\uff0c\u5728innodb\u4e0a\u662f\u4e0d\u51c6\u786e\u7684\u3002<\/p>\n<p>Extra<\/p>\n<p>\u5982\u679c\u662fOnly index\uff0c\u8fd9\u610f\u5473\u7740\u4fe1\u606f\u53ea\u7528\u7d22\u5f15\u6811\u4e2d\u7684\u4fe1\u606f\u68c0\u7d22\u51fa\u7684\uff0c\u8fd9\u6bd4\u626b\u63cf\u6574\u4e2a\u8868\u8981\u5feb\u3002<\/p>\n<p>\u5982\u679c\u662fwhere used\uff0c\u5c31\u662f\u4f7f\u7528\u4e0a\u4e86where\u9650\u5236\u3002<\/p>\n<p>\u5982\u679c\u662fimpossible where \u8868\u793a\u7528\u4e0d\u7740where\uff0c\u4e00\u822c\u5c31\u662f\u6ca1\u67e5\u51fa\u6765\u5565\u3002<\/p>\n<p>\u5982\u679c\u6b64\u4fe1\u606f\u663e\u793aUsing filesort\u6216\u8005Using temporary\u7684\u8bdd\u4f1a\u5f88\u5403\u529b\uff0cWHERE\u548cORDER BY\u7684\u7d22\u5f15\u7ecf\u5e38\u65e0\u6cd5\u517c\u987e\uff0c\u5982\u679c\u6309\u7167WHERE\u6765\u786e\u5b9a\u7d22\u5f15\uff0c\u90a3\u4e48\u5728ORDER BY\u65f6\uff0c\u5c31\u5fc5\u7136\u4f1a\u5f15\u8d77Using filesort\uff0c\u8fd9\u5c31\u8981\u770b\u662f\u5148\u8fc7\u6ee4\u518d\u6392\u5e8f\u5212\u7b97\uff0c\u8fd8\u662f\u5148\u6392\u5e8f\u518d\u8fc7\u6ee4\u5212\u7b97\u3002<\/p>\n<p>\u5173\u4e8eMySQL\u6027\u80fd\u5206\u6790\u53caexplain\u7528\u6cd5\u7684\u77e5\u8bc6\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u5e0c\u671b\u672c\u6b21\u7684\u4ecb\u7ecd\u80fd\u591f\u5bf9\u60a8\u6709\u6240\u6536\u83b7\uff01<\/p>\n<p>&nbsp;<\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.chenweikang.top\">\u5de6\u624b\u4ee3\u7801\u53f3\u624b\u8bd7<\/a> &raquo; <a href=\"https:\/\/www.chenweikang.top\/?p=365\">MySQL\u6027\u80fd\u5206\u6790\u53caexplain\u7684\u4f7f\u7528<\/a><\/p><div class=\"__youshang\">\r\n            <div id=\"__youshang_popup\" class=\"wechat popup\" style=\"display: none;\">\r\n                <div class=\"head\">~\u8c22\u8c22\u6253\u8d4f~<\/div>\r\n                <div class=\"qrcode\"><div class=\"qrcode-li wechat\" ><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/wexin.png\" \/><\/div><div class=\"qrcode-li alipay\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-pay.png\" \/><\/div><div class=\"qrcode-li hongbao\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-hongbao.png\" \/><\/div><\/div>\r\n                <ul class=\"platform\"><li class=\"icon-wechat active\" data-bg-color=\"#05af4e\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-alipay \" data-bg-color=\"#00a2ea\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-hongbao \" data-bg-color=\"#dd5746\" data-thanks=\"<p>\u626b\u7801\u9886\u7ea2\u5305<\/p><p style='margin-top: 24px;'>\uff08\u4f59\u989d\u5b9d\u652f\u4ed8\u65f6\u53ef\u62b5\u73b0\uff09<\/p>\"><\/li><\/ul>\r\n            <\/div>\r\n            <a href=\"javascript:void(0);\" id=\"__youshang_btn\">\u8d4f<\/a>\r\n        <\/div>","protected":false},"excerpt":{"rendered":"<p>MySQL\u6027\u80fd\u5206\u6790\u53caexplain\u7528\u6cd5\u7684\u77e5\u8bc6\u662f\u672c\u6587\u6211\u4eec\u4e3b\u8981\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\uff0c\u63a5\u4e0b\u6765\u5c31\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e9b\u5b9e\u9645\u7684\u4f8b\u5b50\u6765\u4ecb\u7ecd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":363,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[45],"class_list":["post-365","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database","tag-45"],"_links":{"self":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=365"}],"version-history":[{"count":0,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/365\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/media\/363"}],"wp:attachment":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}