转:V$SQL,V$SQLAREA,V$SQLTEXT

V$SQL*表用于查看Shared SQL Area中SQL情况
V$SQLTEXT
V$SQLTEXT用途很简单,就是用来查看完整的SQL语句,V$SQL和V$SQLAREA只能显示1000 bytes,且特殊字符用空格替代。
This view contains the text of SQL statements belonging to shared SQL cursors in the SGA.
Column    Datatype    Description
ADDRESS    RAW(4 | 8)    Used with HASH_VALUE to uniquely identify a cached cursor
HASH_VALUE    NUMBER    Used with ADDRESS to uniquely identify a cached cursor
SQL_ID    VARCHAR2(13)    SQL identifier of a cached cursor
COMMAND_TYPE    NUMBER    Code for the type of SQL statement (SELECT, INSERT, and so on)
PIECE    NUMBER    Number used to order the pieces of SQL text
SQL_TEXT    VARCHAR2(64)    A column containing one piece of the SQL text

Example:
SELECT 'PTIAN' FROM DUAL;  
  
SELECT * FROM V$SQLTEXT WHERE SQL_TEXT LIKE '%PTIAN%';   

Output:
ADDRESS    HASH_VALUE    SQL_ID    COMMAND_TYPE    PIECE    SQL_TEXT
000000008774FF80    2034677615    57zcfn1wnddvg    3    0    select * from v$sqltext where SQL_TEXT LIKE '%PTIAN%'
000000008760AC20    2521097521    ak2vyjub49t9j    3    0    SELECT 'PTIAN' FROM DUAL

V$SQL & V$SQLAREA
V$SQL , V$SQLAREA很类似,看下Tom的解释:
v$sql the details -- if you have multiple copies of the query:
"select * from T" 
in your shared pool, v$sql will have a row per query.  This can happen if user U1 and user U2 both have a table T and both issue "select * from T".  Those are entirely different queries with different plans and so on.  v$sql will have 2 rows. 
v$sqlarea is a aggregate of v$sql.  It selects out DISTINCT sql.  "select * from T" will appear there.
v$sqltext is simply a way to see the entire query.  the v$sql and v$sqlarea views only show the first 1000 bytes.  newlines and other control characters are replace with whitespace.
v$sqltext_with_newlines is v$sqltext without the whitespace replacment.

v$sql     ----------  存储的是具体的SQL 和执行计划相关信息,实际上,v$sqlarea 可以看做 v$sql 根据 sqltext 等 做了 group by 之后的信息
v$sqlarea   ---------  存储的SQL 和一些相关的信息,比如累计的执行次数,逻辑读,物理读等统计信息
而v$SQLAREA 忽略了  执行计划 等差异,只是在形式上sql文本看起来一样!相当于做了个聚合,是多个不同执行计划的sql的聚合和累计信息
(refer:http://www.itpub.net/forum.php?mod=viewthread&tid=181450&page=1)

v$sql与v$sqlarea的源都是一个:X$KGLCURSOR
实际调优中建议使用v$sql,相对来说比v$sqlarea快,而且还不会产生share pool latch的争用
(refer:http://www.itpub.net/thread-522899-1-1.html)

共同点:
1)都存储了sql内容
2)  记录的都是位于内存中的sql内容
3)  因为是内存,所以都不保留历史记录

不同点:
1)存储的为止不都是相同。其中v$sql和v$sqlarea存储的sql都是位于shared sql area中的sql,而v$sqltext是位于sga中的sql。但文档没有明确说明这里的sga是否还包含了psa(私有sql区域--共享服务器模式下)。
2)存储sql的方式也不同,v$sql和v$sqlarea都是用一行来存储sql全文,而v$sqltext用一行存储sql的一行。
3)v$sql不存储包含group by 的sql语句。通常这个视图,在每个查询执行完成后更新,但对于执行很久的sql,它是每5秒更新一次,这点对于查看sql执行状态是有意义的。
4)存储的明细不同--这是最基本的。

最后顺便提一下,v$sqltext_with_newline和v$sqltext是一样,不同的是前者并把换行符和tab替换为空格 ,这样更容易阅读。V$SQLSTATS和V$SQL,v$sqlarea一样都有提供CPU的统计数据.
(Refer:http://lzfhope.blog.163.com/blog/static/63639922008101955913105/)


一些用法:
1.Find top 5 queries with most disk_reads
SELECT SESION.SID,  
       SESION.USERNAME,  
       OPTIMIZER_MODE,  
       HASH_VALUE,  
       ADDRESS,  
       CPU_TIME,  
       ELAPSED_TIME,  
       DISK_READS,  
       DIRECT_WRITES,  
       SQL_TEXT  
  FROM V$SQLAREA SQLAREA, V$SESSION SESION  
 WHERE SESION.SQL_HASH_VALUE = SQLAREA.HASH_VALUE  
   AND SESION.SQL_ADDRESS    = SQLAREA.ADDRESS  
   AND SESION.USERNAME IS NOT NULL  
   AND ROWNUM < 6   
   ORDER BY DISK_READS DESC,ELAPSED_TIME DESC;  
2.See what SQL users are running on the system
select a.sid, a.serial#, b.sql_text  
from v$session a, v$sqlarea b  
where a.sql_address=b.address  
and a.username='GRUMPY';  
   
       SID    SERIAL# SQL_TEXT  
   
---------- ---------- ----------------------------------------------  
       122      61521 select count(*) from gen_person where gen_person_id=95000  

 

转载于:https://www.cnblogs.com/toowang/p/3665791.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值