注意事项:

1、by keeping them "independent" and then aggregating their aggregates -- we significantly  decreased the amount of processing going on.

在写统计个数的SQL语句的过程,我们最好,先把每个union all的子集的结果进行分别的汇总,然后,最终统一进行汇总,

例如下面两个语句的写法:

A:

SELECT COUNT(*)
  FROM (SELECT NULL
          FROM test_explan
        UNION ALL
        SELECT NULL FROM test_explan);

B:

SELECT SUM(cnt)
  FROM (SELECT COUNT(*) cnt
          FROM test_explan
        UNION ALL
        SELECT COUNT(*) cnt FROM test_explan)

推荐使用第二种写法,参考网址:

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:21547067945222

http://blog.csdn.net/tianlesoftware/article/details/5632003

下面是使用跟踪得来的详情:

A

B:

原文地址:https://www.cnblogs.com/caroline/p/2785966.html