本space的功能以整体和收集Oracle经典技术文章为主!

【丹臣】优化SQL的另类思考

上一篇 / 下一篇  2008-05-06 19:54:20 / 个人分类:OracleACE聚合

Buffer Gets    Executions  Gets per Exec  %Total Time (s)  Time (s) Hash Value
------------- ------------ -------------- ------ -------- --------- ----------
116,608,373      164,083          710.7   40.6  7027.07  11922.30 3701069644
Module: java@test.cm2 (TNS V1-V3)
           select count(*)           from test
   where  username = :1    --这是一个高势列,
   and    ends>sysdate
   and approve_status in (0,1,-9)
   and id <> :2            --这是主键
   and promoted_status = 1

如果大家见到这样的SQL语句会怎么样优化?通常的做法,是在当前索引中冗余id字段,以避免回表。但这样要去调整这张大表的索引.

在看到上面的SQL后,询问开发能否明确的知道id=:2并且满足其它条件的这样的记录是否一定存在。开发经过查证后,最后的答复是无法肯定.既然在应用层无法确定,那也要想个办法来解决大量回表的问题。在经过仔细观察后,我将上面这条SQL语句转换成下面两条SQL以及最后一步应用逻辑来实现:

第一条SQL:

Select/*+ index(a, PK_test_ID) */ count(*)   from test a
   where id=:1 and  ends>sysdate and approve_status in (0,1,-9)  and promoted_status = 1 and username=:2

第二条SQL:

select count(*)    from test
   where  username = :1 and     ends>sysdate and approve_status in (0,1,-9) and     and promoted_status = 1

第三步,将两个结果相减即可实现业务

我们在做SQL优化时,如何把一条SQL根据需要等价转化成多条,需要考虑当前的应用逻辑,以及当前数据库中索引的情况,优化便会事半功倍。如何跳出ORACLE去思考问题,希望这个优化案例能对大家有所启示。

--EOF--

作者:丹臣| 【转载时请务必以超链接形式标明文章原始出处和作者信息及本声明】
地址:http://rdc.taobao.com/blog/dba/html/140_sql_performance_tuning_dc.html

TAG: sql优化

引用 删除 cccgw   /   2008-05-10 20:38:12
5
引用 删除 ltxzym   /   2008-05-09 17:12:05
5
引用 删除 Guest   /   2008-05-09 17:04:26
5
 

评分:0

我来说两句

显示全部

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

日历

« 2008-05-16  
    123
45678910
11121314151617
18192021222324
25262728293031

数据统计

  • 访问量: 5350
  • 日志数: 73
  • 建立时间: 2008-03-25
  • 更新时间: 2008-05-10

RSS订阅

Open Toolbar