做sql优化的前提瞧见是sql等价

1.MAX MIN写法的分与合

drop table t purge;
create table t as select * from dba_objects;
alter table t add constraint pk1_object_id primary key (OBJECT_ID);
set autotrace on
set linesize 1000

select min(object_id),max(object_id) from t;
和这个分开写有差别吗
select max(object_id) from t;
select min(object_id) from t;
其实这两种方式不等价,
等价的语句是
 select max, min  
    from (select max(object_id) max from t ) a,(select min(object_id) min from t) b;

2.In与 ><写法之间的异同

drop table t purge;
create table t as select * from dba_objects;
create index idx_object_id on t(object_id,object_type);
UPDATE t SET OBJECT_ID=20 WHERE ROWNUM<=26000;
UPDATE t SET OBJECT_ID=21 WHERE OBJECT_ID<>20;
COMMIT;
set linesize 266
set pagesize 1
alter session set statistics_level=all ;


select  /*+index(t,idx_object_id)*/ * from t  where object_TYPE='TABLE'  AND OBJECT_ID >= 20 AND OBJECT_ID<= 21;
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
-------------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name          | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
-------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |               |      1 |        |   2925 |00:00:00.03 |    1103 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T             |      1 |   2126 |   2925 |00:00:00.03 |    1103 |
|*  2 |   INDEX RANGE SCAN          | IDX_OBJECT_ID |      1 |    320 |   2925 |00:00:00.02 |     730 |
-------------------------------------------------------------------------------------------------------

select  /*+index(t,idx_object_id)*/ * from t t where object_TYPE='TABLE'  AND  OBJECT_ID IN (20,21);
select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
---------------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name           | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
---------------------------------------------------------------------------------------------------------
|   1 |  INLIST ITERATOR             |                |      1 |        |   2920 |00:00:00.01 |     563 |
|   2 |   TABLE ACCESS BY INDEX ROWID| t              |      2 |   2592 |   2920 |00:00:00.01 |     563 |
|*  3 |    INDEX RANGE SCAN          | IDX1_OBJECT_ID |      2 |      1 |   2920 |00:00:00.01 |     214 |

-----------------------------------------------------

3.count(*)与count(列)不等价

4.看似不想等,实则相等

begin
select count(*) into v_cnt from t1 ;
if v_cnt>0 
then  …A逻辑….
else 
then  …B逻辑…..
End;


我来翻译一下这段需求:
获取t1 表的记录数,判断是否大于0,如果大于0走A逻辑,否则就走B逻辑。
因此代码就如上所示来实现了。真正的需求是这样吗?

其实应该是这样的:只要T1表有记录就走A逻辑,否则走B逻辑。

两者有区别吗?其实区别还是很大的,前者可是强调获取记录数,我们是不是一定要遍历整个表得出一个记录数才知道是否大于0?
真正需求的理解可以让我们这样实现,只要从T1表中成功获取到第一条记录,就可以停止检索了,表示该表有记录了,难道事实不是这样?
因此原先的SQL1 从Select count(*) from t1; 被改造为:
Select count(*) from t1 where rownum=1; 

begin
select count(*) into v_cnt from t1 where rownum=1;
if v_cnt=1 
then  …A逻辑….
else 
then  …B逻辑…..
End;

5.从需求优化


版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2014-07-12 14:35  程序那些事  阅读(215)  评论(0编辑  收藏  举报
http://www.cnblogs.com/weiguo21/p/5191887.html