1. 程式人生 > >sql解惑2(謎題45)join比case when要快?

sql解惑2(謎題45)join比case when要快?

感覺這說法不靠譜,於是實驗如下

drop table test;
drop table lv;

create table test as select level as t1,lpad('x',20) as t2 from dual connect by level <= 1000;
create index idx_test_t1 on test(t1);
alter system flush buffer_cache;
set autotrace traceonly;

select case
         when t1 <= 100 then
          '0-100'
         when t1 <= 200 then
          '0-200'
         when t1 <= 300 then
          '0-300'
         when t1 <= 400 then
          '0-400'
         when t1 <= 500 then
          '0-500'
         when t1 <= 600 then
          '0-600'
         when t1 <= 700 then
          '0-700'
         when t1 <= 800 then
          '0-800'
         when t1 <= 900 then
          '0-900'
         when t1 <= 1000 then
          '0-1000'
       end as t1,
       t2
  from test
 group by case
            when t1 <= 100 then
             '0-100'
            when t1 <= 200 then
             '0-200'
            when t1 <= 300 then
             '0-300'
            when t1 <= 400 then
             '0-400'
            when t1 <= 500 then
             '0-500'
            when t1 <= 600 then
             '0-600'
            when t1 <= 700 then
             '0-700'
            when t1 <= 800 then
             '0-800'
            when t1 <= 900 then
             '0-900'
            when t1 <= 1000 then
             '0-1000'
          end,
          t2;


create table lv as select (level * 100) - 100 as tf,level * 100 as td  from dual connect by level <= 10;

alter system flush buffer_cache;

select lv.tf || '-' || lv.td as t1,
       t2
  from test inner join lv on test.t1 > lv.tf and test.t1 <= lv.td
 group by lv.tf,lv.td,
          t2;

表已刪除。

SQL> 
表已刪除。

SQL> SQL> 
表已建立。

SQL> 
索引已建立。

SQL> 
系統已更改。


執行計劃
----------------------------------------------------------
Plan hash value: 1435881708

---------------------------------------------------------------------------
| Id  | Operation	   | Name | Rows  | Bytes | Cost (%CPU)| Time	  |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |	  |  1000 | 25000 |	6  (17)| 00:00:01 |
|   1 |  HASH GROUP BY	   |	  |  1000 | 25000 |	6  (17)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| TEST |  1000 | 25000 |	5   (0)| 00:00:01 |
---------------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement (level=2)


統計資訊
----------------------------------------------------------
	 28  recursive calls
	  0  db block gets
	 17  consistent gets
	  6  physical reads
	  0  redo size
	776  bytes sent via SQL*Net to client
	524  bytes received via SQL*Net from client
	  2  SQL*Net roundtrips to/from client
	  0  sorts (memory)
	  0  sorts (disk)
	 10  rows processed

SQL> SQL> SQL> 
表已建立。

SQL> SQL> 
系統已更改。



執行計劃
----------------------------------------------------------
Plan hash value: 3246781147

------------------------------------------------------------------------------
| Id  | Operation	      | Name | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |      |	  25 |	1275 |	  12  (25)| 00:00:01 |
|   1 |  HASH GROUP BY	      |      |	  25 |	1275 |	  12  (25)| 00:00:01 |
|   2 |   MERGE JOIN	      |      |	  25 |	1275 |	  11  (19)| 00:00:01 |
|   3 |    SORT JOIN	      |      |	  10 |	 260 |	   5  (20)| 00:00:01 |
|   4 |     TABLE ACCESS FULL | LV   |	  10 |	 260 |	   4   (0)| 00:00:01 |
|*  5 |    FILTER	      |      |	     |	     |		  |	     |
|*  6 |     SORT JOIN	      |      |	1000 | 25000 |	   6  (17)| 00:00:01 |
|   7 |      TABLE ACCESS FULL| TEST |	1000 | 25000 |	   5   (0)| 00:00:01 |
------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   5 - filter("TEST"."T1"<="LV"."TD")
   6 - access("TEST"."T1">"LV"."TF")
       filter("TEST"."T1">"LV"."TF")

Note
-----
   - dynamic sampling used for this statement (level=2)


統計資訊
----------------------------------------------------------
	 52  recursive calls
	  0  db block gets
	 27  consistent gets
	 11  physical reads
	  0  redo size
	794  bytes sent via SQL*Net to client
	524  bytes received via SQL*Net from client
	  2  SQL*Net roundtrips to/from client
	  2  sorts (memory)
	  0  sorts (disk)
	 10  rows processed

SQL> 

希望有其它意見的提供不同案例