{"id":159637,"date":"2020-09-24T15:03:23","date_gmt":"2020-09-24T07:03:23","guid":{"rendered":"http:\/\/4563.org\/?p=159637"},"modified":"2020-09-24T15:03:23","modified_gmt":"2020-09-24T07:03:23","slug":"%e5%af%b9%e4%ba%8e%e4%b8%8d%e5%90%8c%e5%ad%97%e6%ae%b5%e7%9a%84-or-%e6%9f%a5%e8%af%a2%e6%80%8e%e6%a0%b7%e5%bb%ba%e7%ab%8b%e6%89%80%e4%bb%a5%e6%9f%a5%e8%af%a2%e9%80%9f%e5%ba%a6%e5%bf%ab","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=159637","title":{"rendered":"\u5bf9\u4e8e\u4e0d\u540c\u5b57\u6bb5\u7684 or \u67e5\u8be2\u600e\u6837\u5efa\u7acb\u6240\u4ee5\u67e5\u8be2\u901f\u5ea6\u5feb?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5bf9\u4e8e\u4e0d\u540c\u5b57\u6bb5\u7684 or \u67e5\u8be2\u600e\u6837\u5efa\u7acb\u6240\u4ee5\u67e5\u8be2\u901f\u5ea6\u5feb?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : RickyC <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        SELECT count(*) FROM TABLE WHERE A=1 OR B=2<\/p>\n<p>\u8fd9\u6837\u7684<br \/>\u6211\u9700\u8981\u5f97\u5230\u603b\u6570<\/p>\n<p>\u5efa\u7acb A+B \u7684\u7d22\u5f15\u5bf9\u4e8e\u8fd9\u79cd OR \u7684\u67e5\u8be2\u597d\u50cf\u4e0d\u7ba1\u7528<\/p>\n<p>\u5355\u8868\u5343\u4e07\u6761\u6570\u636e\u7684      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>18<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3438312\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bay10350154 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             UNION ALL                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438313\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RickyC <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @bay10350154 <br \/> \u7531\u4e8e\u662f\u5343\u4e07\u7ea7\u7684\u8868, \u5373\u4f7f\u4f7f\u7528\u7d22\u5f15 SELECT \u51fa\u5355\u4e00\u6761\u4ef6\u7684\u6240\u6709\u6570\u636e, \u4e5f\u9700\u8981\u5f88\u4e45<\/p>\n<p>UNION ALL \u53ea\u80fd\u7528\u4e8e\u6570\u636e, \u800c\u4e0d\u80fd\u7528\u4e8e count(*)\u5427?                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438314\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : limboMu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5206\u60c5\u51b5\uff0c\u5982\u679c A=1 or B=2 \u7684\u6570\u636e\u5728\u6574\u4e2a\u6570\u636e\u96c6\u7684\u5360\u6bd4\u6bd4\u8f83\u5c0f\uff0c\u53ef\u4ee5\u628a\uff0c\u4e24\u4e2a\u5b57\u6bb5\u5408\u5e76\u6210\u4e00\u4e2a\u5b57\u6bb5\uff0c\u52a0\u7d22\u5f15\u4f18\u5316\u3002\u5982\u679c \u6570\u636e\u96c6\u5360\u6bd4\u6bd4\u8f83\u5927\u7684\u53ef\u4ee5\u8003\u8651\u5f15\u5165\u7f13\u5b58\u6765\u8ba1\u6570\uff0c\u4e0d\u8fc7\u8fd9\u6837\u8981\u8003\u8651\u7f13\u5b58\u548c\u6570\u636e\u5e93\u8ba1\u6570\u7684\u4e00\u81f4\u6027\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438315\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : limboMu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u63a5\u4e0a\uff0c\u7b2c\u4e00\u79cd\u60c5\u51b5\u5982\u679c A B \u5b57\u6bb5\u6570\u636e\u5360\u6bd4\u6bd4\u8f83\u7406\u60f3\u7684\u8bdd\uff0c\u76f4\u63a5\u62c6\u5f00\u7528 UNION ALL \u5206\u522b\u8d70\u7d22\u5f15\u67e5\u8be2\u4e5f\u53ef\u4ee5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438316\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cqxxxxxxx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u8bb0\u5f97\u5efa\u7acb a b \u7684\u7ec4\u5408\u7d22\u5f15\u5bf9 count \u67e5\u8be2\u5373\u4f7f\u7528\u4e86 or \u4e5f\u4f1a\u751f\u6548\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438317\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : icql <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @RickyC \u4e00\u7684\u610f\u601d\u662f UNION ALL \u540e\u518d\u628a\u4e24\u4e2a count \u518d\u52a0\u4e00\u4e0b\u5427\u3002\u3002\u3002\u3002\u4f60\u53ef\u4ee5 sql \u5305\u4e00\u5c42 sum \u4e00\u4e0b\u6216\u8005\u4ee3\u7801\u91cc\u8fb9\u52a0\u4e00\u4e0b<br \/>SELECT count(*) FROM TABLE WHERE A=1 <br \/>UNION ALL<br \/>SELECT count(*) FROM TABLE WHERE B=2                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438318\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RickyC <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @icql A=1 \u548c B=2 \u91cc\u6709\u91cd\u590d\u7684\u5440, \u76f4\u63a5\u76f8\u52a0\u5f97\u5230\u7684\u662f\u4e0d\u6b63\u786e\u7684\u7ed3\u679c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438319\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RickyC <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cqxxxxxxx <br \/>\u76ee\u524d\u770b\u6765 where a=1 or a=2 \u8fd9\u6837\u7684\u8d70\u7d22\u5f15<br \/>\u800c where a=2 or b=2 \u8fd9\u6837\u7684\u4e0d\u8d70\u7d22\u5f15, \u5373\u4f7f\u662f count(*)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438320\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RickyC <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @cqxxxxxxx \u7ec4\u5408\u7d22\u5f15\u4e5f\u4e0d\u597d\u4f7f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438321\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RickyC <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7fa4\u91cc\u4e00\u4f4d\u5927\u54e5\u7ed9\u4e86\u4e2a\u7b54\u6848<\/p>\n<p>\u5148\u67e5 count(*) where a=1, \u5f97 x<br \/>\u518d\u67e5 count(*) where b=2, \u5f97 y<br \/>\u518d\u67e5 count(*) where a=1 and b=2, \u5f97 z<\/p>\n<p>\u7136\u540e\u7528 x+y-z \u5c31\u5f97\u5230 where a=1 or b=2 \u7684\u4e2a\u6570<\/p>\n<p>\u9700\u8981 3 \u4e2a\u7d22\u5f15: \u5355\u72ec a \u7684, \u5355\u72ec b \u7684, a \u548c b \u7684                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438322\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : liprais <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 postgresql \u968f\u4fbf\u627e\u4e86\u4e2a\u8868\u8bd5\u4e86\u8bd5<\/p>\n<p>\u53ea\u9700\u8981\u5728 a,b \u5217\u4e0a\u5355\u72ec\u5efa\u7d22\u5f15\u5c31\u884c\uff0c\u8fd9\u662f\u6267\u884c\u8ba1\u5212:<\/p>\n<p>QUERY PLAN<br \/>Aggregate (cost=22119.36..22119.37 rows=1 width=8) (actual time=21.540..21.542 rows=1 loops=1)<br \/>-&gt; Bitmap Heap Scan on a (cost=300.17..22081.89 rows=14988 width=0) (actual time=1.539..20.603 rows=15506 loops=1)<br \/>Recheck Cond: ((city_name = &#8216;\u5317\u4eac&#8217;::text) OR (city_code = &#8216;120000&#8217;::text))<br \/>Heap Blocks: exact=3805<br \/>-&gt; BitmapOr (cost=300.17..300.17 rows=15445 width=0) (actual time=1.153..1.154 rows=0 loops=1)<br \/>-&gt; Bitmap Index Scan on index_a (cost=0.00..214.28 rows=11449 width=0) (actual time=0.806..0.806 rows=11534 loops=1)<br \/>Index Cond: (city_name = &#8216;\u5317\u4eac&#8217;::text)<br \/>-&gt; Bitmap Index Scan on index_b (cost=0.00..78.39 rows=3996 width=0) (actual time=0.346..0.346 rows=3972 loops=1)<br \/>Index Cond: (city_code = &#8216;120000&#8217;::text)<br \/>Planning Time: 0.128 ms<br \/>Execution Time: 21.566 ms                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438323\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wangritian <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             A+B \u7684\u7d22\u5f15\u5f53\u7136\u5bf9 or B \u4e0d\u8d77\u4f5c\u7528\u4e86\uff0c\u9700\u8981\u5355\u72ec\u5bf9 B \u52a0\u7d22\u5f15                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438324\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : liprais <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             mysql 8.0 \u662f\u53ef\u4ee5\u7684<br \/>explain select * from test.mvcc where a = 1 or b = 2;<br \/>id,select_type,table,partitions,type,possible_keys,key,key_len,ref,rows,filtered,Extra<br \/>1,SIMPLE,mvcc,,index_merge,&#8221;a,b&#8221;,&#8221;a,b&#8221;,&#8221;5,5&#8243;,,24,100,&#8221;Using union(a,b); Using where&#8221;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438325\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RickyC <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @liprais explain \u7684 rows \u5e76\u4e0d\u662f\u7edf\u8ba1\u4e2a\u6570\u5427? \u548c count(*)\u7ed3\u679c\u76f8\u5dee\u592a\u5927                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438326\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : taogen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @RickyC \u52a0\u7d22\u5f15 index (A, B) \u540e OR \u7684\u67e5\u8be2\u7ba1\u7528\u3002\u4e0d\u4fe1\u4f60\u8d34\u4e00\u4e0b explain SELECT count(*) FROM TABLE WHERE A=1 OR B=2                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438327\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhangysh1995 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @liprais EXPLAIN \u662f\u8fd1\u4f3c\u7ed3\u679c\uff0c\u53ef\u80fd\u5f88\u79bb\u8c31                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438328\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : RickyC <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u547d\u4ee4: SELECT count(*) FROM table WHERE `a`=21 OR `b`=4301;<\/p>\n<p>+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |<br \/>+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>| 1 | SIMPLE | table | NULL | index_merge | index_a,index_b,index_ab | index_ab,index_b | 5,5 | NULL | 639711 | 100.00 | Using sort_union(index_ab,index_b); Using where |<br \/>+&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;-\u5206\u9694\u7b26&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>\u547d\u4ee4: SELECT count(*) FROM table WHERE `a`=21 OR `b`=4301;<br \/>+&#8212;&#8212;&#8212;-+<br \/>| count(*) |<br \/>+&#8212;&#8212;&#8212;-+<br \/>| 690113 |<br \/>+&#8212;&#8212;&#8212;-+<\/p>\n<p>1 row in set (2 min 23.63 sec)<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;-\u5206\u9694\u7b26&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\u60a8\u662f\u8bf4 explain \u7684 rows 639711 \u5c31\u662f\u603b\u6761\u6570\u5417?<br \/>\u4f46\u662f\u548c count \u7684 690113 \u6570\u91cf\u4e0d\u540c<\/p>\n<p>@taogen                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3438329\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : liprais <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @zhangysh1995 \u770b\u6267\u884c\u8ba1\u5212\u5f53\u7136\u662f\u770b\u6709\u6ca1\u6709\u547d\u4e2d\u7d22\u5f15\u554a\uff1f\u4f60\u89c9\u5f97\u6211\u8d34\u6267\u884c\u8ba1\u5212\u662f\u770b\u5565?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u4e8e\u4e0d\u540c\u5b57\u6bb5\u7684 or \u67e5\u8be2\u600e\u6837\u5efa\u7acb&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\/159637"}],"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=159637"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/159637\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=159637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=159637"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=159637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}