dbms_stats 手动修改 表 统计信息

dbms_stats 手动修改 表 统计信息 , 作为测试用


SELECT    e.first_name, e.last_name, e.salary, d.department_name
FROM  employees e,  departments d
WHERE d.department_name IN ('Marketing', 'Sales')
AND e.department_id = d.department_id;
 
1.  get table orignal statistics information :


exec  dbms_stats.gather_table_stats( OwnName  => 'SCOTT' ,TabName  => 'EMPLOYEES',Estimate_Percent  => 100 ,Cascade => TRUE );
exec  dbms_stats.gather_table_stats( OwnName  => 'SCOTT' ,TabName  => 'DEPARTMENTS',Estimate_Percent  => 100 ,Cascade => TRUE );


SQL>  select table_name , NUM_ROWS , BLOCKS ,  AVG_ROW_LEN  from user_tables where table_name in ('EMPLOYEES','DEPARTMENTS') ;

TABLE_NAME                       NUM_ROWS     BLOCKS AVG_ROW_LEN
------------------------------ ---------- ---------- -----------
DEPARTMENTS                            27          5          21
EMPLOYEES                             107          5          69
 

Execution Plan
----------------------------------------------------------
Plan hash value: 1021246405

--------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                   |    19 |   722 |     4   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                |                   |       |       |            |          |
|   2 |   NESTED LOOPS               |                   |    19 |   722 |     4   (0)| 00:00:01 |
|*  3 |    TABLE ACCESS FULL         | DEPARTMENTS       |     2 |    32 |     3   (0)| 00:00:01 |
|*  4 |    INDEX RANGE SCAN          | EMP_DEPARTMENT_IX |    10 |       |     0   (0)| 00:00:01 |
|   5 |   TABLE ACCESS BY INDEX ROWID| EMPLOYEES         |    10 |   220 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------

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

   3 - filter("D"."DEPARTMENT_NAME"='Marketing' OR "D"."DEPARTMENT_NAME"='Sales')
   4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")

SQL>


2. set table rows , blocks to middle size

exec dbms_stats.set_table_stats(ownname =>  'SCOTT' , tabname =>  'EMPLOYEES'  , numrows =>  100000 , numblks =>  10000) ;

exec dbms_stats.set_table_stats(ownname =>  'SCOTT' , tabname =>  'DEPARTMENTS'  , numrows =>  10000 , numblks =>  1000) ;

SQL> select table_name , NUM_ROWS , BLOCKS ,  AVG_ROW_LEN  from user_tables where table_name in ('EMPLOYEES','DEPARTMENTS') ;

TABLE_NAME                       NUM_ROWS     BLOCKS AVG_ROW_LEN
------------------------------ ---------- ---------- -----------
DEPARTMENTS                         10000       1000          21
EMPLOYEES                          100000      10000          69

 
SELECT   e.first_name, e.last_name, e.salary, d.department_name
FROM  employees e,  departments d
WHERE d.department_name IN ('Marketing', 'Sales')
AND e.department_id = d.department_id;

Execution Plan
----------------------------------------------------------
Plan hash value: 1901596505

--------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                   |  2743K|    99M|   302   (7)| 00:00:04 |
|   1 |  MERGE JOIN                  |                   |  2743K|    99M|   302   (7)| 00:00:04 |
|   2 |   TABLE ACCESS BY INDEX ROWID| EMPLOYEES         |   100K|  2148K|    10   (0)| 00:00:01 |
|   3 |    INDEX FULL SCAN           | EMP_DEPARTMENT_IX |   106 |       |     1   (0)| 00:00:01 |
|*  4 |   SORT JOIN                  |                   |   741 | 11856 |   274   (1)| 00:00:04 |
|*  5 |    TABLE ACCESS FULL         | DEPARTMENTS       |   741 | 11856 |   273   (1)| 00:00:04 |
--------------------------------------------------------------------------------------------------

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

   4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
       filter("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")
   5 - filter("D"."DEPARTMENT_NAME"='Marketing' OR "D"."DEPARTMENT_NAME"='Sales')

SQL>


3. set table rows , blocks to big size

exec dbms_stats.set_table_stats(ownname =>  'SCOTT' , tabname =>  'EMPLOYEES'  , numrows =>  10000000000000 , numblks =>  10000) ;
exec dbms_stats.set_table_stats(ownname =>  'SCOTT' , tabname =>  'DEPARTMENTS'  , numrows =>  100000000 , numblks =>  1000) ;

SQL> select table_name , NUM_ROWS , BLOCKS ,  AVG_ROW_LEN  from user_tables where table_name in ('EMPLOYEES','DEPARTMENTS') ;

TABLE_NAME                       NUM_ROWS     BLOCKS AVG_ROW_LEN
------------------------------ ---------- ---------- -----------
DEPARTMENTS                        100000       1000          21
EMPLOYEES                       100000000      10000          69

SQL>


SELECT   e.first_name, e.last_name, e.salary, d.department_name
FROM  employees e,  departments d
WHERE d.department_name IN ('Marketing', 'Sales')
AND e.department_id = d.department_id;
Execution Plan
----------------------------------------------------------
Plan hash value: 1021246405

--------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |                   |  2743P|    15E|    27M  (1)| 90:39:08 |
|   1 |  NESTED LOOPS                |                   |       |       |            |          |
|   2 |   NESTED LOOPS               |                   |  2743P|    15E|    27M  (1)| 90:39:08 |
|*  3 |    TABLE ACCESS FULL         | DEPARTMENTS       |  7407K|   113M|  2160  (88)| 00:00:26 |
|*  4 |    INDEX RANGE SCAN          | EMP_DEPARTMENT_IX |   106 |       |     0   (0)| 00:00:01 |
|   5 |   TABLE ACCESS BY INDEX ROWID| EMPLOYEES         |   370G|  7588G|     9   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------

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

   3 - filter("D"."DEPARTMENT_NAME"='Marketing' OR "D"."DEPARTMENT_NAME"='Sales')
   4 - access("E"."DEPARTMENT_ID"="D"."DEPARTMENT_ID")

SQL>

4.


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16993226/viewspace-772635/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/16993226/viewspace-772635/

你可能感兴趣的:(dbms_stats 手动修改 表 统计信息)