文章出自: http://blog.sina.com.cn/s/blog_53a1165e0100nisg.html
v$sql?span xml:lang="EN-US">v$sqlarea视图?
v$sqlarea?/span>v$sql两个视图的不同之处在于,v$sql?每一?span xml:lang="EN-US">SQL保留????span xml:lang="EN-US">v$sqlarea??span xml:lang="EN-US">sql_text进?group by,?过version_count计算子指针的?。下面?这个??进??点延伸探讨??
首先介绍??/span>v$sql视图?strong>v$sql视图列举了共?span xml:lang="EN-US">SQL区(Shared SQL Area)中?span xml:lang="EN-US">SQL统?信息,这??图中的信?经分组,每个SQL指针都包?条独立的记录。这??图的主?字?如下?/span>
Column | Datatype | Descrption |
SQL_TEXT | VARCHAR2(1000) | 当前SQL指针的前1000?符(也就??这里记录?span xml:lang="EN-US">SQL?完整的) |
EXECUTIONS | NUMBER | 执?次数 |
DISK_READS | NUMBER | 这个子指?span xml:lang="EN-US">Disk Read的??/span> |
BUFFER_GETS | NUMBER | 这个子指针的Buffer Gets数量 |
OPTIMIZER_MODE | VARCHAR2(10) | SQL执?的优化器模式 |
OPTIMIZER_COST | NUMBER | SQL执?成本 |
HASH_VALUE | NUMBER | ?span xml:lang="EN-US">Library Cache?指针?span xml:lang="EN-US">Hash Value?/span> |
用前文应用的例子进?进一步?明,假定数据库中存在???span xml:lang="EN-US">TQ,用户下存在??span xml:lang="EN-US">EMP?以下测试来自Oracle 10gR2数据库环境)?/span>
[email protected]> create table emp as select * from scott.emp;
Table created.
[email protected]> set autotrace on
[email protected]> select count(*) from emp;
COUNT(*)
----------
14
Execution Plan
----------------------
Plan hash value: 2083865914
-------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| EMP | 14 | 3 (0)| 00:00:01 |
-------------------------------
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------
5 recursive calls
0 db block gets
7 consistent gets
5 physical reads
0 redo size
411 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
这个查?的统计信?示,执??strong>5?理??strong>7?span xml:lang="EN-US">Consistent Gets,来看一?/span>v$sql??录的统?数据?/span>
[email protected]> select sql_text,executions,disk_reads,optimizer_mode,buffer_gets,hash_value
2 from v$sql where sql_text='select count(*) from emp';
SQL_TEXT EXECUTIONS DISK_READS OPTIMIZER_ BUFFER_GETS HASH_VALUE
------------------------------ ---------- ---------- ---------- ----------- ----------
select count(*) from emp 1 5 ALL_ROWS 7 2295140356
记录的信?AUTOTRACE显示的信?全一致?在?次执行时?strong>这个SQL?span xml:lang="EN-US">HASH_VALUE??算出来为2295140356,并且随之,这个SQL的父指针?span xml:lang="EN-US">Parent Cursor)在内存??创建,一?指针同时创建?strong>父指针可以?认为?span xml:lang="EN-US">Hash Value的相关信?子指针可以?认为?span xml:lang="EN-US">SQL的元数据?
再?执?这个查?,统计信?的物理??span xml:lang="EN-US">DISK_READS)不再?加,因为数据已经?span xml:lang="EN-US">Buffer???span xml:lang="EN-US">BUFFER_GETS继续增加?strong>执?次数也变?span xml:lang="EN-US">2?/span>?/span>
[email protected]> select count(*) from emp;
COUNT(*)
----------
14
Execution Plan
----------------------
Plan hash value: 2083865914
-------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| EMP | 14 | 3 (0)| 00:00:01 |
-------------------------------
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
411 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
[email protected]> select sql_text,executions,disk_reads,optimizer_mode,buffer_gets,hash_value
2 from v$sql where sql_text='select count(*) from emp';
SQL_TEXT EXECUTIONS DISK_READS OPTIMIZER_ BUFFER_GETS HASH_VALUE
------------------------------ ---------- ---------- ---------- ----------- ----------
select count(*) from emp 2 5 ALL_ROWS 10 2295140356
v$sqlarea视图也是非常重?的一??图,?span xml:lang="EN-US">Oracle 9iR2的文档中?span xml:lang="EN-US">Oracle这样定义这个视图?strong>v$sqlarea列出了共?span xml:lang="EN-US">SQL区(Shared SQL Area)中?span xml:lang="EN-US">SQL统?信息,这?span xml:lang="EN-US">SQL按照SQL文本的不?/span>,每条会记录?行统计数?/span>?strong>注意这里?说的?span xml:lang="EN-US">?/span>按照SQL文本?/span>来进行区分,也就??这个视图的信?以看作是根据SQL_TEXT进?的一次汇总统?/strong>?
v$sqlarea视图的主要字段?下:
Column | Datatype | Description |
SQL_TEXT | VARCHAR2(1000) | 当前指针的前1000??/span> |
VERSION_COUNT | NUMBER | Cache??指针下存在的子指针的数量 |
EXECUTIONS | NUMBER | 总的执?次数,包?有子指针执?次数的汇?/span> |
DISK_READS | NUMBER | ?有子指针?span xml:lang="EN-US">Disk Reads总和 |
BUFFER_GETS | NUMBER | ?有子指针?span xml:lang="EN-US">Buffer Gets总和 |
OPTIMIZER_MODE | VARCHAR2(10) | SQL执?的优化器?/span> |
HASH_VALUE | NUMBER | 父指针的Hash Value |
通过前文?知道,文?同的SQL?,在数据库中的意义可能完全不同?比如数??在两??strong>TQ?strong>DBTAN,两?户各拥有?张数?EMP?
那么当两?户发出一??span xml:lang="EN-US">select count(*) from emp时,这个查?访问的?象,返回的结果可能完全不同,TQ的查询???span xml:lang="EN-US">TQ.EMP??span xml:lang="EN-US">DBTAN用户访问的则?span xml:lang="EN-US">DBTAN.EMP表?但??span xml:lang="EN-US">SQL_TEXT上来说,这两?span xml:lang="EN-US">SQL没有任何区别?
继续前面的测试,再来?单看?下以下的输出?/span>
[email protected]> create table emp as select * from scott.emp where rownum <9;
Table created.
[email protected]> set autotrace on
[email protected]> select count(*) from emp;
COUNT(*)
----------
8
Execution Plan
----------------------
Plan hash value: 2083865914
-------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| EMP | 8 | 3 (0)| 00:00:01 |
-------------------------------
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------
5 recursive calls
0 db block gets
7 consistent gets
5 physical reads
0 redo size
411 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
现在v$sql?该有了两条完全一样的SQL,但??询的物理对象却是?不同?/span>
[email protected]> select sql_text,executions,disk_reads,optimizer_mode,buffer_gets,hash_value
2 from v$sql where sql_text='select count(*) from emp';
SQL_TEXT EXECUTIONS DISK_READS OPTIMIZER_ BUFFER_GETS HASH_VALUE
------------------------------ ---------- ---------- ---------- ----------- ----------
select count(*) from emp 3 5 ALL_ROWS 13 2295140356
select count(*) from emp 1 5 ALL_ROWS 7 2295140356
现在再来查?v$sqlarea视图,就可以看到这两个视图的不同:
[email protected]> select sql_text,executions,disk_reads,buffer_gets,hash_value,version_count
2 from v$sqlarea where sql_text='select count(*) from emp';
SQL_TEXT EXECUTIONS DISK_READS BUFFER_GETS HASH_VALUE VERSION_COUNT
------------------------------ ---------- ---------- ----------- ---------- -------------
select count(*) from emp 4 10 20 2295140356 2
在这??图中?span xml:lang="EN-US">Oracle?span xml:lang="EN-US">v$sql?sql_text相同?span xml:lang="EN-US">2?指针合并起来,执行?数等信息也都进?了累计,version_count也显示为2,这就是v$sqlarea的聚合作用??