例如有多个union 会不会造成多次过滤
- SQL code
select username from tab1unionselect username from tab2unionselect username from tab3...unionselect username from tabn
union 是每次都去除重复行还是别的?
- SQL code
select distinct username from (select username from tab1union allselect username from tab2union allselect username from tab3...union allselect username from tabn)
好像只是过滤一次,效率会高吗
------解决方案--------------------
如果只是考虑这两段代码的效率的话,我想第二个会高一些。
------解决方案--------------------
distinct + union all
好些!
------解决方案--------------------
测试了下,测试的结果如下
- SQL code
SQL> select tbrq from dbscgs_mx 2 union 3 select tbrq from dbscgs_mxbak 4 union 5 select trunc(lrrq) from dbscgs_mx 6 union 7 select trunc(lrrq) from dbscgs_mxbak;......已选择57行。实际:187select distinct rq from(select tbrq rq from dbscgs_mxunion allselect tbrq rq from dbscgs_mxbakunion allselect trunc(lrrq) rq from dbscgs_mxunion allselect trunc(lrrq) rq from dbscgs_mxbak);......已选择57行。实际:171