tuning 03 Sizing the Share pool

share pool : (组成)
library cache: stores shared sql and pl/sql code (包含 statement text, parsed code, execution plan)
data dictionary cache : keeps information about dictionary objects. (包含 definitions for tables, columns, and privileges from the data dictionary tables)
调优 share pool 考虑的内容:
A cache miss on the data dictionary cache or library cache is more expensive than a miss on the database buffer cache. Tuning the shared pool is a priority.
当你调优 share pool 时, 你首先要集中精神调优 library cache, 因为 语法上, 倾向于保存 data dictionary data 在内存中的时间要大于 library cache data. 所以, 如果 library cache 的命中率可以接受的同时, data dictionary cache 也必然是可以接受的.
shared_pool_size 是用来定义 share pool 大小的参数
在 share pool 中存储的 sql 和 pl/sql 是对所有用户共享的, 这样可以避免重复解析, 另外这里使用的算法是 LRU (least recently used)
If a user issues a statement that is already in the cache, the Oracle server can use the cached version without having to reparse it.
To find whether a statement is already cached, the Oracle server:
- Reduces the statement to the numeric value of the ASCII text
- Uses a hash function of this number
那么要如何调优 library cache 呢?
- Make sure that users can share statements (As much generic code as possible, Bind variables rather than constants)
- Prevent statements from being aged out by allocating enough space
- Avoid invalidations that induce reparsing
注: If a schema object is referenced in a sSQL statement and that object is later modified in any way, the SQL statement held in memory is rendered invalid.
一些术语: Terminology
Gets: (Parse) The number of lookups for objects of the newspace (搜索时需要的资源)
Pins: (Execution) The number of reads or executions of the objects of the namespace (执行SQL statement时需要的资源)
Reloads: (Reparse) The number of library cache misses on the execution step, causing implicit reparsing of the statement and block. (重解析时, 需要的资源)
在 v$librarycache 中的每一行, 都包含着一个namespace 统计信息, 这里的namespace 就对应了内存中的一个空间(这个空间就好比一个房子, 卧室是一个namespace, 它的名字是卧室, 客厅也是一个空间, 它的名字是客厅)
一些重要的动态视图:
v$sgastat , 用来显示 SGA structure, The contents of the shared pool are not aged out as long as free memory is avalilable in the shared pool.
v$librarycache, statistics on library cache management
v$sqlarea, Full statistics about all shared cursors, and the first 1000 characters of the SQL statement
v$sqltext, The full SQL text without truncation, in multiple rows
v$db_object_cache: Database objects cached, including packages; also objects such as tables and synonyms, where these are referenced in SQL statements.
在 v$librarycache 这个视同中的 GETHITRATIO determines the percentage of parse calls that find a cursor to share(GETHITS/GETS). This ratio should be in the high 90s in OLTP environments. If not, you can improve the efficency of your application code.
select sum(pins) "Executions", sum(reloads) "Cache Misses", sum(reloads)/sum(pins) from v$librarycache;
如果以上sql语句执行结果大于1%, 那么就要增加 SHARED_POOL_SIZE.
When Invalidations Occur
If a schema object is referenced in a SQL statement and that object is later modified in any way, the shared SQL area becomes invalidated(marked as invalid), and the statement must be reparsed the next time it is executed, and therefore reloaded.
For example, when a table, sequence, synonym or view is re-created or altered or dropped, or a procedure or package specification is recompiled, all dependent shared SQL areas are invalidated.
Example:
select count(*) from hr.employees;
select namespace, pins, reloads, invalidations from v$librarycache; -- 这时 invalidation 是 0
analyze table hr.employees compute statistics;
select count(* ) from hr.employees;
select namespace, pins, reloads, invalidations from v$librarycache; -- 这时 invalidation 是 2, 其余参数 reloads 等也都增加了
Cached Execution Plans
这个是真正解析后的, oracle 执行 SQL 语句的步骤
With this feature, the Oracle server preserves the actual execution plan of a cached SQL statement in memory. 注意, 这个执行计划必须已经是cache中的了, 所以之前说的 SQL 语句调优, 首先要保证 shared pool 的命中率, 后面的是在命中率的基础上, 再调优 SQL 语句.
When the SQL statement ages out of the library cache, the corresponding cached execution plan is removed.
The main benefit of this feature is better diagnosis of query performance. 这个执行计划对 SQL语句的调优很有用.
v$sql_plan, can be used to view the actual execution plan information for cached cursors.
还有一个 table 跟这个视图很类似, PLAN_TABLE

UGA 不用考虑,是给 shared server 准备的, dedicate server 不用
library cache 是用来解析 sql 的,把文本的sql语句编译成可执行的文件

自动内存管理参数 SGA_TARGET, 就是说, 你只要指定这么一个参数就可以了. 这样 SGA 里的组件都会自动指定.
SGA_TARGET 参数,指定整个SGA大小,之后的很多参数会被自动指定,oracle会自己优化
alter system set shared_pool_size=10000 scope=both

最大指标: 尽量多的使用已经解析的sql语句
olap: 给领导用的,用户少,但是查询的量都比较大,不太注意reparsing,因为查询的sql语句复杂,一般都需要reparsing。因为sql语句少, 所以肯定可重复利用的就不多.
oltp: 给n多人用,并发量大, 所以要注意尽量避免 reparsing 

两条语句共享一个sql parse代码
注释,空格,等等都要必须一摸一样,才能被认为是同样的,例如下边的不能共享

上面设定了参数 CURSOR_SHARING 如果设置成 similar, 那么上例中的两个一样的SQL语句就可以不经过解析.

注意 schema, 因为有可能 employees 这个表在多个schema中都有, 比如你写 select * from employees; 但是如果是另一个用户也这么写, 而那个用户自己也有employees表, 那么这两条sql语句不能共享.
绑定变量的名字要一样,绑定变量对提高性能很有用


set timing on
每次执行, 都会报告执行时间
接下来执行了两个存储过程, 一个是使用绑定变量的, 一个是不使用绑定变量, 执行出来的结果插入10000条记录, (0.77 秒, 3.60秒, 可以看到是5倍的关系)
-- 使用绑定变量的
create or replace procedure proc1
begin
for i in 1..10000
loop
execute immediate ' insert into m values(:x)' USING i;
end loop;
end;
/
-- 不使用绑定变量的
create or replace procedure proc2
begin
for i in 1..10000
loop
execute immediate 'insert into m values('||i||')';
end loop;
end;
/

Latch 是一种锁. 内存锁

增大 library cache 大小.
确认 sql 语句的问题, 为什么不能共享 sql parse.

调优 library 总的指导原则是, 尽量少的 parse .

v$librarycache

v$librarycache

v$sgastat 里有个重要指标就是 shared pool free memory
以上的各种动态视图, 需要各种查询联机文档.

数据仓库是OLAP,是用来分析的,跟OLTP不一样,目前我们使用的是OLTP



尽量避免使用动态SQL语句





select namespace, gethitration, pinhitratio, reloads, invalidations from v$librarycache;

只看前 4 行就可以了.

这个参数上边已经提到过, 如果你没有能力调优SQL了, 你可以将这个参数设置成 similar, 当然尽量不改, 因为改了以后会有副作用.

v$librarycache 中的值都是累积的, 所以我们可以参考 statspack 的方法, 找一个时间段, 看这个时间段之内的值, 这样就比较准确.











select free_space, requests, request_misses, request_failures from v$shared_pool_reserved;











例子:






tuning 03 Sizing the Share pool的更多相关文章
- oracle 内存结构 share pool sql解析的过程
1.sql解析的过程 oracle首先将SQL文本转化为ASCII字符,然后根据hash函数计算其对应的hash值(hash_value).根据计算出的hash值到library cache中找到对应 ...
- 共享内存 share pool (2):BUCKET /FREE LISTS /RESERVED FREE LISTS /UNPINNED RECREATABLE CHUNKS (lru first)
相关概念 BUCKET :每个bucket上挂有一个 chunk list.同一个BUCKET中的chunk在物理地址上是不一定相邻的 FREE LISTS:按bucket划分,共有255个,buck ...
- 共享内存 share pool (1):heap /extent /chunk/
相关概念 CHUNK: Shared pool物理层面上由许多内存块组成,这些内在块称为chunk.但是chunk是大小不一的,在内存中一个chunk是连续的. EXTENT:由多个连续的chunk组 ...
- share pool 管理机制
Library cache是Shared pool的一部分,它几乎是Oracle内存结构中最复杂的一部分,主要存放shared curosr(SQL)和PLSQL对象(function,procedu ...
- Tuning 04 Sizing the Buffer Cache
Buffer Cache 特性 The buffer cache holds copies of the data blocks from the data files. Because the bu ...
- Tuning 05 Sizing other SGA Structure
Redo Log Buffer Content The Oracle server processes copy redo entries from the user’s memory space t ...
- (转载)处理SQL解析失败导致share pool 的争用
通过关联x$kglcursorx$kglcursor_child_sqlid视图: 通过使用Oracle10035Event事件可以找到解析失败的SQL: 通过oraclesystemdump也可以找 ...
- SPA测试
1.生产端:环境准备为了进行SPA测试,在生产数据库中创建了SPA测试专用用户,避免与其他用户相互混淆与可能产生的误操作. CREATE USER SPA IDENTIFIED BY SPA DEFA ...
- Improve Scalability With New Thread Pool APIs
Pooled Threads Improve Scalability With New Thread Pool APIs Robert Saccone Portions of this article ...
随机推荐
- python读取中文
如何从文件中读取300个汉字? 看起来很简单,但很容易掉坑里了. 一开始我这么写: try: fd = codecs.open(os.path.join(settings.TEXT_CONTENT_D ...
- Android 事件分发
引言 项目中涉及到的触摸事件分发较多,例如:歌词模式下,上下滑动滚动歌词,左右滑动切换歌曲.此时,理解事件分发机制显得尤为重要 , 既要保证下方的ViewPager能接收到,又要确保上层View能响应 ...
- 算法笔记_054:Prim算法(Java)
目录 1 问题描述 2 解决方案 2.1 贪心法 1 问题描述 何为Prim算法? 此处引用网友博客中一段介绍(PS:个人感觉网友的这篇博客对于Prim算法讲解的很清楚,本文与之相区别的地方在于具 ...
- python——python数据结构之栈、队列的实现
这个在官网中list支持,有实现. 补充一下栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIF ...
- Vue实例初始化的选项配置对象详解
Vue实例初始化的选项配置对象详解 1. Vue实例的的data对象 介绍 Vue的实例的数据对象data 我们已经用了很多了,数据绑定离不开data里面的数据.也是Vue的核心属性. 它是Vue绑定 ...
- Java之内部类(1) - 为什么需要内部类
为什么需要内部类 一般来说,内部类继承自某个类或实现某个接口,内部类的代码操作创建它的外围类的对象.所以可以认为内部类提供了某种进入其外围类的窗口. 内部类必须要回答的一个问题是:如果只是需要一个对接 ...
- 增强for循环、Map接口遍历、可变參数方法
增强for循环 1.for循环能做得事情.增强for循环大部分都能做(假设要想获得下标的时候就必须使用简单for循环了) 2.增强for有时候可以方便的处理集合遍历的问题,可是集合的标准遍历是使用迭代 ...
- 09-spring学习-资源访问接口
目标: 1,掌握Resource接口的使用. 2,掌握ResourceLoader接口的使用. 3,掌握各种资源数据的读取操作. 具体内容: 要想进行资源读取操作,首先想到IO包中提供的操作类. 但是 ...
- Oracle,用left join 替代 exists ,not exists,in , not in,提高效率
Not IN问题 Exists,not Exists,in,not in 例如: FROM YSHB B WHERE YSHA.code=b.code ) 等同于 DELETE A FROM YSHA ...
- Oracle中查询某字段不为空或者为空的SQL语句怎么写
比如 insert into table a (a1,b1)values("a1",''); 对于这种情况,因为表里存的是'',其实是没有内容的,要查询这个字段,不能直接使用 se ...