v$sql、v$sqlarea、v$sqltext、v$sql_plan

--v$sqltext
--Concept:This view contains the text of SQL statements belonging to shared SQL cursors in the SGA.
--记录完整的sql,但sql被分片存储
/*
 Name             Null?    Type
 ----------------------------------------- -------- ----------------------------
 ADDRESS              RAW(4)
 HASH_VALUE              NUMBER            --address、hash_value唯一标识一条sql
 SQL_ID               VARCHAR2(13)
 COMMAND_TYPE              NUMBER
 PIECE                NUMBER               --PIECE分片之后的顺序
 SQL_TEXT					    VARCHAR2(64)         --SQL_TEXT为sql文本
 */
select * from v$sqltext;

--v$sqlarea
--Concept:V$SQLAREA lists statistics on shared SQL area and contains one row per SQL string. It provides statistics on SQL statements that are in memory,
-- parsed, and ready for execution.
--记录共享sql区中的统计信息(执行次数、逻辑读、物理读等);相同文本的sql只记录一次
/*
 Name             Null?    Type
 ----------------------------------------- -------- ----------------------------
 SQL_TEXT              VARCHAR2(1000)
 SQL_FULLTEXT              CLOB
 SQL_ID               VARCHAR2(13)
 SHARABLE_MEM              NUMBER
 PERSISTENT_MEM             NUMBER
 RUNTIME_MEM              NUMBER
 SORTS                NUMBER
 VERSION_COUNT              NUMBER
 LOADED_VERSIONS            NUMBER
 OPEN_VERSIONS              NUMBER
 USERS_OPENING              NUMBER
 FETCHES              NUMBER
 EXECUTIONS              NUMBER
 PX_SERVERS_EXECUTIONS            NUMBER
 END_OF_FETCH_COUNT            NUMBER
 USERS_EXECUTING            NUMBER
 LOADS                NUMBER
 FIRST_LOAD_TIME            VARCHAR2(76)
 INVALIDATIONS              NUMBER
 PARSE_CALLS              NUMBER
 DISK_READS              NUMBER
 DIRECT_WRITES              NUMBER
 BUFFER_GETS              NUMBER
 APPLICATION_WAIT_TIME            NUMBER
 CONCURRENCY_WAIT_TIME            NUMBER
 CLUSTER_WAIT_TIME            NUMBER
 USER_IO_WAIT_TIME            NUMBER
 PLSQL_EXEC_TIME            NUMBER
 JAVA_EXEC_TIME             NUMBER
 ROWS_PROCESSED             NUMBER
 COMMAND_TYPE              NUMBER
 OPTIMIZER_MODE             VARCHAR2(10)
 OPTIMIZER_COST             NUMBER
 OPTIMIZER_ENV              RAW(797)
 OPTIMIZER_ENV_HASH_VALUE          NUMBER
 PARSING_USER_ID            NUMBER
 PARSING_SCHEMA_ID            NUMBER
 PARSING_SCHEMA_NAME            VARCHAR2(30)
 KEPT_VERSIONS              NUMBER
 ADDRESS              RAW(4)
 HASH_VALUE              NUMBER
 OLD_HASH_VALUE             NUMBER
 PLAN_HASH_VALUE            NUMBER
 MODULE               VARCHAR2(64)
 MODULE_HASH              NUMBER
 ACTION               VARCHAR2(64)
 ACTION_HASH              NUMBER
 SERIALIZABLE_ABORTS            NUMBER
 OUTLINE_CATEGORY            VARCHAR2(64)
 CPU_TIME              NUMBER
 ELAPSED_TIME              NUMBER
 OUTLINE_SID              VARCHAR2(40)
 LAST_ACTIVE_CHILD_ADDRESS			    RAW(4)
 REMOTE 					    VARCHAR2(1)
 OBJECT_STATUS					    VARCHAR2(19)
 LITERAL_HASH_VALUE				    NUMBER
 LAST_LOAD_TIME 				    DATE
 IS_OBSOLETE					    VARCHAR2(1)
 CHILD_LATCH					    NUMBER
 SQL_PROFILE					    VARCHAR2(64)
 PROGRAM_ID					    NUMBER
 PROGRAM_LINE#					    NUMBER
 EXACT_MATCHING_SIGNATURE			    NUMBER
 FORCE_MATCHING_SIGNATURE			    NUMBER
 LAST_ACTIVE_TIME				    DATE
 BIND_DATA					    RAW(2000)
*/
select * from v$sqlarea

--v$sql
--Concept:V$SQL lists statistics on shared SQL area without the GROUP BY clause and contains one row for each child of the original SQL text entered. 
--Statistics displayed in V$SQL are normally updated at the end of query execution. However, for long running queries, they are updated every 5 seconds.
-- This makes it easy to see the impact of long running SQL statements while they are still in progress.
--记录共享sql区中的统计信息(执行次数、逻辑读、物理读等);每一次执行sql语句都会记录,且与v$sql_plan关联可查询每条sql语句的执行计划。
/*
 Name					   Null?    Type
 ----------------------------------------- -------- ----------------------------
 SQL_TEXT					    VARCHAR2(1000)
 SQL_FULLTEXT					    CLOB
 SQL_ID 					    VARCHAR2(13)
 SHARABLE_MEM					    NUMBER           --占用共享内存的大小
 PERSISTENT_MEM 				    NUMBER         --生命周期内的固定内存大小
 RUNTIME_MEM					    NUMBER           --执行期内的固定内存大小
 SORTS						    NUMBER               --完成的排序数
 LOADED_VERSIONS				    NUMBER
 OPEN_VERSIONS					    NUMBER
 USERS_OPENING					    NUMBER         --执行语句的用户数
 FETCHES					    NUMBER               --sql语句的fetch数
 EXECUTIONS					    NUMBER             --自它被载入缓存库后的执行次数
 PX_SERVERS_EXECUTIONS				    NUMBER
 END_OF_FETCH_COUNT				    NUMBER
 USERS_EXECUTING				    NUMBER         --执行语句的用户数
 LOADS						    NUMBER               --对象被载入过的次数
 FIRST_LOAD_TIME				    VARCHAR2(76)   --初次载入时间
 INVALIDATIONS					    NUMBER         --无效的次数
 PARSE_CALLS					    NUMBER           --解析调用次数
 DISK_READS					    NUMBER             --读磁盘数
 DIRECT_WRITES					    NUMBER
 BUFFER_GETS					    NUMBER           --读缓存区数
 APPLICATION_WAIT_TIME				    NUMBER
 CONCURRENCY_WAIT_TIME				    NUMBER
 CLUSTER_WAIT_TIME				    NUMBER
 USER_IO_WAIT_TIME				    NUMBER
 PLSQL_EXEC_TIME				    NUMBER
 JAVA_EXEC_TIME 				    NUMBER
 ROWS_PROCESSED 				    NUMBER
 COMMAND_TYPE					    NUMBER
 OPTIMIZER_MODE 				    VARCHAR2(10)
 OPTIMIZER_COST 				    NUMBER
 OPTIMIZER_ENV					    RAW(797)
 OPTIMIZER_ENV_HASH_VALUE			    NUMBER
 PARSING_USER_ID				    NUMBER
 PARSING_SCHEMA_ID				    NUMBER
 PARSING_SCHEMA_NAME				    VARCHAR2(30)
 KEPT_VERSIONS					    NUMBER
 ADDRESS					    RAW(4)
 TYPE_CHK_HEAP					    RAW(4)
 HASH_VALUE					    NUMBER
 OLD_HASH_VALUE 				    NUMBER
 PLAN_HASH_VALUE				    NUMBER
 CHILD_NUMBER					    NUMBER
 SERVICE					    VARCHAR2(64)
 SERVICE_HASH					    NUMBER
 MODULE 					    VARCHAR2(64)
 MODULE_HASH					    NUMBER
 ACTION 					    VARCHAR2(64)
 ACTION_HASH					    NUMBER
 SERIALIZABLE_ABORTS				    NUMBER
 OUTLINE_CATEGORY				    VARCHAR2(64)
 CPU_TIME					    NUMBER
 ELAPSED_TIME					    NUMBER
 OUTLINE_SID					    NUMBER
 CHILD_ADDRESS					    RAW(4)
 SQLTYPE					    NUMBER
 REMOTE 					    VARCHAR2(1)
 OBJECT_STATUS					    VARCHAR2(19)
 LITERAL_HASH_VALUE				    NUMBER
 LAST_LOAD_TIME 				    VARCHAR2(76)
 IS_OBSOLETE					    VARCHAR2(1)
 CHILD_LATCH					    NUMBER
 SQL_PROFILE					    VARCHAR2(64)
 PROGRAM_ID					    NUMBER
 PROGRAM_LINE#					    NUMBER
 EXACT_MATCHING_SIGNATURE			    NUMBER
 FORCE_MATCHING_SIGNATURE			    NUMBER
 LAST_ACTIVE_TIME				    DATE
 BIND_DATA					    RAW(2000)
*/
select * from v$sql;
--注:可根据视图v$sql中的plan_hash_value直接判断sql语句的执行计划是否相同

--v$sql_plan
--Concept:V$SQL_PLAN contains the execution plan information for each child cursor loaded in the library cache.
--记录每条sql语句的执行计划
/*
 Name					   Null?    Type
 ----------------------------------------- -------- ----------------------------
 ADDRESS					    RAW(4)
 HASH_VALUE					    NUMBER
 SQL_ID 					    VARCHAR2(13)
 PLAN_HASH_VALUE				    NUMBER
 CHILD_ADDRESS					    RAW(4)
 CHILD_NUMBER					    NUMBER
 TIMESTAMP					    DATE
 OPERATION					    VARCHAR2(120)
 OPTIONS					    VARCHAR2(120)
 OBJECT_NODE					    VARCHAR2(160)
 OBJECT#					    NUMBER
 OBJECT_OWNER					    VARCHAR2(30)
 OBJECT_NAME					    VARCHAR2(30)
 OBJECT_ALIAS					    VARCHAR2(65)
 OBJECT_TYPE					    VARCHAR2(80)
 OPTIMIZER					    VARCHAR2(80)
 ID						    NUMBER
 PARENT_ID					    NUMBER
 DEPTH						    NUMBER
 POSITION					    NUMBER
 SEARCH_COLUMNS 				    NUMBER
 COST						    NUMBER
 CARDINALITY					    NUMBER
 BYTES						    NUMBER
 OTHER_TAG					    VARCHAR2(140)
 PARTITION_START				    VARCHAR2(20)
 PARTITION_STOP 				    VARCHAR2(20)
 PARTITION_ID					    NUMBER
 OTHER						    VARCHAR2(4000)
 DISTRIBUTION					    VARCHAR2(80)
 CPU_COST					    NUMBER
 IO_COST					    NUMBER
 TEMP_SPACE					    NUMBER
 ACCESS_PREDICATES				    VARCHAR2(4000)
 FILTER_PREDICATES				    VARCHAR2(4000)
 PROJECTION					    VARCHAR2(4000)
 TIME						    NUMBER
 QBLOCK_NAME					    VARCHAR2(30)
 REMARKS					    VARCHAR2(4000)
 OTHER_XML					    CLOB
*/
select * from v$sql_plan;

--注:看似相同的sql语句,往往具有不同的sql执行计划,而v$sql_area恰恰忽略了sql执行计划的差异。

--应用举例
--查看资源消耗最多的sql
select *
from v$sqlarea
where buffer_gets>10000000 or disk_reads>1000000
order by buffer_gets+100*disk_reads desc;
--查看使用频率最高的前5条sql
select address,hash_value,sql_text,executions
from (
select row_number() over(order by executions desc) as top,address,hash_value,sql_text,executions
from v$sql
) t
where t.top<=5;
--查看某条语句的资源消耗情况
select address,hash_value,sql_text,disk_reads,buffer_gets,round((buffer_gets-disk_reads)/buffer_gets,4)*100 as "hits(%)"
,round(cpu_time/executions,4) as "cpu(秒/次)",round(elapsed_time/executions,4) as "execute(秒/次)",executions
from v$sqlarea
where address=hextoraw('4DC41CA4') and hash_value=864012087
--查看磁盘读取频率最高的前5条sql
select address,hash_value,sql_text,disk_reads
from (
select row_number() over(order by disk_reads desc) as top,address,hash_value,sql_text,disk_reads
from v$sql
) t
where t.top<=5;
--查看缓冲区读取频率最高的前5条sql
select address,hash_value,sql_text,buffer_gets
from (
select row_number() over(order by buffer_gets desc) as top,address,hash_value,sql_text,buffer_gets
from v$sql
) t
where t.top<=5;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值