在有无柱状图的情况下cursor_sharing=similar,force对SQL执行计划的影响

在没有柱状图的情况下,cursor_sharing=similar和force的区别
SQL> conn zhou/zhou
Connected.

SQL> create table cursor_t as select * from sys.obj$;

Table created.

SQL> show parameter cursor

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
cursor_sharing                       string      EXACT
cursor_space_for_time                boolean     FALSE
open_cursors                         integer     300
session_cached_cursors               integer     20
SQL> alter system set cursor_sharing=similar;

System altered.

SQL> show parameter cursor

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
cursor_sharing                       string      SIMILAR
cursor_space_for_time                boolean     FALSE
open_cursors                         integer     300
session_cached_cursors               integer     20
SQL> select count(*) from cursor_t where obj#=100;

  COUNT(*)
----------
         1

SQL> select SQL_TEXT from v$sql where sql_text like 'select count(*) from cursor_t%';

SQL_TEXT
--------------------------------------------------------------------------------
select count(*) from cursor_t where obj#=:"SYS_B_0"

SQL> select count(*) from cursor_t where obj#=1000;

  COUNT(*)
----------
         1

SQL>  select SQL_TEXT from v$sql where sql_text like 'select count(*) from cursor_t%';

SQL_TEXT
--------------------------------------------------------------------------------
select count(*) from cursor_t where obj#=:"SYS_B_0"
select count(*) from cursor_t where obj#=:"SYS_B_0"

SQL> select SQL_TEXT,version_count from v$sqlarea where sql_text like 'select count(*) from cursor_t%';

SQL_TEXT
--------------------------------------------------------------------------------
VERSION_COUNT
-------------
select count(*) from cursor_t where obj#=:"SYS_B_0"
            2


SQL> alter system set cursor_sharing=force;

System altered.

SQL> alter system flush shared_pool;

System altered.

SQL> select SQL_TEXT from v$sql where sql_text like 'select count(*) from cursor_t%';

no rows selected

SQL> select count(*) from cursor_t where obj#=100;

  COUNT(*)
----------
         1

SQL> select SQL_TEXT from v$sql where sql_text like 'select count(*) from cursor_t%';

SQL_TEXT
--------------------------------------------------------------------------------
select count(*) from cursor_t where obj#=:"SYS_B_0"

SQL> select count(*) from cursor_t where obj#=1000;

  COUNT(*)
----------
         1

SQL> select SQL_TEXT from v$sql where sql_text like 'select count(*) from cursor_t%';

SQL_TEXT
--------------------------------------------------------------------------------
select count(*) from cursor_t where obj#=:"SYS_B_0"



SQL> select SQL_TEXT,version_count from v$sqlarea where sql_text like 'select count(*) from cursor_t%';

SQL_TEXT
--------------------------------------------------------------------------------
VERSION_COUNT
-------------
select count(*) from cursor_t where obj#=:"SYS_B_0"
            1
           
在有柱状图的情况下,cursor_sharing=smilar,force区别
          
SQL> conn zhou/zhou
Connected.
SQL> select count(*) from cursor_t;

  COUNT(*)
----------
     57348

SQL> update cursor_t set obj#=1000 where obj#>10;

57338 rows updated.

SQL> commit;

Commit complete.

SQL> create index cursor_t_idx on cursor_t(obj#);

Index created.

SQL>  alter system set cursor_sharing=exact;

System altered.


SQL> exec DBMS_STATS.GATHER_TABLE_STATS(ownname=>user,tabname=>'CURSOR_T',method_opt=>'FOR COLUMNS OBJ# SIZE AUTO',cascade=>TRUE);

PL/SQL procedure successfully completed.


SQL> alter system set session_cached_cursors =0 scope=spfile;

System altered.

SQL> conn /as sysdba
Connected.
SQL> startup force
ORACLE instance started.

Total System Global Area 1073741824 bytes
Fixed Size                  2089400 bytes
Variable Size             633343560 bytes
Database Buffers          432013312 bytes
Redo Buffers                6295552 bytes
Database mounted.
Database opened.

SQL> alter system set cursor_sharing=similar;

System altered.

SQL> select count(*) from cursor_t where obj#=1000;

  COUNT(*)
----------
     57338

SQL>  select sql_id,CHILD_NUMBER,PLAN_HASH_VALUE from v$sql where sql_text like 'select count(*) from cursor_t%' order by LAST_ACTIVE_TIME;

SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE
------------- ------------ ---------------
5gmt75b7sctf4            0       439480374

SQL> select count(*) from cursor_t where obj#=1;

  COUNT(*)
----------
         1

SQL>  select sql_id,CHILD_NUMBER,PLAN_HASH_VALUE from v$sql where sql_text like 'select count(*) from cursor_t%' order by LAST_ACTIVE_TIME;

SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE
------------- ------------ ---------------
5gmt75b7sctf4            0       439480374
5gmt75b7sctf4            1      4056874985

SQL> select count(*) from cursor_t where obj#=2;

  COUNT(*)
----------
         1

SQL> select sql_id,CHILD_NUMBER,PLAN_HASH_VALUE from v$sql where sql_text like 'select count(*) from cursor_t%' order by LAST_ACTIVE_TIME;

SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE
------------- ------------ ---------------
5gmt75b7sctf4            0       439480374
5gmt75b7sctf4            1      4056874985
5gmt75b7sctf4            2      4056874985

SQL> select count(*) from cursor_t where obj#=3;

  COUNT(*)
----------
         1

SQL> select sql_id,CHILD_NUMBER,PLAN_HASH_VALUE from v$sql where sql_text like 'select count(*) from cursor_t%' order by LAST_ACTIVE_TIME;

SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE
------------- ------------ ---------------
5gmt75b7sctf4            0       439480374
5gmt75b7sctf4            1      4056874985
5gmt75b7sctf4            2      4056874985
5gmt75b7sctf4            3      4056874985

SQL> SELECT * FROM table(dbms_xplan.display_cursor('5gmt75b7sctf4',0));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID  5gmt75b7sctf4, child number 0
-------------------------------------
select count(*) from cursor_t where obj#=:"SYS_B_0"

Plan hash value: 439480374

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

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

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------

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

|   0 | SELECT STATEMENT      |              |       |       |    27 (100)|
     |

|   1 |  SORT AGGREGATE       |              |     1 |     3 |            |
     |

|*  2 |   INDEX FAST FULL SCAN| CURSOR_T_IDX | 57334 |   167K|    27   (4)| 00:0

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
0:01 |

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


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

   2 - filter("OBJ#"=:SYS_B_0)


19 rows selected.

SQL> SELECT * FROM table(dbms_xplan.display_cursor('5gmt75b7sctf4',1));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID  5gmt75b7sctf4, child number 1
-------------------------------------
select count(*) from cursor_t where obj#=:"SYS_B_0"

Plan hash value: 4056874985

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

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

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------

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

|   0 | SELECT STATEMENT  |              |       |       |     1 (100)|
|

|   1 |  SORT AGGREGATE   |              |     1 |     3 |            |
|

|*  2 |   INDEX RANGE SCAN| CURSOR_T_IDX |     5 |    15 |     1   (0)| 00:00:01

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
|

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


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

   2 - access("OBJ#"=:SYS_B_0)


19 rows selected.

SQL> SELECT * FROM table(dbms_xplan.display_cursor('5gmt75b7sctf4',2));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID  5gmt75b7sctf4, child number 2
-------------------------------------
select count(*) from cursor_t where obj#=:"SYS_B_0"

Plan hash value: 4056874985

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

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

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------

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

|   0 | SELECT STATEMENT  |              |       |       |     1 (100)|
|

|   1 |  SORT AGGREGATE   |              |     1 |     3 |            |
|

|*  2 |   INDEX RANGE SCAN| CURSOR_T_IDX |     5 |    15 |     1   (0)| 00:00:01

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
|

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


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

   2 - access("OBJ#"=:SYS_B_0)


19 rows selected.

SQL> SELECT * FROM table(dbms_xplan.display_cursor('5gmt75b7sctf4',3));

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID  5gmt75b7sctf4, child number 3
-------------------------------------
select count(*) from cursor_t where obj#=:"SYS_B_0"

Plan hash value: 4056874985

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

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

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------

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

|   0 | SELECT STATEMENT  |              |       |       |     1 (100)|
|

|   1 |  SORT AGGREGATE   |              |     1 |     3 |            |
|

|*  2 |   INDEX RANGE SCAN| CURSOR_T_IDX |     5 |    15 |     1   (0)| 00:00:01

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
|

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


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

   2 - access("OBJ#"=:SYS_B_0)


19 rows selected.

SQL> alter system set cursor_sharing=force;                       

System altered.

SQL> alter system flush shared_pool;

System altered.

SQL> select count(*) from cursor_t where obj#=1000;

  COUNT(*)
----------
     57338

SQL> select sql_id,CHILD_NUMBER,PLAN_HASH_VALUE from v$sql where sql_text like 'select count(*) from cursor_t%' order by LAST_ACTIVE_TIME;

SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE
------------- ------------ ---------------
5gmt75b7sctf4            0       439480374

SQL> select count(*) from cursor_t where obj#=1;

  COUNT(*)
----------
         1

SQL> select sql_id,CHILD_NUMBER,PLAN_HASH_VALUE from v$sql where sql_text like 'select count(*) from cursor_t%' order by LAST_ACTIVE_TIME;

SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE
------------- ------------ ---------------
5gmt75b7sctf4            0       439480374

SQL> select count(*) from cursor_t where obj#=2;

  COUNT(*)
----------
         1

SQL>  select sql_id,CHILD_NUMBER,PLAN_HASH_VALUE from v$sql where sql_text like 'select count(*) from cursor_t%' order by LAST_ACTIVE_TIME;

SQL_ID        CHILD_NUMBER PLAN_HASH_VALUE
------------- ------------ ---------------
5gmt75b7sctf4            0       439480374

你可能感兴趣的:(oracle,sql,Access)