{"id":97702,"date":"2026-08-30T02:00:37","date_gmt":"2026-08-29T18:00:37","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/97702.html"},"modified":"2026-08-30T02:00:37","modified_gmt":"2026-08-29T18:00:37","slug":"%e8%af%a6%e8%a7%a3mysql-%e6%85%a2%e6%9f%a5%e8%af%a2%e6%97%a5%e5%bf%97-%e9%99%84%e8%af%a6%e7%bb%86%e6%a1%88%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/97702.html","title":{"rendered":"\u8be6\u89e3MySQL \u6162\u67e5\u8be2\u65e5\u5fd7( \u9644\u8be6\u7ec6\u6848\u4f8b )"},"content":{"rendered":"<h2>\u6982\u5ff5<\/h2>\n<p>\u6162\u67e5\u8be2\u65e5\u5fd7&#xff08;slow query log&#xff09;\u662f MySQL \u8bb0\u5f55\u6267\u884c\u65f6\u95f4\u8d85\u8fc7\u6307\u5b9a\u9608\u503cSQL \u7684\u65e5\u5fd7&#xff0c;\u7528\u6765\u5b9a\u4f4d\u6162 SQL\u3001\u6027\u80fd\u74f6\u9888&#xff0c;\u662f SQL \u4f18\u5316\u6700\u57fa\u7840\u7684\u5de5\u5177\u3002<\/p>\n<h2>\u4e00\u3001\u6162 SQL<\/h2>\n<p>\u6162 SQL \u4e0d\u662f\u8bed\u6cd5\u9519\u8bef\u7684 SQL&#xff0c;\u662f\u6267\u884c\u6548\u7387\u4f4e\u4e0b&#xff0c;\u6d88\u8017\u5927\u91cf\u6570\u636e\u5e93\u8d44\u6e90&#xff08;CPU\u3001IO\u3001\u9501&#xff09;&#xff0c;\u6267\u884c\u8017\u65f6\u8f83\u957f\u7684 SQL \u8bed\u53e5\u3002 \u7b80\u5355\u8bf4&#xff1a;\u8dd1\u7684\u6162\u3001\u8017\u8d44\u6e90&#xff0c;\u62d6\u57ae MySQL \u7684 SQL&#xff0c;\u5c31\u53eb\u6162 SQL\u3002<\/p>\n<p>\u6162\u67e5\u8be2\u65e5\u5fd7\u53ea\u662f\u7528\u6765\u6355\u83b7\u6162 SQL \u7684\u5de5\u5177&#xff1b;\u6162 SQL \u662f\u5bf9\u8c61&#xff0c;\u6162\u67e5\u8be2\u65e5\u5fd7\u662f\u8bb0\u5f55\u5de5\u5177&#xff0c;\u4e8c\u8005\u4e0d\u8981\u6df7\u6dc6\u3002<\/p>\n<h3>\u5224\u5b9a\u6807\u51c6<\/h3>\n<p>\u6709\u4e24\u4e2a\u6838\u5fc3\u5224\u65ad\u7ef4\u5ea6&#xff0c;\u4e0d\u80fd\u53ea\u770b\u6267\u884c\u65f6\u95f4<\/p>\n<li>\u6267\u884c\u8017\u65f6&#xff1a;\u8d85\u8fc7 long_query_time \u9608\u503c&#xff08;\u6bd4\u5982 1s&#xff09;&#xff0c;\u6162\u67e5\u8be2\u65e5\u5fd7\u4f1a\u6355\u83b7\u3002<\/li>\n<li>\u626b\u63cf\u884c\u6570 (Rows_examined)&#xff1a;\u626b\u63cf\u5927\u91cf\u6570\u636e&#xff0c;\u54ea\u6015\u8fd4\u56de\u5f88\u5c11\u7ed3\u679c\u3001\u8017\u65f6\u5f88\u77ed&#xff0c;\u4f9d\u7136\u5c5e\u4e8e\u6f5c\u5728\u6162 SQL\u3002<\/li>\n<p>\u4e3e\u4f8b&#xff1a;\u4e00\u6761 SQL \u6267\u884c 0.4 \u79d2&#xff0c;\u6ca1\u8fbe\u5230 1s \u9608\u503c\u4e0d\u4f1a\u8fdb\u6162\u65e5\u5fd7&#xff0c;\u4f46\u626b\u63cf 20 \u4e07\u884c\u3002\u6570\u636e\u91cf\u589e\u957f\u540e&#xff0c;\u672a\u6765\u4f1a\u53d8\u6210\u4e25\u91cd\u6162 SQL&#xff0c;\u8fd9\u5c31\u662f\u9690\u5f62\u6162 SQL&#xff0c;\u6700\u5bb9\u6613\u88ab\u5ffd\u7565\u3002<\/p>\n<h3>\u6162 SQL \u5e38\u89c1\u6210\u56e0<\/h3>\n<h4>1. \u7d22\u5f15\u95ee\u9898&#xff08;\u5360 80% \u4ee5\u4e0a\u573a\u666f&#xff09;<\/h4>\n<ul>\n<li>\u6ca1\u5efa\u7d22\u5f15&#xff0c;\u53d1\u751f\u5168\u8868\u626b\u63cf<\/li>\n<li>\u7d22\u5f15\u5931\u6548&#xff1a;\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u3001like %xxx\u3001or \u6761\u4ef6\u3001\u5bf9\u7d22\u5f15\u5217\u505a\u51fd\u6570\u8fd0\u7b97\u3001not in \u7b49<\/li>\n<li>\u7d22\u5f15\u9009\u62e9\u9519\u8bef&#xff1a;\u4f18\u5316\u5668\u9009\u9519\u7d22\u5f15<\/li>\n<li>\u7d22\u5f15\u533a\u5206\u5ea6\u5dee&#xff0c;\u4f4e\u9009\u62e9\u6027\u7d22\u5f15<\/li>\n<\/ul>\n<h4>2. SQL \u5199\u6cd5\u95ee\u9898<\/h4>\n<ul>\n<li>select * \u67e5\u8be2\u5927\u91cf\u4e0d\u9700\u8981\u5b57\u6bb5&#xff0c;\u56de\u8868\u5f00\u9500\u5927&#xff0c;\u7f51\u7edc\u4f20\u8f93\u91cf\u5927<\/li>\n<li>\u5927\u8868 join&#xff0c;\u5173\u8054\u5b57\u6bb5\u65e0\u7d22\u5f15<\/li>\n<li>\u4e0d\u5408\u7406\u5b50\u67e5\u8be2&#xff0c;\u884d\u751f\u8868\u6ca1\u6709\u7d22\u5f15<\/li>\n<li>limit \u5927\u504f\u79fb&#xff1a;limit 100000,10&#xff0c;\u626b\u63cf\u524d\u9762\u5341\u4e07\u884c<\/li>\n<li>in \u91cc\u9762\u653e\u6210\u5343\u4e0a\u4e07\u7684\u503c<\/li>\n<li>order by \/group by \u6ca1\u6709\u5408\u9002\u7d22\u5f15&#xff0c;\u89e6\u53d1\u6587\u4ef6\u6392\u5e8f\u3001\u4e34\u65f6\u8868<\/li>\n<\/ul>\n<h4>3. \u4e1a\u52a1 &amp; \u6570\u636e\u5c42\u9762<\/h4>\n<ul>\n<li>\u5355\u8868\u6570\u636e\u91cf\u8fc7\u5927&#xff0c;\u5343\u4e07\u7ea7\u522b\u4ee5\u4e0a&#xff0c;\u6ca1\u6709\u5206\u5e93\u5206\u8868<\/li>\n<li>\u4e8b\u52a1\u8fc7\u5927&#xff1a;\u4e00\u4e2a\u4e8b\u52a1\u91cc\u9762\u6267\u884c\u5927\u91cf DML&#xff0c;\u9501\u6301\u6709\u65f6\u95f4\u957f&#xff0c;\u5bfc\u81f4\u5927\u91cf SQL \u963b\u585e\u7b49\u5f85\u9501<\/li>\n<li>\u6279\u91cf\u4e00\u6b21\u6027\u64cd\u4f5c\u6d77\u91cf\u6570\u636e<\/li>\n<\/ul>\n<h4>4. \u6570\u636e\u5e93\u73af\u5883\u56e0\u7d20<\/h4>\n<ul>\n<li>IO \u538b\u529b\u9ad8\u3001CPU \u6253\u6ee1&#xff0c;\u518d\u597d\u7684 SQL \u4e5f\u8dd1\u6162<\/li>\n<li>\u9501\u7b49\u5f85&#xff1a;\u4e0d\u662f SQL \u672c\u8eab\u6162&#xff0c;\u662f\u7b49\u884c\u9501 \/ \u8868\u9501&#xff0c;Lock_time\u5f88\u9ad8<\/li>\n<li>buffer pool \u592a\u5c0f&#xff0c;\u5927\u91cf\u6570\u636e\u8bfb\u78c1\u76d8\u800c\u4e0d\u662f\u5185\u5b58<\/li>\n<\/ul>\n<h3>\u6162 SQL \u5e26\u6765\u7684\u5371\u5bb3<\/h3>\n<li>CPU\/IO \u98d9\u5347&#xff0c;\u6570\u636e\u5e93\u6574\u4f53\u54cd\u5e94\u53d8\u6162<\/li>\n<li>\u9501\u7b49\u5f85\u5806\u79ef&#xff0c;\u8fde\u63a5\u6570\u6253\u6ee1&#xff0c;\u5e94\u7528\u62a5\u9519<\/li>\n<li>\u8fde\u9501\u96ea\u5d29&#xff1a;\u4e00\u6761\u6162 SQL \u5360\u6ee1\u8d44\u6e90&#xff0c;\u6b63\u5e38\u4e1a\u52a1 SQL \u4e5f\u88ab\u62d6\u6162<\/li>\n<li>\u4e3b\u4ece\u5ef6\u8fdf&#xff1a;\u5927 DML \u6162 SQL \u9020\u6210\u4ece\u5e93\u590d\u5236\u5ef6\u8fdf<\/li>\n<h3>\u5982\u4f55\u53d1\u73b0\u6162 SQL<\/h3>\n<li>\u6162\u67e5\u8be2\u65e5\u5fd7 slow_query_log&#xff1a;\u4e8b\u540e\u8bb0\u5f55\u5df2\u7ecf\u6267\u884c\u5b8c\u7684\u6162 SQL<\/li>\n<li>show processlist \/ show full processlist&#xff1a;\u6293\u6b63\u5728\u8fd0\u884c\u7684\u6162 SQL<\/li>\n<li>performance_schema&#xff1a;\u7ebf\u4e0a\u6301\u7eed\u76d1\u63a7 SQL \u6267\u884c\u6307\u6807<\/li>\n<li>APM \u5de5\u5177&#xff08;SkyWalking \u7b49&#xff09;&#xff1a;\u5e94\u7528\u4fa7\u4e0a\u62a5\u6162 SQL<\/li>\n<li>explain&#xff1a;\u62ff\u5230 SQL \u5206\u6790\u6267\u884c\u8ba1\u5212&#xff0c;\u770b type\u3001key\u3001rows\u3001Extra<\/li>\n<h3>\u5e38\u89c1\u8bef\u533a<\/h3>\n<p>\u274c\u6162 SQL \u5c31\u662f\u6267\u884c\u65f6\u95f4\u5f88\u957f\u7684 SQL<\/p>\n<p>\u9519&#xff0c;\u5b58\u5728\u5927\u91cf\u9690\u5f62\u6162 SQL&#xff1a;\u8017\u65f6\u77ed&#xff0c;\u4f46\u626b\u63cf\u884c\u6570\u5de8\u5927\u3002<\/p>\n<p>\u274c\u6ca1\u51fa\u73b0\u5728\u6162\u67e5\u8be2\u65e5\u5fd7&#xff0c;\u5c31\u4e0d\u662f\u6162 SQL<\/p>\n<p>\u9608\u503c\u8bbe\u7f6e\u8fc7\u9ad8&#xff0c;\u9690\u5f62\u6162 SQL \u4e0d\u4f1a\u843d\u65e5\u5fd7\u3002<\/p>\n<p>\u274c\u00a0explain \u770b\u5230 type \u4e0d\u662f ALL \u5c31\u6ca1\u95ee\u9898<\/p>\n<p>range \u4e5f\u53ef\u80fd\u626b\u63cf\u51e0\u5341\u4e07\u884c&#xff0c;\u4f9d\u7136\u662f\u6162 SQL\u3002<\/p>\n<p>\u274c\u6d4b\u8bd5\u73af\u5883\u8dd1\u5f88\u5feb&#xff0c;\u7ebf\u4e0a\u5c31\u662f\u6162<\/p>\n<p>\u6d4b\u8bd5\u5e93\u6570\u636e\u91cf\u5c0f&#xff0c;\u770b\u4e0d\u51fa\u5168\u8868\u626b\u63cf\u95ee\u9898&#xff1b;\u7ebf\u4e0a\u771f\u5b9e\u6570\u636e\u91cf\u624d\u4f1a\u66b4\u9732\u3002<\/p>\n<p>\u7b80\u5355\u4f8b\u5b50<\/p>\n<p>&#8212; \u574f&#xff1a;name\u65e0\u7d22\u5f15&#xff0c;\u5168\u8868\u626b\u63cf\u767e\u4e07\u884c<br \/>\nselect * from user where name &#061; &#039;\u5f20\u4e09&#039;;<\/p>\n<p>&#8212; \u574f&#xff1a;\u5927\u504f\u79fb\u5206\u9875&#xff0c;\u626b\u63cf\u524d\u976210\u4e07\u884c<br \/>\nselect id,name from order limit 100000,10;<\/p>\n<p>&#8212; \u574f&#xff1a;\u7d22\u5f15\u5217\u4e0a\u7528\u51fd\u6570&#xff0c;\u7d22\u5f15\u5931\u6548<br \/>\nselect * from user where DATE(create_time)&#061;&#039;2026\u201108\u201127&#039;;<\/p>\n<p>\u6162 SQL \u548c\u6162\u67e5\u8be2\u65e5\u5fd7\u7684\u533a\u522b<\/p>\n<ul>\n<li>\u6162 SQL&#xff1a;\u4f4e\u6548 SQL \u8bed\u53e5\u672c\u8eab<\/li>\n<li>\u6162\u67e5\u8be2\u65e5\u5fd7&#xff1a;MySQL \u63d0\u4f9b\u8bb0\u5f55\u6162 SQL \u7684\u65e5\u5fd7\u6587\u4ef6&#xff0c;\u662f\u53d1\u73b0\u6162 SQL \u7684\u624b\u6bb5\u3002<\/li>\n<\/ul>\n<h2>\u4e8c\u3001\u6162 SQL\u65e5\u5fd7<\/h2>\n<h3>1. \u6838\u5fc3\u53c2\u6570<\/h3>\n<table>\n<tr>\u53c2\u6570\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>slow_query_log<\/td>\n<td>\u662f\u5426\u5f00\u542f\u6162\u67e5\u8be2\u65e5\u5fd7&#xff1a;ON\u5f00\u542f&#xff0c;OFF\u5173\u95ed&#xff0c;\u9ed8\u8ba4 OFF<\/td>\n<\/tr>\n<tr>\n<td>slow_query_log_file<\/td>\n<td>\u6162\u65e5\u5fd7\u6587\u4ef6\u8def\u5f84&#xff0c;\u4f8b\u5982 \/var\/lib\/mysql\/mysql-slow.log<\/td>\n<\/tr>\n<tr>\n<td>long_query_time<\/td>\n<td>\u6162\u67e5\u8be2\u9608\u503c&#xff0c;\u5355\u4f4d\u79d2&#xff0c;SQL \u6267\u884c\u65f6\u95f4\u8d85\u8fc7\u8be5\u503c\u5c31\u8bb0\u5f55&#xff1b;\u9ed8\u8ba4 10 \u79d2&#xff0c;\u53ef\u4ee5\u8bbe\u7f6e\u5c0f\u6570\u59820.5\u4ee3\u8868 500ms<\/td>\n<\/tr>\n<tr>\n<td>log_queries_not_using_indexes<\/td>\n<td>\u8bb0\u5f55\u6ca1\u6709\u4f7f\u7528\u7d22\u5f15\u7684 SQL&#xff0c;\u5373\u4f7f\u6267\u884c\u65f6\u95f4\u6ca1\u8fbe\u5230 long_query_time&#xff0c;\u751f\u4ea7\u8c28\u614e\u5f00\u542f&#xff0c;\u5927\u5e93\u4f1a\u4ea7\u751f\u5927\u91cf\u65e5\u5fd7<\/td>\n<\/tr>\n<tr>\n<td>log_throttle_queries_not_using_indexes<\/td>\n<td>\u9650\u5236\u6bcf\u5206\u949f\u8bb0\u5f55\u65e0\u7d22\u5f15 SQL \u7684\u6761\u6570&#xff0c;\u9632\u6b62\u65e5\u5fd7\u7206\u76d8&#xff0c;\u914d\u5408\u4e0a\u9762\u53c2\u6570\u4f7f\u7528<\/td>\n<\/tr>\n<tr>\n<td>log_output<\/td>\n<td>\u65e5\u5fd7\u8f93\u51fa\u65b9\u5f0f&#xff1a;FILE\u6587\u4ef6&#xff1b;TABLE\u5b58\u5165 mysql.slow_log \u8868&#xff1b;FILE,TABLE\u4e24\u8005\u540c\u65f6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6ce8\u610f&#xff1a;long_query_time\u7edf\u8ba1\u7684\u662f\u5b9e\u9645\u6267\u884c\u65f6\u95f4&#xff0c;\u4e0d\u5305\u542b\u9501\u7b49\u5f85\u65f6\u95f4\u3002SQL \u547d\u4e2d\u6162\u67e5\u8be2\u6761\u4ef6\u624d\u5199\u5165\u65e5\u5fd7\u3002<\/p>\n<h3>2. \u5f00\u542f\u6162\u67e5\u8be2<\/h3>\n<p>\u65b9\u5f0f 1&#xff1a;\u4e34\u65f6\u5f00\u542f&#xff08;\u91cd\u542f MySQL \u5931\u6548&#xff09;<\/p>\n<p>set global slow_query_log &#061; ON;<br \/>\nset global long_query_time &#061; 1; &#8212; \u8d85\u8fc71\u79d2\u5c31\u8bb0\u5f55<br \/>\nset global log_queries_not_using_indexes &#061; OFF;<\/p>\n<p>global \u4fee\u6539\u540e&#xff0c;\u5df2\u5efa\u7acb\u7684\u4f1a\u8bdd\u4e0d\u751f\u6548&#xff0c;\u9700\u8981\u91cd\u65b0\u8fde\u63a5\u6570\u636e\u5e93\u3002<\/p>\n<p>\u65b9\u5f0f 2&#xff1a;\u6c38\u4e45\u914d\u7f6e my.cnf\/my.ini<\/p>\n<p>[mysqld]<br \/>\nslow_query_log &#061; 1<br \/>\nslow_query_log_file &#061; \/var\/lib\/mysql\/mysql-slow.log<br \/>\nlong_query_time &#061; 1<br \/>\nlog_queries_not_using_indexes &#061; 0<br \/>\nlog_output &#061; FILE<\/p>\n<p>\u26a0\ufe0f&#xff1a;\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u9700\u8981\u91cd\u542f MySQL \u670d\u52a1\u3002<\/p>\n<p>\u67e5\u770b\u5f53\u524d\u914d\u7f6e<\/p>\n<p>show variables like &#039;%slow_query%&#039;;<br \/>\nshow variables like &#039;long_query_time&#039;;<\/p>\n<h3>3. \u6162\u67e5\u8be2\u65e5\u5fd7\u6587\u4ef6\u5185\u5bb9\u89e3\u8bfb<\/h3>\n<p>\u793a\u4f8b\u65e5\u5fd7\u7247\u6bb5&#xff1a;<\/p>\n<p># Time: 260827 10:20:30<br \/>\n# User&#064;Host: root[root] &#064; localhost []  Id:    12<br \/>\n# Query_time: 2.345678  Lock_time: 0.000123 Rows_sent: 100  Rows_examined: 100000<br \/>\nSET timestamp&#061;1787826030;<br \/>\nselect * from big_table where name&#061;&#039;xxx&#039;;<\/p>\n<p>\u5b57\u6bb5\u91ca\u4e49&#xff1a;<\/p>\n<li>Query_time&#xff1a;SQL \u5b9e\u9645\u6267\u884c\u8017\u65f6 2.34s&#xff08;\u6838\u5fc3\u6307\u6807&#xff09;<\/li>\n<li>Lock_time&#xff1a;\u7b49\u5f85\u9501\u7684\u65f6\u95f4<\/li>\n<li>Rows_sent&#xff1a;\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u884c\u6570<\/li>\n<li>Rows_examined&#xff1a;\u626b\u63cf\u7684\u884c\u6570&#xff0c;\u8fd9\u4e2a\u503c\u5f88\u5927\u4ee3\u8868\u626b\u63cf\u5927\u91cf\u6570\u636e&#xff0c;\u662f\u4f18\u5316\u91cd\u70b9<\/li>\n<li>SET timestamp&#xff1a;SQL \u6267\u884c\u65f6\u95f4\u6233<\/li>\n<p>\u91cd\u70b9\u770b&#xff1a;Query_time \u770b\u6162&#xff1b;Rows_examined\u770b\u626b\u63cf\u91cf&#xff0c;\u5f88\u591a SQL \u8017\u65f6\u4e0d\u957f&#xff0c;\u4f46\u626b\u63cf\u767e\u4e07\u884c&#xff0c;\u672a\u6765\u6570\u636e\u91cf\u4e0a\u6da8\u5c31\u4f1a\u53d8\u5361\u3002<\/p>\n<h3>4. \u5206\u6790\u5de5\u5177&#xff1a;mysqldumpslow<\/h3>\n<p>MySQL \u81ea\u5e26\u5de5\u5177&#xff0c;\u89e3\u6790\u6162\u65e5\u5fd7&#xff0c;\u6c47\u603b\u7edf\u8ba1&#xff0c;\u4e0d\u7528\u8089\u773c\u770b\u5e9e\u5927\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<h4>\u5e38\u7528\u547d\u4ee4<\/h4>\n<p># \u67e5\u770b\u5e2e\u52a9<br \/>\nmysqldumpslow &#8211;help<\/p>\n<p># \u6309\u7167\u67e5\u8be2\u65f6\u95f4\u6392\u5e8f&#xff0c;\u53d6\u524d10\u6761\u6162SQL<br \/>\nmysqldumpslow -s t -t 10 \/var\/lib\/mysql\/mysql-slow.log<\/p>\n<p># \u6309\u7167\u626b\u63cf\u884c\u6570\u6392\u5e8f&#xff08;\u770b\u626b\u63cf\u91cf\u5927\u7684SQL&#xff09;<br \/>\nmysqldumpslow -s r -t 10 mysql-slow.log<\/p>\n<p># \u5339\u914d\u5305\u542bselect\u7684\u8bed\u53e5<br \/>\nmysqldumpslow -s t -g &#034;select&#034; mysql-slow.log<\/p>\n<p>\u53c2\u6570\u8bf4\u660e&#xff1a;<\/p>\n<ul>\n<li>-s&#xff1a;\u6392\u5e8f\u65b9\u5f0f\n<ul>\n<li>t&#xff1a;Query_time \u67e5\u8be2\u65f6\u95f4<\/li>\n<li>r&#xff1a;Rows_sent \u8fd4\u56de\u884c\u6570<\/li>\n<li>a&#xff1a;\u5e73\u5747\u67e5\u8be2\u65f6\u95f4<\/li>\n<\/ul>\n<\/li>\n<li>-t&#xff1a;top N&#xff0c;\u8f93\u51fa\u591a\u5c11\u6761<\/li>\n<li>-g&#xff1a;\u6b63\u5219\u8fc7\u6ee4 SQL<\/li>\n<\/ul>\n<p>mysqldumpslow \u4f1a\u628a\u503c\u66ff\u6362\u6210N\u3001S\u505a\u805a\u5408&#xff0c;\u76f8\u540c\u7ed3\u6784\u4e0d\u540c\u53c2\u6570\u7684 SQL \u5408\u5e76\u7edf\u8ba1&#xff0c;\u65b9\u4fbf\u770b\u9ad8\u9891\u6162 SQL\u3002<\/p>\n<h4>\u7b2c\u4e09\u65b9\u5de5\u5177<\/h4>\n<ul>\n<li>pt-query-digest&#xff08;percona toolkit&#xff09;&#xff1a;\u5de5\u4e1a\u7ea7\u6162\u65e5\u5fd7\u5206\u6790&#xff0c;\u6bd4 mysqldumpslow \u5f3a\u5927&#xff0c;\u652f\u6301\u5206\u6790\u6587\u4ef6\u3001binlog\u3001general log&#xff0c;\u8f93\u51fa\u62a5\u544a\u5305\u542b\u5360\u6bd4\u3001\u6b21\u6570\u3001\u5e73\u5747\u8017\u65f6\u3002\u751f\u4ea7\u73af\u5883\u6700\u5e38\u7528\u3002<\/li>\n<\/ul>\n<h3>5. \u751f\u4ea7\u6700\u4f73\u5b9e\u8df5<\/h3>\n<li>\u9608\u503c\u8bbe\u7f6e&#xff1a;\u7ebf\u4e0a\u5efa\u8bae long_query_time&#061;1&#xff08;1 \u79d2&#xff09;&#xff0c;\u4e1a\u52a1\u654f\u611f\u53ef\u4ee5\u8c03\u5230 0.5 \u79d2&#xff1b;\u4e0d\u8981\u8bbe\u7f6e 0&#xff0c;\u4f1a\u8bb0\u5f55\u5168\u90e8 SQL&#xff0c;IO \u538b\u529b\u5de8\u5927\u3002<\/li>\n<li>log_queries_not_using_indexes&#xff1a;\u751f\u4ea7\u73af\u5883\u4e0d\u5efa\u8bae\u5e38\u5f00&#xff0c;\u538b\u6d4b\u3001\u6392\u67e5\u9636\u6bb5\u4e34\u65f6\u6253\u5f00\u5373\u53ef\u3002<\/li>\n<li>\u65e5\u5fd7\u6587\u4ef6\u8981\u505a\u8f6e\u8f6c\u5207\u5272&#xff0c;\u9632\u6b62\u65e5\u5fd7\u5360\u6ee1\u78c1\u76d8&#xff0c;\u914d\u5408 logrotate\u3002<\/li>\n<li>\u6162\u67e5\u8be2\u65e5\u5fd7\u53ea\u505a\u53d1\u73b0\u95ee\u9898&#xff0c;\u62ff\u5230 SQL \u540e\u4e00\u5b9a\u8981\u7528 explain \u5206\u6790\u6267\u884c\u8ba1\u5212&#xff0c;\u5b9a\u4f4d\u7d22\u5f15\u3001\u5168\u8868\u626b\u63cf\u95ee\u9898\u3002<\/li>\n<li>\u4e0d\u8981\u957f\u671f\u5f00\u542f\u8f93\u51fa\u5230 TABLE \u6a21\u5f0f&#xff0c;mysql.slow_log\u8868\u6570\u636e\u91cf\u5927\u4e5f\u4f1a\u5f71\u54cd\u6027\u80fd\u3002<\/li>\n<h3>6. \u5e38\u89c1\u8bef\u533a<\/h3>\n<li>\u274c&#xff1a;long_query_time&#061;0 \u5168\u90e8\u8bb0\u5f55&#xff0c;\u7ebf\u4e0a\u7981\u6b62&#xff0c;IO \u5f00\u9500\u5f88\u9ad8\u3002<\/li>\n<li>\u274c&#xff1a;\u53ea\u770b\u6267\u884c\u65f6\u95f4&#xff0c;\u5ffd\u7565Rows_examined&#xff1b;\u6709\u4e9b SQL \u5f88\u5feb&#xff0c;\u4f46\u626b\u63cf\u51e0\u5341\u4e07\u884c&#xff0c;\u6d41\u91cf\u4e0a\u6da8\u5c31\u4f1a\u96ea\u5d29\u3002<\/li>\n<li>\u274c&#xff1a;\u6162\u65e5\u5fd7\u8bb0\u5f55 SQL \u4e0d\u4ee3\u8868 SQL \u4e00\u5b9a\u5199\u7684\u5dee&#xff0c;\u6709\u53ef\u80fd\u662f\u670d\u52a1\u5668\u538b\u529b\u5927\u3001\u9501\u51b2\u7a81\u3001IO \u6253\u6ee1\u5bfc\u81f4\u53d8\u6162\u3002<\/li>\n<li>\u274c&#xff1a;\u4fee\u6539 global \u53d8\u91cf\u540e&#xff0c;\u5f53\u524d\u4f1a\u8bdd\u76f4\u63a5\u6d4b\u8bd5&#xff0c;\u9700\u8981\u91cd\u8fde\u4f1a\u8bdd\u65b0\u53c2\u6570\u624d\u751f\u6548\u3002<\/li>\n<h3>7. \u548c show processlist\u3001performance_schema \u7684\u533a\u522b<\/h3>\n<ul>\n<li>\u6162\u67e5\u8be2\u65e5\u5fd7&#xff1a;\u4e8b\u540e\u8bb0\u5f55&#xff0c;\u5df2\u7ecf\u8dd1\u5b8c\u7684\u6162 SQL&#xff1b;<\/li>\n<li>show processlist&#xff1a;\u5b9e\u65f6\u770b\u5f53\u524d\u6b63\u5728\u8fd0\u884c SQL&#xff1b;<\/li>\n<li>performance_schema&#xff1a;\u53ef\u4ee5\u505a\u66f4\u7ec6\u7c92\u5ea6\u76d1\u63a7&#xff0c;\u4e0d\u9700\u8981\u6587\u4ef6\u65e5\u5fd7&#xff0c;\u9002\u5408\u7ebf\u4e0a\u6301\u7eed\u76d1\u63a7\u3002<\/li>\n<\/ul>\n<h2>\u4e09\u3001\u62ff\u5230\u6162 SQL \u4e4b\u540e\u6807\u51c6\u6392\u67e5\u4f18\u5316\u6d41\u7a0b<\/h2>\n<h3>\u6574\u4f53\u6b65\u9aa4<\/h3>\n<li>\u6355\u83b7\u6162 SQL&#xff1a;\u6162\u67e5\u8be2\u65e5\u5fd7 \/performance_schema\/ APM&#xff0c;\u62ff\u5230\u5b8c\u6574 SQL \u6587\u672c<\/li>\n<li>Explain \u770b\u6267\u884c\u8ba1\u5212&#xff0c;\u5b9a\u4f4d\u6839\u56e0<\/li>\n<li>\u5206\u6790&#xff1a;\u7d22\u5f15&#xff1f;SQL \u5199\u6cd5&#xff1f;\u6570\u636e\u91cf&#xff1f;\u9501&#xff1f;\u4e8b\u52a1&#xff1f;<\/li>\n<li>\u7ed9\u51fa\u4f18\u5316\u65b9\u6848&#xff0c;\u9a8c\u8bc1\u6539\u5199 SQL<\/li>\n<li>\u4e0a\u7ebf&#xff0c;\u89c2\u5bdf\u6162\u65e5\u5fd7\u3001QPS\u3001CPU\u3001IO \u53d8\u5316<\/li>\n<h3>1. Explain \u91cd\u70b9\u770b\u54ea\u4e9b\u5b57\u6bb5<\/h3>\n<p>explain select * from t where name&#061;&#039;xxx&#039;;<\/p>\n<ul>\n<li>type&#xff1a;\u8bbf\u95ee\u7c7b\u578b&#xff0c;\u6027\u80fd\u4ece\u4f18\u5230\u52a3&#xff1a;system &gt; const &gt; eq_ref &gt; ref &gt; range &gt; index &gt; ALL\n<ul>\n<li>ALL&#xff1a;\u5168\u8868\u626b\u63cf&#xff0c;\u91cd\u70b9\u4f18\u5316<\/li>\n<li>range&#xff1a;\u8303\u56f4\u626b\u63cf&#xff0c;\u8981\u770b\u626b\u63cf\u884c\u6570\u662f\u5426\u8fc7\u5927<\/li>\n<\/ul>\n<\/li>\n<li>key&#xff1a;\u5b9e\u9645\u4f7f\u7528\u7684\u7d22\u5f15&#xff0c;NULL \u4ee3\u8868\u6ca1\u7528\u5230\u7d22\u5f15<\/li>\n<li>rows&#xff1a;\u9884\u4f30\u626b\u63cf\u884c\u6570&#xff0c;\u6570\u503c\u8d8a\u5927\u8d8a\u5371\u9669<\/li>\n<li>Extra&#xff1a;\u989d\u5916\u4fe1\u606f\n<ul>\n<li>Using filesort&#xff1a;\u6587\u4ef6\u6392\u5e8f&#xff0c;\u9700\u8981\u78c1\u76d8\u6392\u5e8f&#xff0c;\u6027\u80fd\u5dee&#xff0c;\u8981\u5efa\u7acb\u8054\u5408\u7d22\u5f15\u907f\u514d<\/li>\n<li>Using temporary&#xff1a;\u4f7f\u7528\u4e34\u65f6\u8868&#xff0c;\u5e38\u89c1 group by \/distinct&#xff0c;\u5f00\u9500\u5927<\/li>\n<li>Using index&#xff1a;\u8986\u76d6\u7d22\u5f15&#xff0c;\u5f88\u597d&#xff0c;\u4e0d\u9700\u8981\u56de\u8868<\/li>\n<li>Using where&#xff1a;\u5b58\u50a8\u5f15\u64ce\u8fd4\u56de\u6570\u636e\u540e&#xff0c;MySQL \u670d\u52a1\u5c42\u518d\u8fc7\u6ee4<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u26a0\ufe0f explain \u662f\u9884\u4f30&#xff0c;\u4e0d\u662f\u771f\u5b9e\u8fd0\u884c\u7ed3\u679c&#xff0c;\u4f18\u5316\u5668\u4f1a\u6839\u636e\u7edf\u8ba1\u4fe1\u606f\u9009\u62e9\u7d22\u5f15&#xff0c;\u7edf\u8ba1\u4fe1\u606f\u4e0d\u51c6\u4f1a\u5bfc\u81f4 explain \u7ed3\u679c\u548c\u771f\u5b9e\u6267\u884c\u4e0d\u4e00\u81f4\u3002<\/p>\n<h3>2. \u5bf9\u5e94\u539f\u56e0<\/h3>\n<p>\u5b9e\u6218\u793a\u4f8b<\/p>\n<p>\u5efa\u8868&#xff1a;<\/p>\n<p>CREATE TABLE &#096;user&#096; (<br \/>\n  &#096;id&#096; int NOT NULL AUTO_INCREMENT COMMENT &#039;\u4e3b\u952e&#039;,<br \/>\n  &#096;name&#096; varchar(32) DEFAULT NULL,<br \/>\n  &#096;age&#096; int DEFAULT NULL,<br \/>\n  &#096;status&#096; tinyint DEFAULT NULL,<br \/>\n  &#096;create_time&#096; datetime DEFAULT NULL,<br \/>\n  PRIMARY KEY (&#096;id&#096;),<br \/>\n  KEY &#096;idx_status_createtime&#096; (&#096;status&#096;,&#096;create_time&#096;)<br \/>\n) ENGINE&#061;InnoDB DEFAULT CHARSET&#061;utf8mb4;<\/p>\n<h4>\u793a\u4f8b 1&#xff1a;\u5168\u8868\u626b\u63cf type&#061;ALL&#xff08;\u6700\u574f&#xff09;<\/h4>\n<p>explain select * from user where name&#061;&#039;zhangsan&#039;;<\/p>\n<table>\n<tr>idselect_typetabletypepossible_keyskeykey_lenrefrowsExtra<\/tr>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>SIMPLE<\/td>\n<td>user<\/td>\n<td>ALL<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<td>10000<\/td>\n<td>Using where<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u89e3\u8bfb&#xff1a;<\/p>\n<ul>\n<li>type&#061;ALL&#xff1a;\u5168\u8868\u626b\u63cf<\/li>\n<li>possible_keys&#061;NULL&#xff1a;\u6ca1\u6709\u53ef\u9009\u7d22\u5f15<\/li>\n<li>key&#061;NULL&#xff1a;\u6ca1\u4f7f\u7528\u7d22\u5f15<\/li>\n<li>rows&#061;10000&#xff1a;\u9884\u4f30\u626b\u63cf 10000 \u884c<\/li>\n<li>Extra:Using where&#xff1a;\u5f15\u64ce\u628a\u5168\u90e8\u6570\u636e\u8fd4\u56de\u7ed9 MySQL \u670d\u52a1\u5c42&#xff0c;\u5728\u670d\u52a1\u5c42\u505aname&#061;&#039;zhangsan&#039;\u8fc7\u6ee4\u3002<\/li>\n<\/ul>\n<p>\u95ee\u9898&#xff1a;\u6ca1\u6709\u7d22\u5f15&#xff0c;\u5168\u8868\u626b\u63cf&#xff0c;\u626b\u63cf\u5168\u90e8 10000 \u884c&#xff0c;\u5728 server \u5c42\u8fc7\u6ee4\u6570\u636e\u3002 \u89e3\u51b3\u65b9\u6cd5<\/p>\n<p>&#8212; \u7ed9\u67e5\u8be2\u6761\u4ef6\u5efa\u7acb\u7d22\u5f15<br \/>\ncreate index idx_name on user(name);<\/p>\n<p>\u6ce8\u610f&#xff1a;\u5b57\u6bb5\u533a\u5206\u5ea6\u5f88\u4f4e\u4e0d\u8981\u5efa\u7d22\u5f15&#xff08;\u6bd4\u5982\u6027\u522b&#xff09;&#xff0c;\u7d22\u5f15\u6536\u76ca\u6781\u5c0f\u3002<\/p>\n<h4>\u793a\u4f8b 2&#xff1a;range \u8303\u56f4\u626b\u63cf&#xff08;\u98ce\u9669\u573a\u666f&#xff09;<\/h4>\n<p>explain select * from user where id between 1 and 8000;<\/p>\n<table>\n<tr>idselect_typetabletypepossible_keyskeykey_lenrefrowsExtra<\/tr>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>SIMPLE<\/td>\n<td>user<\/td>\n<td>range<\/td>\n<td>PRIMARY<\/td>\n<td>PRIMARY<\/td>\n<td>4<\/td>\n<td>NULL<\/td>\n<td>8000<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u95ee\u9898&#xff1a;type \u867d\u7136\u662f range&#xff0c;\u4f46\u662f\u9884\u4f30\u626b\u63cf 8000 \u884c&#xff0c;\u626b\u63cf\u6570\u636e\u91cf\u5927&#xff0c;\u4f9d\u7136\u662f\u6162 SQL\u3002range \u4e0d\u7b49\u4e8e\u5feb\u3002 \u89e3\u51b3\u65b9\u6cd5<\/p>\n<li>\u4e1a\u52a1\u5c42\u9762\u7f29\u5c0f\u67e5\u8be2\u8303\u56f4&#xff0c;\u4e0d\u8981\u4e00\u6b21\u6027\u67e5\u8fc7\u5927\u533a\u95f4&#xff1b;<\/li>\n<li>\u5982\u679c\u4e0d\u9700\u8981\u5168\u90e8\u5b57\u6bb5&#xff0c;\u4f7f\u7528\u8986\u76d6\u7d22\u5f15\u51cf\u5c11\u56de\u8868&#xff1b;<\/li>\n<li>\u5927\u6570\u636e\u91cf\u5206\u9875\u4f7f\u7528\u4e66\u7b7e\u5206\u9875 id&gt;xxx limit &#8230;&#xff0c;\u907f\u514d\u5927\u8303\u56f4 between\u3002<\/li>\n<p>&#8212; \u4e66\u7b7e\u5206\u9875\u793a\u4f8b<br \/>\nselect id,name from user where id&gt;100 limit 10;<\/p>\n<h4>\u793a\u4f8b 3&#xff1a;Using filesort \u6587\u4ef6\u6392\u5e8f<\/h4>\n<p>\u5220\u9664 idx_status_createtime \u7d22\u5f15\u540e\u6267\u884c<\/p>\n<p>explain select * from user where status&#061;1 order by create_time desc;<\/p>\n<table>\n<tr>idselect_typetabletypepossible_keyskeykey_lenrefrowsExtra<\/tr>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>SIMPLE<\/td>\n<td>user<\/td>\n<td>ALL<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<td>10000<\/td>\n<td>Using where; Using filesort<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u95ee\u9898&#xff1a;\u67e5\u8be2\u51fa\u6ee1\u8db3 where \u7684\u6570\u636e\u4e4b\u540e&#xff0c;MySQL \u989d\u5916\u505a\u6392\u5e8f\u64cd\u4f5c&#xff0c;\u5185\u5b58\u4e0d\u591f\u5c31\u843d\u5230\u78c1\u76d8\u6587\u4ef6\u6392\u5e8f&#xff0c;\u6027\u80fd\u5dee\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6cd5<\/p>\n<p>\u5efa\u7acb\u8054\u5408\u7d22\u5f15&#xff1a;where \u8fc7\u6ee4\u5b57\u6bb5\u653e\u524d\u9762&#xff0c;order by \u6392\u5e8f\u5b57\u6bb5\u7d27\u8ddf\u5176\u540e\u3002<\/p>\n<p>create index idx_status_createtime on user(status,create_time);<\/p>\n<p>\u5efa\u7acb\u7d22\u5f15\u540e&#xff0c;\u7d22\u5f15\u672c\u8eab\u5df2\u7ecf\u6709\u5e8f&#xff0c;\u4e0d\u9700\u8981 filesort\u3002<\/p>\n<p>\u6ce8\u610f&#xff1a;\u6392\u5e8f\u65b9\u5411\u8981\u4e00\u81f4&#xff0c;\u4e0d\u80fd\u4e00\u4e2a asc \u4e00\u4e2a desc&#xff0c;\u4f1a\u5931\u6548\u3002<\/p>\n<hr \/>\n<h4>\u793a\u4f8b 4&#xff1a;Using temporary \u4e34\u65f6\u8868<\/h4>\n<p>status \u65e0\u7d22\u5f15&#xff1a;<\/p>\n<p>explain select status,count(*) from user group by status;<\/p>\n<p>\u8868\u683c<\/p>\n<table>\n<tr>idselect_typetabletypepossible_keyskeykey_lenrefrowsExtra<\/tr>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>SIMPLE<\/td>\n<td>user<\/td>\n<td>ALL<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<td>NULL<\/td>\n<td>10000<\/td>\n<td>Using temporary; Using filesort<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u95ee\u9898&#xff1a;group by \/distinct \u9700\u8981\u521b\u5efa\u4e34\u65f6\u8868\u5b58\u50a8\u4e2d\u95f4\u5206\u7ec4\u7ed3\u679c&#xff0c;\u5f00\u9500\u9ad8\u3002 \u89e3\u51b3\u65b9\u6cd5<\/p>\n<p>&#8212; group by\u7684\u5b57\u6bb5\u5efa\u7acb\u7d22\u5f15<br \/>\ncreate index idx_status on user(status);<\/p>\n<p>\u7d22\u5f15\u6709\u5e8f&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u5206\u7ec4&#xff0c;\u4e0d\u9700\u8981\u4e34\u65f6\u8868\u3002<\/p>\n<p>\u5982\u679c\u4e1a\u52a1\u65e0\u6cd5\u5efa\u7d22\u5f15&#xff1a;\u51cf\u5c11 group by \u5206\u7ec4\u57fa\u6570&#xff0c;\u4e1a\u52a1\u5185\u5b58\u505a\u805a\u5408&#xff0c;\u4e0d\u8981\u6570\u636e\u5e93\u5927\u8868\u5206\u7ec4\u3002<\/p>\n<h4>\u793a\u4f8b 5&#xff1a;Using index \u2705\u8986\u76d6\u7d22\u5f15&#xff08;\u4f18\u79c0&#xff09;<\/h4>\n<p>explain select status,create_time from user where status&#061;1;<\/p>\n<table>\n<tr>idselect_typetabletypepossible_keyskeykey_lenrefrowsExtra<\/tr>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>SIMPLE<\/td>\n<td>user<\/td>\n<td>ref<\/td>\n<td>idx_status_createtime<\/td>\n<td>idx_status_createtime<\/td>\n<td>2<\/td>\n<td>const<\/td>\n<td>500<\/td>\n<td>Using index<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8bf4\u660e&#xff1a;\u67e5\u8be2\u5b57\u6bb5\u5168\u90e8\u5305\u542b\u5728\u7d22\u5f15\u4e2d&#xff0c;\u4e0d\u9700\u8981\u56de\u8868\u67e5\u8be2\u4e3b\u952e\u6570\u636e&#xff0c;\u6027\u80fd\u5f88\u9ad8\u3002<\/p>\n<p>\u5982\u4f55\u5b9e\u73b0\u8986\u76d6\u7d22\u5f15 select \u540e\u9762\u67e5\u8be2\u7684\u5217&#xff0c;\u5168\u90e8\u653e\u5230\u8054\u5408\u7d22\u5f15\u91cc\u3002<\/p>\n<p>&#8212; \u67e5\u8be2 status,create_time&#xff0c;\u7d22\u5f15\u5305\u542b\u8fd9\u4e24\u4e2a\u5b57\u6bb5<br \/>\nKEY &#096;idx_status_createtime&#096; (&#096;status&#096;,&#096;create_time&#096;)<\/p>\n<p>\u274c\u4e0d\u8981 select *&#xff0c;\u4f1a\u7834\u574f\u8986\u76d6\u7d22\u5f15&#xff0c;\u89e6\u53d1\u56de\u8868\u3002<\/p>\n<h4>\u793a\u4f8b 6&#xff1a;possible_keys \u6709\u7d22\u5f15&#xff0c;key \u4e3a NULL&#xff08;\u6709\u7d22\u5f15\u4f46\u662f\u4e0d\u8d70&#xff09;<\/h4>\n<p>\u573a\u666f&#xff1a;\u7d22\u5f15\u533a\u5206\u5ea6\u5dee\u3002status \u53ea\u6709 0 \u548c 1 \u4e24\u4e2a\u503c\u3002<\/p>\n<p>explain select * from user where status&#061;1;<\/p>\n<p>possible_keys \u663e\u793aidx_status&#xff0c;key&#061;null&#xff0c;type&#061;ALL\u3002<\/p>\n<p>\u95ee\u9898&#xff1a;\u4f18\u5316\u5668\u5224\u65ad\u8d70\u7d22\u5f15\u56de\u8868\u4ee3\u4ef7\u9ad8\u4e8e\u5168\u8868\u626b\u63cf&#xff0c;\u653e\u5f03\u7d22\u5f15\u76f4\u63a5\u5168\u8868\u626b\u63cf\u3002 \u89e3\u51b3\u65b9\u6cd5<\/p>\n<li>\u4f4e\u533a\u5206\u5ea6\u5b57\u6bb5\u4e0d\u9002\u5408\u5355\u72ec\u5efa\u7d22\u5f15&#xff1b;<\/li>\n<li>\u8c03\u6574\u4e1a\u52a1&#xff0c;\u589e\u52a0\u8fc7\u6ee4\u6761\u4ef6&#xff1b;<\/li>\n<li>\u53ef\u4ee5\u5f3a\u5236\u7d22\u5f15&#xff08;\u4ec5\u4e34\u65f6\u9a8c\u8bc1&#xff0c;\u751f\u4ea7\u614e\u7528&#xff09;<\/li>\n<p>select * from user force index(idx_status) where status&#061;1;<\/p>\n<h4>\u793a\u4f8b 7&#xff1a;\u7d22\u5f15\u5931\u6548&#xff08;\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362&#xff09;<\/h4>\n<p>name \u662f varchar \u5b57\u7b26\u4e32\u5b57\u6bb5&#xff0c;\u67e5\u8be2\u4f20\u5165\u6570\u5b57<\/p>\n<p>explain select * from user where name&#061;1234;<\/p>\n<p>\u5373\u4f7f\u6709idx_name\u7d22\u5f15&#xff0c;key \u4f9d\u65e7\u4e3a NULL&#xff0c;type&#061;ALL\u3002 \u539f\u56e0&#xff1a;\u5b57\u6bb5\u53d1\u751f\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362&#xff0c;\u7d22\u5f15\u5931\u6548\u3002 \u89e3\u51b3\u65b9\u6cd5&#xff1a;\u4fdd\u6301\u7c7b\u578b\u4e00\u81f4<\/p>\n<p>select * from user where name&#061;&#039;1234&#039;;<\/p>\n<h4>\u793a\u4f8b 8&#xff1a;type&#061;index&#xff08;\u626b\u63cf\u6574\u4e2a\u7d22\u5f15\u6811&#xff09;<\/h4>\n<p>explain select name from user;<\/p>\n<p>type&#061;index&#xff0c;Extra \u4e3a\u7a7a\u3002<\/p>\n<p>type&#061;index&#xff1a;\u904d\u5386\u6574\u4e2a\u4e8c\u7ea7\u7d22\u5f15\u6811&#xff0c;\u4e0d\u662f\u5168\u8868&#xff0c;\u4f46\u626b\u63cf\u5927\u91cf\u7d22\u5f15\u9875&#xff0c;\u6027\u80fd\u5dee&#xff1b;\u4e0d\u8981\u548cUsing index\u6df7\u6dc6\u3002 \u89e3\u51b3\u65b9\u6cd5&#xff1a;\u4e1a\u52a1\u52a0 where \u6761\u4ef6&#xff0c;\u4e0d\u8981\u4e0d\u52a0\u6761\u4ef6\u67e5\u8be2\u5927\u91cf\u6570\u636e\u3002<\/p>\n<h3>Extra \u5b57\u6bb5\u901f\u67e5\u8868<\/h3>\n<p>\u8868\u683c<\/p>\n<table>\n<tr>Extra\u73b0\u8c61\u5904\u7406\u65b9\u6848<\/tr>\n<tbody>\n<tr>\n<td>Using where<\/td>\n<td>\u5b58\u50a8\u5f15\u64ce\u8fd4\u56de\u6570\u636e&#xff0c;\u670d\u52a1\u5c42\u8fc7\u6ee4<\/td>\n<td>\u642d\u914d type&#061;ALL \u624d\u9700\u8981\u4f18\u5316<\/td>\n<\/tr>\n<tr>\n<td>Using filesort<\/td>\n<td>\u6587\u4ef6\u6392\u5e8f<\/td>\n<td>\u5efa\u7acb\u8054\u5408\u7d22\u5f15&#xff0c;where \u6761\u4ef6 &#043; order by \u5b57\u6bb5<\/td>\n<\/tr>\n<tr>\n<td>Using temporary<\/td>\n<td>\u4e34\u65f6\u8868<\/td>\n<td>group by\/distinct \u5b57\u6bb5\u5efa\u7d22\u5f15<\/td>\n<\/tr>\n<tr>\n<td>Using index<\/td>\n<td>\u8986\u76d6\u7d22\u5f15<\/td>\n<td>\u2705\u4f18\u79c0&#xff0c;\u5c3d\u91cf\u505a\u5230<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><\/h3>\n<h3>3. \u533a\u5206&#xff1a;\u662f SQL \u672c\u8eab\u6162&#xff0c;\u8fd8\u662f\u9501\u7b49\u5f85\u6162<\/h3>\n<p>\u770b\u6162\u65e5\u5fd7\u91cc Lock_time&#xff1a;<\/p>\n<ul>\n<li>Lock_time \u5927&#xff0c;Query_time \u4e0d\u5927&#xff1a;SQL \u672c\u8eab\u5f88\u5feb&#xff0c;\u5927\u91cf\u65f6\u95f4\u5728\u7b49\u9501&#xff0c;\u5c5e\u4e8e\u9501\u51b2\u7a81\u95ee\u9898&#xff0c;\u4e0d\u662f SQL \u7d22\u5f15\u95ee\u9898\u3002 \u6392\u67e5\u65b9\u5411&#xff1a;\u5927\u4e8b\u52a1\u3001\u957f\u4e8b\u52a1&#xff0c;\u884c\u9501\u51b2\u7a81\u3002<\/li>\n<li>Lock_time \u5f88\u5c0f&#xff0c;Query_time \u5f88\u5927&#xff1a;SQL \u6267\u884c\u672c\u8eab\u6162&#xff0c;\u4f18\u5148\u4f18\u5316\u7d22\u5f15\u548c SQL \u5199\u6cd5\u3002<\/li>\n<\/ul>\n<h3>4. \u4f18\u5316\u540e\u9a8c\u8bc1<\/h3>\n<li>explain \u786e\u8ba4\u6267\u884c\u8ba1\u5212\u6539\u5584<\/li>\n<li>\u7ebf\u4e0a\u7070\u5ea6\u89c2\u5bdf&#xff1a;\u6162\u65e5\u5fd7\u4e0d\u518d\u51fa\u73b0\u8fd9\u6761 SQL&#xff0c;\u6570\u636e\u5e93 CPU\u3001IO \u4e0b\u964d&#xff0c;\u54cd\u5e94\u65f6\u95f4\u4e0b\u964d<\/li>\n<li>\u5173\u6ce8\u8fb9\u754c&#xff1a;\u6570\u636e\u589e\u957f\u540e\u4f1a\u4e0d\u4f1a\u518d\u6b21\u6076\u5316<\/li>\n<h3><\/h3>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u62ff\u5230 explain \u6267\u884c\u8ba1\u5212&#xff1a;<\/p>\n<li>\u770btype&#xff0c;\u4f18\u5148\u4fdd\u8bc1\u81f3\u5c11\u8fbe\u5230 range\/ref \u7ea7\u522b&#xff0c;\u907f\u514d ALL&#xff1b;<\/li>\n<li>\u770bkey\u786e\u8ba4\u662f\u5426\u771f\u6b63\u4f7f\u7528\u7d22\u5f15&#xff0c;possible_keys \u6709\u7d22\u5f15\u4e0d\u7b49\u4e8e\u5b9e\u9645\u4f7f\u7528&#xff1b;<\/li>\n<li>\u770brows\u9884\u4f30\u626b\u63cf\u884c\u6570&#xff0c;\u884c\u6570\u8fc7\u5927\u5373\u4f7f type \u4e0d\u9519\u4e5f\u662f\u98ce\u9669 SQL&#xff1b;<\/li>\n<li>\u770b Extra&#xff0c;\u51fa\u73b0Using filesort\u3001Using temporary\u9700\u8981\u4f18\u5316&#xff1b;<\/li>\n<li>\u5224\u65ad\u662f\u7f3a\u5c11\u7d22\u5f15\u3001\u7d22\u5f15\u5931\u6548\u3001\u4f18\u5316\u5668\u653e\u5f03\u7d22\u5f15&#xff1b;\u9488\u5bf9\u6027\u5efa\u7d22\u5f15\u3001\u6539\u5199 SQL&#xff1b;<\/li>\n<li>\u4f18\u5316\u5b8c\u518d\u6b21 explain \u9a8c\u8bc1\u3002<\/li>\n<p>\u6ce8\u610f&#xff1a;rows \u662f\u9884\u4f30\u503c&#xff0c;MySQL \u7edf\u8ba1\u4fe1\u606f\u4e0d\u51c6&#xff0c;explain \u7ed3\u679c\u4e0d\u7b49\u4e8e\u771f\u5b9e\u6267\u884c\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u5ff5\u6162\u67e5\u8be2\u65e5\u5fd7&#xff08;slow query log&#xff09;\u662f MySQL \u8bb0\u5f55\u6267\u884c\u65f6\u95f4\u8d85\u8fc7\u6307\u5b9a\u9608\u503cSQL \u7684\u65e5\u5fd7&#xff0c;\u7528\u6765\u5b9a\u4f4d\u6162 SQL\u3001\u6027\u80fd\u74f6\u9888&#xff0c;\u662f SQL \u4f18\u5316\u6700\u57fa\u7840\u7684\u5de5\u5177\u3002\u4e00\u3001\u6162 SQL\u6162 SQL \u4e0d\u662f\u8bed\u6cd5\u9519\u8bef\u7684 SQL&#xff0c;\u662f\u6267\u884c\u6548\u7387\u4f4e\u4e0b&#xff0c;\u6d88\u8017\u5927\u91cf\u6570\u636e\u5e93\u8d44\u6e90&#xff08;CPU\u3001IO\u3001\u9501&#xff09;&#xff0c;\u6267\u884c\u8017\u65f6\u8f83\u957f\u7684 SQL \u8bed\u53e5\u3002 \u7b80\u5355\u8bf4&#xff1a;\u8dd1\u7684\u6162\u3001\u8017\u8d44\u6e90&#xff0c;\u62d6<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10187,6404,10955,114,3061,100],"topic":[],"class_list":["post-97702","post","type-post","status-publish","format-standard","hentry","category-server","tag-codingplan","tag-sql","tag-10955","tag-mysql","tag-100"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u8be6\u89e3MySQL \u6162\u67e5\u8be2\u65e5\u5fd7( \u9644\u8be6\u7ec6\u6848\u4f8b ) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/97702.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u8be6\u89e3MySQL \u6162\u67e5\u8be2\u65e5\u5fd7( \u9644\u8be6\u7ec6\u6848\u4f8b ) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6982\u5ff5\u6162\u67e5\u8be2\u65e5\u5fd7&#xff08;slow query log&#xff09;\u662f MySQL \u8bb0\u5f55\u6267\u884c\u65f6\u95f4\u8d85\u8fc7\u6307\u5b9a\u9608\u503cSQL \u7684\u65e5\u5fd7&#xff0c;\u7528\u6765\u5b9a\u4f4d\u6162 SQL\u3001\u6027\u80fd\u74f6\u9888&#xff0c;\u662f SQL \u4f18\u5316\u6700\u57fa\u7840\u7684\u5de5\u5177\u3002\u4e00\u3001\u6162 SQL\u6162 SQL \u4e0d\u662f\u8bed\u6cd5\u9519\u8bef\u7684 SQL&#xff0c;\u662f\u6267\u884c\u6548\u7387\u4f4e\u4e0b&#xff0c;\u6d88\u8017\u5927\u91cf\u6570\u636e\u5e93\u8d44\u6e90&#xff08;CPU\u3001IO\u3001\u9501&#xff09;&#xff0c;\u6267\u884c\u8017\u65f6\u8f83\u957f\u7684 SQL \u8bed\u53e5\u3002 \u7b80\u5355\u8bf4&#xff1a;\u8dd1\u7684\u6162\u3001\u8017\u8d44\u6e90&#xff0c;\u62d6\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/97702.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-29T18:00:37+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/97702.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/97702.html\",\"name\":\"\u8be6\u89e3MySQL \u6162\u67e5\u8be2\u65e5\u5fd7( \u9644\u8be6\u7ec6\u6848\u4f8b ) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-29T18:00:37+00:00\",\"dateModified\":\"2026-08-29T18:00:37+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/97702.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/97702.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/97702.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u8be6\u89e3MySQL \u6162\u67e5\u8be2\u65e5\u5fd7( \u9644\u8be6\u7ec6\u6848\u4f8b )\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u8be6\u89e3MySQL \u6162\u67e5\u8be2\u65e5\u5fd7( \u9644\u8be6\u7ec6\u6848\u4f8b ) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/97702.html","og_locale":"zh_CN","og_type":"article","og_title":"\u8be6\u89e3MySQL \u6162\u67e5\u8be2\u65e5\u5fd7( \u9644\u8be6\u7ec6\u6848\u4f8b ) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6982\u5ff5\u6162\u67e5\u8be2\u65e5\u5fd7&#xff08;slow query log&#xff09;\u662f MySQL \u8bb0\u5f55\u6267\u884c\u65f6\u95f4\u8d85\u8fc7\u6307\u5b9a\u9608\u503cSQL \u7684\u65e5\u5fd7&#xff0c;\u7528\u6765\u5b9a\u4f4d\u6162 SQL\u3001\u6027\u80fd\u74f6\u9888&#xff0c;\u662f SQL \u4f18\u5316\u6700\u57fa\u7840\u7684\u5de5\u5177\u3002\u4e00\u3001\u6162 SQL\u6162 SQL \u4e0d\u662f\u8bed\u6cd5\u9519\u8bef\u7684 SQL&#xff0c;\u662f\u6267\u884c\u6548\u7387\u4f4e\u4e0b&#xff0c;\u6d88\u8017\u5927\u91cf\u6570\u636e\u5e93\u8d44\u6e90&#xff08;CPU\u3001IO\u3001\u9501&#xff09;&#xff0c;\u6267\u884c\u8017\u65f6\u8f83\u957f\u7684 SQL \u8bed\u53e5\u3002 \u7b80\u5355\u8bf4&#xff1a;\u8dd1\u7684\u6162\u3001\u8017\u8d44\u6e90&#xff0c;\u62d6","og_url":"https:\/\/www.wsisp.com\/helps\/97702.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-29T18:00:37+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/97702.html","url":"https:\/\/www.wsisp.com\/helps\/97702.html","name":"\u8be6\u89e3MySQL \u6162\u67e5\u8be2\u65e5\u5fd7( \u9644\u8be6\u7ec6\u6848\u4f8b ) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-29T18:00:37+00:00","dateModified":"2026-08-29T18:00:37+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/97702.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/97702.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/97702.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u8be6\u89e3MySQL \u6162\u67e5\u8be2\u65e5\u5fd7( \u9644\u8be6\u7ec6\u6848\u4f8b )"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/97702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=97702"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/97702\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=97702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=97702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=97702"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=97702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}