【原创-每日一优】count/sum优化

上一篇 / 下一篇  2008-04-02 16:28:05 / 个人分类:Oracle优化

【原理】
SQL> select count(*) from emp where deptno=20;

  COUNT(*)
----------
         5
   
SQL> select sum(decode(deptno, 20, 1, null)) from emp;

SUM(DECODE(DEPTNO,20,1))
------------------------
                       5


SQL> select count(decode(deptno, 20, 1, null)) from emp;

COUNT(DECODE(DEPTNO,20,1))
--------------------------
                         5
【案例】
http://www.itpub.net/thread-964972-1-1.html
SELECT    count(postedtt."ttDateTime"),
    NVL(SUM(postedtt."Stake"), 0) AS "SumStake"
    FROM    postedtt
    WHERE    TO_CHAR(postedtt."ttDateTime", 'YYYY-MM') = '2008-03'
        ORDER BY TO_CHAR(postedtt."ttDateTime", 'YYYY-MM-DD');

优化后
SELECT    count(decode(TO_CHAR(t.ttDateTime, 'YYYY-MM'), '2008-03', 1, null),
FROM    postedtt t
union all
select    NVL(SUM(t.Stake), 0)
FROM    postedtt t
WHERE    TO_CHAR(t.ttDateTime, 'YYYY-MM') = '2008-03'
/

create a index for ttDateTime column and analyze it

TAG:

引用 删除 jvkojvko   /   2008-05-26 16:44:53
这样会更快吗?好像还更慢一些啊
引用 删除 junmail   /   2008-04-07 13:30:26
5
引用 删除 Guest   /   2008-04-03 17:01:38
5
引用 删除 Guest   /   2008-04-03 12:03:31
5
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-07-20  
  12345
6789101112
13141516171819
20212223242526
2728293031  

数据统计

  • 访问量: 4513
  • 日志数: 144
  • 图片数: 1
  • 文件数: 5
  • 建立时间: 2007-12-07
  • 更新时间: 2008-07-17

RSS订阅

Open Toolbar