{"id":198246,"date":"2020-11-08T22:32:57","date_gmt":"2020-11-08T14:32:57","guid":{"rendered":"http:\/\/4563.org\/?p=198246"},"modified":"2020-11-08T22:32:57","modified_gmt":"2020-11-08T14:32:57","slug":"%e5%92%a8%e8%af%a2%e5%85%b3%e4%ba%8e-order_by-%e5%92%8c-group_by-%e7%9a%84%e4%b8%80%e4%ba%9b%e7%b4%a2%e5%bc%95%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=198246","title":{"rendered":"\u54a8\u8be2\u5173\u4e8e order_by \u548c group_by \u7684\u4e00\u4e9b\u7d22\u5f15\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u54a8\u8be2\u5173\u4e8e order_by \u548c group_by \u7684\u4e00\u4e9b\u7d22\u5f15\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SjwNo1 <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h2>\u8868\u7ed3\u6784<\/h2>\n<p>\u8868\u540d label_file_bridges<br \/> +&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/> | Field | Type | Null | Key | Default | Extra |<br \/> +&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/> | id | int(32) | NO | PRI | NULL | auto_increment |<\/p>\n<p>| label_id | int(32) | NO | MUL | NULL | |<\/p>\n<p>| file_id | int(32) | NO | | NULL | |<\/p>\n<p>| is_active | tinyint(1) | NO | | NULL | |<br \/> +&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<\/p>\n<h2>\u7d22\u5f15<\/h2>\n<p>PRIMARY id<br \/> idx_label_file_bridge_label_id_file_id_is_active (label_id, file_id, is_active)<\/p>\n<h2>sql<\/h2>\n<p>select file_id, group_concat(label_id) from label_file_bridges where label_id in (1,2,3,4,5,6,7,8,9,10) and is_active=1 group by file_id;<\/p>\n<h2>explain<\/h2>\n<p>id: 1<br \/> select_type: SIMPLE<br \/> table: label_file_bridges<br \/> partitions: NULL<br \/> type: index<br \/> possible_keys: idx_label_file_bridge_label_id_file_id_is_active<br \/> key: idx_label_file_bridge_label_id_file_id_is_active<br \/> key_len: 9<br \/> ref: NULL<br \/> rows: 48182<br \/> filtered: 10.00<br \/> Extra: Using where; Using index; Using filesort<\/p>\n<h2>\u7591\u95ee<\/h2>\n<p>\u8868\u6570\u636e\u91cf\u767e\u4e07\u7ea7\uff0c\u4ee5\u4e0a\u662f\u6d4b\u8bd5\u6570\u636e\uff0c \u4f7f\u7528\u6b64 sql \u901f\u5ea6\u8fd8\u884c\uff0c\u4f46\u662f\u5b58\u5728\u5916\u5c42\u6392\u5e8f filesort, \u8c03\u8bd5\u53d1\u73b0\u7528 in + order_by \u6216\u8005 in + group_by \u90fd\u4f1a\u6709\u8fd9\u4e2a\u60c5\u51b5 \u8bf7\u6559\u4e00\u4e0b\u8fd9\u4e2a sql \u6216\u8005\u8868\u7ed3\u6784 ( \u7d22\u5f15 ) \u6709\u53ef\u4f18\u5316\u7684\u5730\u65b9\u5417\uff0c\u6709\u53ef\u80fd\u907f\u514d filesort \u5417<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4243885\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lpts007 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u52a0 order by null \u8bd5\u8fc7\u6ca1                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4243886\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fhsan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             order by label_id, file_id, is_active<br \/>group by file_id                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4243887\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Egfly <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7d22\u5f15\u4f4d\u7f6e\u6362\u4e00\u4e0b file_id \u653e\u5728 label_id \u524d\u9762\u5c31\u884c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4243888\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : SjwNo1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @lpts007 \u8bd5\u4e86 \u6ca1\u6709\u6539\u53d8\u54e6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4243889\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : SjwNo1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @fhsan \u4e0d\u884c\u54c8\u54c8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4243890\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : SjwNo1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Egfly \u53ef\u4ee5\u5566\u54c8\u54c8\uff0c\u539f\u56e0\u662f\u4ec0\u4e48\u5462                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4243891\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Egfly <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @SjwNo1 <\/p>\n<p>\u4e3b\u8981\u662f in \u5bfc\u81f4\u7684\u3002\u7ecf\u8fc7 label_id \u7684 in \u67e5\u8be2\u4e4b\u540e\u7684\u7ed3\u679c\u96c6\u4e0d\u662f\u6709\u5e8f\u7684\uff08\u867d\u7136\u4f60 in \u7684\u6570\u636e\u662f\u6709\u5e8f\u7684\uff0c\u4f46\u662f\u4f18\u5316\u5668\u5e76\u4e0d\u77e5\u9053\uff09\u3002\u6240\u4ee5\u9700\u8981\u4f7f\u7528\u4e34\u65f6\u8868\u5b8c\u6210 group_by \u64cd\u4f5c\u3002<\/p>\n<p>\u5982\u679c\u4f60\u628a in \u6362\u6210 label_id=xxx,\u5e94\u8be5\u4e5f\u662f\u4e0d\u7528 file_sort,\u53ef\u4ee5\u76f4\u63a5\u8d70\u539f\u7d22\u5f15\uff0c\u4f60\u53ef\u4ee5 explain \u770b\u4e00\u4e0b\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4243892\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : SjwNo1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Egfly \u61c2\u4e86 \u8c22\u8c22                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4243893\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zlowly <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u662f\u5426\u53ef\u4ee5\u8003\u8651\u7528\u8868\u5206\u533a\uff1f<br \/>is_active \u5b57\u6bb5\u4ece\u540d\u5b57\u4e0a\u731c\u6d4b\u662f\u53ef\u53d8\u7684\uff0c\u4e0d\u9002\u5408\u5206\u533a\u3002\u4f46\u8fd9\u4e2a label_id \u4e0d\u77e5\u9053\u5b83\u7684\u6570\u636e\u662f\u5426\u662f\u53ef\u4ee5\u8003\u8651\u8fdb\u884c\u5206\u533a\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4243894\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : SjwNo1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @zlowly \u6709\u5728\u8003\u8651\u5206\u533a\uff0c\u4e0d\u8fc7\u8fd9\u5f20\u8868\u5806\u79ef\u7684\u76f8\u5173\u4e1a\u52a1\u592a\u591a\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u54a8\u8be2\u5173\u4e8e order_by \u548c g&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\/198246"}],"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=198246"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/198246\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=198246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=198246"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=198246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}