您的位置:首页 > 其它

第一章 基础知识

2011-03-13 15:05 309 查看
1)opitimizer statistic

1@@@@statistic for truncate

@@@as we know, the schedual would gather statistic in 10pm

@@@I create a table as below.

SQL> conn hr/hr

SQL> select no1, count(*) from t1 group by no1;

NO1 COUNT(*)

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

1 4096000

2 64

4 64

5 64

3 64

@@@

@@@I truncate the table, and make sure it work.

SQL> truncate table t1;

Table truncated.

SQL> select no1, count(*) from t1 group by no1;

no rows selected

@@@

@@@After truncate operation whether the space was released or not? the answer is yes

@@@After you gather opertimizer statistic,

@@@you would find the space is empty in fact.

SQL> col owner format a10 trunc;

SQL> col table_name format a20 trunc;

SQL> select owner,table_name,num_rows,blocks from dba_tables where table_name='T1';

OWNER TABLE_NAME NUM_ROWS BLOCKS

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

HR T1 4086131 13157

SQL> exec dbms_stats.gather_table_stats('HR','T1');

SQL> select owner,table_name,num_rows,blocks from dba_tables where table_name='T1';

OWNER TABLE_NAME NUM_ROWS BLOCKS

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

HR T1 0 0

@@@Summary:

@@@the truncate operation modify the data, and do not modify

@@@the metadata in the data dictionary table.

2@@@@hint for select to use index

continue.....

@@@

@@@First: create index b-tree

@@@

SQL> select no1, count(*) from t1 group by no1;

NO1 COUNT(*)

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

1 4096000

2 64

4 64

5 64

3 64

SQL> create index it1_no1 on t1(no1) tablespace indextbs01;

SQL> exec dbms_stats.gather_table_stats('HR','T1');

SQL> exec dbms_stats.gather_index_stats('HR','IT1_NO1');

@@@

SQL> col segment_name format a20 trunc;

SQL> select segment_name,bytes/1024/1024 size_M from dba_segments where segment_name='T1';

SEGMENT_NAME SIZE_M

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

T1 104

SQL> select segment_name,bytes/1024/1024 size_M from dba_segments where segment_name ='IT1_NO1';

SEGMENT_NAME SIZE_M

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

IT1_NO1 64

@@@

SQL> select num_rows,blocks from dba_tables where table_name='T1';

NUM_ROWS BLOCKS

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

4133785 13157

SQL> select num_rows,leaf_blocks,distinct_keys from dba_indexes where owner='HR' and index_name='IT1_NO1';

NUM_ROWS LEAF_BLOCKS DISTINCT_KEYS

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

4086216 7984 5

@@@

@@@Summary:

@@@index it1_no1 => 64M

@@@table t1 => 104M

@@@

@@@Second: begin hint to using index forcing

@@@

@@@in this case, access index is slower than access table.

@@@it is not necessary to use index enforcing,

@@@Just keep in mind statistic is fresh

SQL> exec dbms_stats.gather_table_stats('HR','T1');

SQL> exec dbms_stats.gather_index_stats('HR','IT1_NO1');

SQL> set linesize 200

SQL> set autot traceonly

SQL> alter system flush buffer_cache;

SQL> select * from t1 where no1=1;

4096000 rows selected.

Elapsed: 00:01:36.32

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

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

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

| 0 | SELECT STATEMENT | | 4133K| 70M| 3036 (6)| 00:00:37 |

|* 1 | TABLE ACCESS FULL| T1 | 4133K| 70M| 3036 (6)| 00:00:37 |

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

Predicate Information (identified by operation id):

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

1 - filter("NO1"=1)

Statistics

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

0 recursive calls

0 db block gets

286235 consistent gets

8640 physical reads

0 redo size

70451950 bytes sent via SQL*Net to client

3004195 bytes received via SQL*Net from client

273068 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

4096000 rows processed

SQL> alter system flush buffer_cache;

SQL> select /*+ index(t1 it1_no1) */ * from t1 where no1=1;

4096000 rows selected.

Elapsed: 00:01:40.54

Execution Plan

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

Plan hash value: 1126917783

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

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

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

| 0 | SELECT STATEMENT | | 4133K| 70M| 27698 (1)| 00:05:33 |

| 1 | TABLE ACCESS BY INDEX ROWID| T1 | 4133K| 70M| 27698 (1)| 00:05:33 |

|* 2 | INDEX RANGE SCAN | IT1_NO1 | 4086K| | 8088 (2)| 00:01:38 |

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

Predicate Information (identified by operation id):

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

2 - access("NO1"=1)

Statistics

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

0 recursive calls

0 db block gets

566608 consistent gets

21007 physical reads

0 redo size

144999137 bytes sent via SQL*Net to client

3004195 bytes received via SQL*Net from client

273068 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

4096000 rows processed

SQL> alter system flush buffer_cache;

@@@

@@@Summary:

@@@oracle opertimizer make a good judgement.

@@@after you gather the statistic.

@@@And in this case, you should create bitmap index batter.

@@@

@@@Thrid: compare the bitmap index

@@@

@@@due to the few physical read, do not flush buffer_cache each time.

SQL> create bitmap index ibitamp_t1_no1 on t1(no1) tablespace indextbs01;

SQL> exec dbms_stats.gather_table_stats('HR','T1');

SQL> select /*+ index(t1 ibitmap_t1_no1) */ * from t1 where no1=1;

4096000 rows selected

Elapsed: 00:01:42.46

Execution Plan

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

Plan hash value: 1342262429

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

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

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

| 0 | SELECT STATEMENT | | 4117K| 70M| 32407 (1)| 00:06:29 |

| 1 | TABLE ACCESS BY INDEX ROWID | T1 | 4117K| 70M| 32407 (1)| 00:06:29 |

| 2 | BITMAP CONVERSION TO ROWIDS| | | | | |

|* 3 | BITMAP INDEX SINGLE VALUE | IBITAMP_T1_NO1 | | | | |

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

Predicate Information (identified by operation id):

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

3 - access("NO1"=1)

Statistics

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

1 recursive calls

0 db block gets

286244 consistent gets

0 physical reads

0 redo size

144999137 bytes sent via SQL*Net to client

3004195 bytes received via SQL*Net from client

273068 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

4096000 rows processed

SQL> select * from t1 where no1=1;

4096000 rows selected.

Elapsed: 00:01:38.08

Execution Plan

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

Plan hash value: 3617692013

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

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

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

| 0 | SELECT STATEMENT | | 4117K| 70M| 3035 (6)| 00:00:37 |

|* 1 | TABLE ACCESS FULL| T1 | 4117K| 70M| 3035 (6)| 00:00:37 |

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

Predicate Information (identified by operation id):

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

1 - filter("NO1"=1)

Statistics

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

1 recursive calls

0 db block gets

286235 consistent gets

0 physical reads

0 redo size

70451950 bytes sent via SQL*Net to client

3004195 bytes received via SQL*Net from client

273068 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

4096000 rows processed

@@@got it

SQL> select /*+ index(t1 ibitmap_t1_no1) */ no1 from t1 where no1=1;

4096000 rows selected.

Elapsed: 00:00:44.03

Execution Plan

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

Plan hash value: 3758204669

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

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

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

| 0 | SELECT STATEMENT | | 4117K| 11M| 86 (0)| 00:00:02 |

| 1 | BITMAP CONVERSION TO ROWIDS| | 4117K| 11M| 86 (0)| 00:00:02 |

|* 2 | BITMAP INDEX SINGLE VALUE | IBITAMP_T1_NO1 | | | | |

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

Predicate Information (identified by operation id):

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

2 - access("NO1"=1)

Statistics

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

1 recursive calls

0 db block gets

173 consistent gets

0 physical reads

0 redo size

70451630 bytes sent via SQL*Net to client

3004195 bytes received via SQL*Net from client

273068 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

4096000 rows processed

本文出自 “Oracle+Linux=>Majesty” 博客,请务必保留此出处http://majesty.blog.51cto.com/3493901/898717
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: