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;
例子: