{"id":171391,"date":"2020-10-25T08:11:23","date_gmt":"2020-10-25T00:11:23","guid":{"rendered":"http:\/\/4563.org\/?p=171391"},"modified":"2020-10-25T08:11:23","modified_gmt":"2020-10-25T00:11:23","slug":"mysql-%e6%9f%a5%e8%af%a2%e4%bd%bf%e7%94%a8%e4%b8%bb%e9%94%ae-id-%e6%8e%92%e5%ba%8f%e7%ab%9f%e7%84%b6%e6%98%af%e8%b5%b0-filesort-%e7%9a%84","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=171391","title":{"rendered":"MySQL \u67e5\u8be2\u4f7f\u7528\u4e3b\u952e id \u6392\u5e8f\u7adf\u7136\u662f\u8d70 filesort \u7684?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  MySQL \u67e5\u8be2\u4f7f\u7528\u4e3b\u952e id \u6392\u5e8f\u7adf\u7136\u662f\u8d70 filesort \u7684?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : NULL2020 <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6700\u8fd1\u4e86\u89e3\u5230\uff0cmysql \u7684\u6392\u5e8f\u4e5f\u9700\u8981\u6ee1\u8db3 [\u524d\u5bfc\u5217 \/\u6700\u5de6\u524d\u7f00] \u539f\u5219\u624d\u4f1a\u8d70\u7d22\u5f15\u6392\u5e8f\uff0c\u4e0d\u7136\u5c31\u662f filesort \u3002<\/p>\n<p>\u4e4b\u524d\u4e00\u76f4\u6ca1\u7559\u610f\u8fc7\uff0c\u5de5\u4f5c\u4e2d\u5927\u91cf\u4f7f\u7528\u4e3b\u952e id \u6392\u5e8f\uff0c\u7528 explain \u5206\u6790\u540e\u53d1\u73b0\u51e0\u4e4e\u90fd\u662f\u8d70 filesort\uff0c\u4e0d\u7ba1\u662f<\/p>\n<pre><code>select * where  a=123 order by id desc;  select id, a,b,c where  a=123 order by id desc;  <\/code><\/pre>\n<p>\u90fd\u65e0\u6cd5\u8d70\u4e3b\u952e\u7d22\u5f15\u8fdb\u884c\u6392\u5e8f\uff0cid \u662f\u81ea\u589e\u4e3b\u952e\uff0ca \u5217\u6709\u7d22\u5f15\uff0c\u67e5\u8be2\u53ef\u4ee5\u7528\u5230 a \u5217\u7684\u7d22\u5f15\uff0c\u4f46\u6392\u5e8f\u4f9d\u65e7\u8d70 filesort.<\/p>\n<p>\u6309\u7406\u8bf4\uff0c\u4e3b\u952e\u4e5f\u6709\u7d22\u5f15\uff0c\u4e3a\u4ec0\u4e48\u65e0\u6cd5\u4f7f\u7528\u7d22\u5f15\u8fdb\u884c\u6392\u5e8f\u5462\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>15<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3857165\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : OysterQAQ <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6bcf\u6b21\u67e5\u8be2\u53ea\u80fd\u547d\u4e2d\u4e00\u4e2a\u7d22\u5f15\uff0c\u89e3\u51b3\u65b9\u6cd5\uff0c\u52a0\u8054\u5408\u7d22\u5f15(a,is)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857166\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : user8341 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             a \u5217\u7684\u7d22\u5f15\u662f\u6309\u7167 a \u7684\u5927\u5c0f\u987a\u5e8f\u6392\u5217\u7684\uff0c\u4e0d\u662f\u6309\u7167 id \u7684\u5927\u5c0f\u987a\u5e8f\u6392\u5217\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857167\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : OysterQAQ <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e86\u89e3\u4e0b\u7d22\u5f15\u6392\u5e8f\u7684\u539f\u7406\u5c31\u660e\u767d\u4e86\u2026\u2026\u672c\u8eab\u7d22\u5f15\u662f\u6709\u5e8f\u7684\u624d\u53ef\u4ee5\u505a\u5230\u6392\u5e8f\uff0corder \u4e4b\u524d\u7684\u7b5b\u9009\u9879\u8fd8\u5f97\u662f=                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857168\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : NULL2020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @OysterQAQ #1 \u611f\u8c22\uff0c\u6d4b\u8bd5\u4e86\u4e0b\u786e\u5b9e\u662f\u8fd9\u6837\uff0cwhere \u91cc\u4f7f\u7528\u65e0\u7d22\u5f15\u7684\u5217\u5219\u4f1a\u8d70\u7d22\u5f15\u6392\u5e8f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857169\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : user8341 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3a\u4ec0\u4e48\u4e0d\u7528\u4e3b\u952e\u7d22\u5f15\uff1f\u56e0\u4e3a\u6ca1\u7528\u5427\u3002<\/p>\n<p>\u4f60\u6309 A \u7684\u7d22\u5f15\u627e\u5230\u4e00\u7ec4\u8bb0\u5f55\uff0c\u4ed6\u4eec\u7684 ID \u662f\u4e71\u5e8f\u7684\u3002<br \/>\u7136\u540e\u4f60\u600e\u4e48\u7528 ID \u7d22\u5f15\u53bb\u6392\u5e8f\uff1f\u6211\u662f\u60f3\u4e0d\u51fa\u6765\u6709\u4ec0\u4e48\u529e\u6cd5\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5148\u7528 ID \u7684\u7d22\u5f15\uff0c\u90a3\u4e5f\u6ca1\u7528\uff0c\u56e0\u4e3a\u4e0d\u7b26\u5408 where \u91cc\u9762\u7684\u6761\u4ef6\uff0c\u53ea\u80fd\u5168\u8868\u626b\u63cf\uff0c\u7528\u4e0d\u5230\u7d22\u5f15\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857170\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : NULL2020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @user8341 #5 1 \u5927\u4f6c\u8bf4\u7684\u662f\u6b63\u89e3\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857171\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : user8341 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @NULL2020 \u4e00\u8bf4\u7684\u6ca1\u9519\u554a\u3002\u8054\u5408\u7d22\u5f15(a, id)\u5c31\u662f\u6309\u7167 A \u4e3a\u4e3b\u8981\u5173\u952e\u5b57\uff0cid \u4e3a\u6b21\u8981\u5173\u952e\u5b57\u6392\u5e8f\u3002\u6240\u4ee5\u7528\u8fd9\u4e2a\u7d22\u5f15\uff0c\u5982\u679c\u4f60 A \u7684\u6761\u4ef6\u662f = \u7684\uff0cID \u5c31\u662f\u6709\u5e8f\u7684\u3002<br \/>\u4f46\u662f\u5982\u679c A \u6761\u4ef6\u662f&gt;, &lt;\u4e4b\u7c7b\u7684\uff0c\u6050\u6015 ID \u8fd8\u662f\u65e0\u5e8f\uff0c\u8fd8\u662f\u8981 filesort \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857172\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : user8341 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @OysterQAQ \u4e0d\u51c6\u786e\u3002\u73b0\u5728\u7684 mysql \u8fd8\u6709 Index Merge Optimization \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857173\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : OysterQAQ <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @user8341 \u5efa\u8bae\u5b9e\u9645\u4e0a\u770b\u770b\u67e5\u8be2\u8ba1\u5212\u518d\u8bf4\u5427\uff0c\u4e0d\u77e5\u9053\u4f60\u6709\u6ca1\u6709\u770b\u8fc7\u9ad8\u6027\u80fd mysql\uff0c\u91cc\u9762\u786e\u5b9e\u6709\u8bf4\u5230\u51e0\u4e2a\u5355\u5217\u7d22\u5f15\u7684\u7d22\u5f15\u5408\u5e76\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u4f60\u5e94\u8be5\u53bb\u770b\u770b\u4f60\u7684\u6267\u884c\u8ba1\u5212\uff0c\u8fd9\u4e5f\u662f\u4e66\u4e2d\u4e00\u76f4\u5f3a\u8c03\u7684\uff0c\u5728\u4e0d\u662f\u5f88\u4e86\u89e3\u5185\u90e8\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b\uff0c\u4ee5\u67e5\u8be2\u8ba1\u5212\u4e3a\u4e3b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857174\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : user8341 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @OysterQAQ \u4f60\u8bf4\u5f97\u6ca1\u9519\u3002\u4e0d\u8fc7\u5047\u5982\u67e5\u8be2\u8ba1\u5212\u80fd\u89e3\u7b54\u201d\u4e3a\u4ec0\u4e48\u201c\u7684\u95ee\u9898\uff0c\u76f8\u4fe1\u4e3b\u4e5f\u4e0d\u4f1a\u6765\u95ee\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857175\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : NULL2020 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @user8341 #10 \u4ed6\u89e3\u7b54\u4e86\u6211\u7684\u95ee\u9898\u554a\uff0c [\u4e00\u4e2a\u67e5\u8be2\u53ea\u80fd\u547d\u4e2d\u4e00\u4e2a\u7d22\u5f15] \uff0c\u6240\u4ee5\u5047\u5982 where \u6761\u4ef6\u91cc\u547d\u4e2d\u4e86\u7d22\u5f15\uff0c\u90a3\u6392\u5e8f\u5c31\u4e0d\u4f1a\u547d\u4e2d\uff0c\u53ea\u80fd filesort                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857176\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : user8341 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @NULL2020 <\/p>\n<p>\u4e00\u4e2a\u67e5\u8be2\u53ea\u80fd\u7528\u4e00\u4e2a\u7d22\u5f15\u662f\u9519\u7684\u554a\u3002\u518d\u8005\uff0c\u4f60\u600e\u4e48\u89e3\u91ca a &gt; 123 \u8fd9\u6837\u7684\u6761\u4ef6\uff0corder by id \u6392\u5e8f\u5c31\u8981 filesort \u5462\uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857177\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wakzz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3b\u4f60\u786e\u5b9a\u4f60\u4f7f\u7528\u4e86 a \u5b57\u6bb5\u7d22\u5f15\uff1f\u5982\u679c\u4f7f\u7528\u4e86 a \u5b57\u6bb5\u7d22\u5f15\uff0c\u90a3\u4e48\u6392\u5e8f\u76f4\u63a5\u4f7f\u7528 a \u7d22\u5f15\uff0c\u4e0d\u9700\u8981\u989d\u5916\u6392\u5e8f\u4e5f\u5c31\u662f\u4e0d\u9700\u8981 filesort \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857178\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wakzz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @user8341 \u7d22\u5f15\u6811\u5f53\u7d22\u5f15\u503c\u76f8\u540c\u65f6\uff0c\u662f\u4ee5\u4e3b\u952e\u6709\u5e8f\u6392\u5217\u7684\uff0c\u5e76\u4e0d\u662f\u4e71\u5e8f\u3002\u5982\u679c\u662f\u57fa\u4e8e\u7d22\u5f15\u7684\u8303\u56f4\u67e5\u8be2\u786e\u5b9e\u6574\u4f53\u4e71\u5e8f\uff0c\u4f46\u5982\u679c\u662f\u57fa\u4e8e\u7d22\u5f15\u7684\u5355\u503c\u67e5\u8be2\uff0c\u90a3\u7ed3\u679c\u672c\u8eab\u5c31\u662f\u4ee5\u4e3b\u952e\u6392\u5e8f\u7684\uff0c\u5e76\u4e0d\u662f\u4e71\u5e8f\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3857179\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : user8341 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wakzz \u4f60\u8fde\u4ed6\u539f\u5e16\u90fd\u6ca1\u4ed4\u7ec6\u770b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>MySQL \u67e5\u8be2\u4f7f\u7528\u4e3b\u952e id \u6392&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/171391"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=171391"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/171391\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=171391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=171391"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=171391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}