ITPub博客

首页 > Linux操作系统 > Linux操作系统 > tuning 第七章(四)

tuning 第七章(四)

原创 Linux操作系统 作者:to_be_dba 时间:2013-01-09 18:44:10 0 删除 编辑
六、管理服务器和客户端结果集缓存result caches

result cache是一块内存,在SGA或用户应用内存中,存储查询结果或查询块以供以后使用。这些缓存行可以被其他语句或会话,指导过旧。

1.管理服务器结果缓存
是共享池中的一块内存池。该池包含了sql和pl/sql的结果缓存。

olap应用中服务器结果缓存效果可以很好,主要取决于应用。在数据仓库中,高效的缓存接受大量行但只返回一小部分结果。

服务器结果缓存的工作原理:
查询执行时,数据库查看缓存内存来判断结果是否存在于缓存中。如果存在,数据库从内存返回结果,而不再执行查询;如果没有缓存,就需要执行查询,返回结果并将其存在缓存中。
重复执行查询语句时,从缓存获得数据减少了响应时间。
当数据库对象被修改后,以前的缓存就不可用了。


如:
SQL> select /*+result_cache*/* from t;
Elapsed: 00:00:00.43

Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873

-------------------------------------------------------------------------------------------------

| Id  | Operation          | Name                       | Rows  | Bytes | Cost (%CPU)| Time     |

-------------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT   |                            |     1 |   503 |    31  (0)| 00:00:01 |
|   1 |  RESULT CACHE      | by08hcu3d8mnd6kdhdpa7t672f |       |       |     |          |
|   2 |   TABLE ACCESS FULL| T                          |     1 |   503 |    31  (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------


Result Cache Information (identified by operation id):
------------------------------------------------------

   1 - column-count=55; dependencies=(SCOTT.T); name="select /*+result_cache*/*
from t"


Note
-----
   - dynamic sampling used for this statement (level=2)

SQL> r
  1* select /*+result_cache*/* from t
Elapsed: 00:00:00.00

Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873

-------------------------------------------------------------------------------------------------

| Id  | Operation          | Name                       | Rows  | Bytes | Cost (%CPU)| Time     |

-------------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT   |                            |     1 |   503 |    31  (0)| 00:00:01 |
|   1 |  RESULT CACHE      | by08hcu3d8mnd6kdhdpa7t672f |       |       |     |          |
|   2 |   TABLE ACCESS FULL| T                          |     1 |   503 |    31  (0)| 00:00:01 |

-------------------------------------------------------------------------------------------------


Result Cache Information (identified by operation id):
------------------------------------------------------

   1 - column-count=55; dependencies=(SCOTT.T); name="select /*+result_cache*/*
from t"


Note
-----
   - dynamic sampling used for this statement (level=2)


上面的例子示意证明从缓存获取数据更快。

但是,根据官方文档,这时候应该可以从v$result_cache_objects中通过执行计划中得到的缓存名查到数据,但实验中此表无数据。

在另外一个测试系统中,则得到了相应结果。

    ID TYPE STATUS NAME NAMESPACE CACHE_ID
1 4 Dependency Published    CMP.T     CMP.T
2 6 Result Published "select /*+result_cache*/* from t where wner='SCOTT'" SQL 7frjx3fvby1gw5307kwpxf5duz
3 5 Result Published "select /*+result_cache*/* from t where table_name='DBA_TABLES'" SQL gwfcmpndj4b9k75xbujp1pphzh
4 7 Result Invalid "select /*+result_cache*/* from t where wner='SYS'" SQL 6t0dg2f1vn6mn00z4hp37gsfdp


服务器结果缓存初始化参数:
result_cache_max_size设置为服务器结果缓存分配的内存。
result_cache_max_result每个结果可以使用的最大服务器结果缓存数量。默认是5%,但可以在会话级在1~100间设置
result_cache_remote_expiration远程数据库对象在服务器结果缓存中保存的过期时间,默认是0,表示远程对象不缓存
可以使用初始化参数:
数据库启动时,从共享池分配内存给服务器结果缓存。大小取决于共享池以及系统管理的内存量。算法是:
当使用memory_target明确指定内存时,分配0.25%给结果缓存
当使用sga_target参数设置共享池大小时,分配sga_target的0.5%给结果缓存
当设置了shared_pool_size时,分配共享池1%的内存给结果缓存

server result cache增长指导最大大小。超过此大小的不缓存。数据库使用LRU算法将旧数据aged out,但不会自动释放。使用dbms_result_cache.flush清除。


设置result_cache_max_size可以改变参数的值。RAC环境下,result cache在各个实例间可以区别定义。


dbms_result_cache的用法:
1)查看服务器结果缓存
SQL> set serveroutput on;
SQL>  exec dbms_result_cache.memory_report;

R e s u l t   C a c h e   M e m o r y   R e p o r t
[Parameters]
Block Size          = 1K bytes
Maximum Cache Size  = 1M bytes (1K blocks)
Maximum Result Size = 51K bytes (51 blocks)
[Memory]
Total Memory = 9440 bytes [0.006% of the Shared Pool]

... Fixed Memory = 9440 bytes [0.006% of the Shared Pool]
... Dynamic Memory = 0 bytes [0.000% of the Shared Pool]

PL/SQL procedure successfully completed

2)清除缓存信息:
 exec dbms_result_cache.Flush;


2.管理客户端结果缓存

oracle call interface(OCI) client resultcache是客户端上的一块内存去,为OCI应用缓存sql查询结果。
此缓存在每个客户端进程上都存在,在进程中被所有内部的会话共享

客户端的结果缓存独立于服务器端结果缓存的,可以两者都缓存也可以只启用一个缓存。

客户端结果缓存可以减少查询的响应时间,更有效地使用数据库资源(CPU、I/O等)、减少内存成本

工作原理:
客户端用户发起对服务器中数据的查询请求,并由服务器返回数据,并在客户端将结果缓存起来(在设置了参数的情况下),再次查询时就使用缓存。但如果中间发现数据库的数据或元数据修改了,则缓存被置为不合法状态,不再使用该缓存。

相应参数有:
client_result_cache_size 设置每个客户端进程的最大结果缓存值,32768或更大才有效,小于此值无效;
在客户端节点上可以增加或不用该缓存,但不能从disable变为enable状态。

client_result_cache_lag设置客户端结果缓存的时间。如果OCI应用一段时间内未调用数据库,client cache lag设置将在语句下次调用时强制进行合法性检查。

compatible 设置兼容的数据库版本,必须是11.0.0.0或跟高。对于视图中的客户端缓存,必须是11.2.0.0.0或以上

3.为结果缓存指定查询
如果同时设置了客户端结果缓存和服务器结果缓存,oracle提供选项来控制哪些查询被缓存

结果缓存的模式:
决定哪些查询会被缓存到client/server result caches。oracle推荐为只读或读操作为主的数据库使用缓存

result_cache_mode参数界定结果缓存的行为,默认是manual,即只有在使用hint或表注释时才使用;可以改为force,即所有结果都缓存,只有在指定hint:/*+no_result_cache*/时不缓存。force参数不推荐使用,由于会影响性能,产生大量锁。

可以为实例(alter system)、会话或服务器设置result_cache_mode参数。

使用sql结果缓存的hint:
/*+ RESULT_CACHE */指定使用结果缓存
/*+ NO_RESULT_CACHE */指定不适用结果缓存

result_cache只在hint指定的查询块上缓存,如果hint应用在视图上,只缓存这个结果。还需要注意:
1)视图必须是标准视图(create view语句创建的)、select语句的from子句中的内视图(inline view)或with子句的内视图
2)视图查询出来的相关联列,属于外部查询块,不能缓存
3)查询结果缓存在服务器结果缓存中,而不是客户端的
4)缓存视图不能与外部查询合并


使用结果缓存表注释:
使用表注释可以控制结果缓存。表注释只对整个查询有效,对查询段无效。主要的好处在于不用再应用级添加结果缓存的hint了。
表注释的优先级比hint低,通过使用hint能忽略表注释。result_cache表注释的值有:

default:
查询中至少一个表设置为了default,则结果缓存不会为查询在表级进行缓存,除非result_cache_mode参数设置为force或制定了result_cache的hint。

如果查询中的所有表都标记为force,则缓存查询结果。表注释在会话级优先于result_cache_mode参数


实验:
SQL> create table t2(table_name varchar2(200),owner varchar2(200)) result_cache (mode default);

Table created.

Elapsed: 00:00:00.09
SQL> select owner,count(table_name) from t2 group by owner;

no rows selected

Elapsed: 00:00:00.02

Execution Plan
----------------------------------------------------------
Plan hash value: 4229074666

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |   204 |     3  (34)| 00:00:01 |
|   1 |  HASH GROUP BY     |      |     1 |   204 |     3  (34)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| T2   |     1 |   204 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement (level=2)


SQL> alter table t2 result_cache(mode force);

Table altered.

SQL> select owner,count(table_name) from t2 group by owner;

no rows selected

Elapsed: 00:00:00.04

Execution Plan
----------------------------------------------------------
Plan hash value: 4229074666

--------------------------------------------------------------------------------------------------

| Id  | Operation           | Name                       | Rows  | Bytes | Cost(%CPU)| Time     |

--------------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT    |                            |     1 |   204 |     3  (34)| 00:00:01 |

|   1 |  RESULT CACHE       | 6gg3z5ygcpfkc5y0j7vst1fgby |       |       |      |          |

|   2 |   HASH GROUP BY     |                            |     1 |   204 |     3  (34)| 00:00:01 |

|   3 |    TABLE ACCESS FULL| T2                         |     1 |   204 |     2   (0)| 00:00:01 |

--------------------------------------------------------------------------------------------------

设置了mode为force后,每次都是用缓存,除非指定hint:


SQL> select /*+no_result_cache*/owner,count(table_name) from t2 group by owner;

no rows selected

Elapsed: 00:00:00.01

Execution Plan
----------------------------------------------------------
Plan hash value: 4229074666

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |   204 |     3  (34)| 00:00:01 |
|   1 |  HASH GROUP BY     |      |     1 |   204 |     3  (34)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| T2   |     1 |   204 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------

Note
-----
   - dynamic sampling used for this statement (level=2)

查询结果缓存表:
select t.TYPE,t.STATUS,t.NAME,t.NAMESPACE,t.CACHE_ID from v$result_cache_objects t;

1 Dependency Published SCOTT.T2  2012-12-21 2:47:42 SCOTT.T2
3 Result Published select owner,count(table_name) from t2 group by owner SQL 2012-12-21 2:47:42 6gg3z5ygcpfkc5y0j7vst1fgby

   --不相关结果已经剔除


总结:以上实验中,我们在建表时指定了表注释,将表以默认模式缓存。这时,由于mode是default,只有通过hint可以从缓存中得到结果;
当mode设置为force时,查询时就直接从缓存中得到结果,除非使用no_result_cache的hint。因为hint的优先级高,因此不从缓存取数据了。


七、结果缓存的约束条件(前提)

允许结果缓存并不意味着这个设置能够确保特定结果设置能够被放入客户端或服务器缓存中。

1.结果缓存的读一致性要求
为了使快照重用,必须保证读一致性。
因此,创建的结果缓存必须保证数据与最近一次提交后的状态相同;特殊情况下,查询的时间点利用闪回进行查询也是可以的。

2.其他要求
当临时表或sys、system下的表被查询时,不能放入结果缓存
序列的当前值或下一个值伪列不能放入结果缓存
sql函数current_date、current_timestamp等不能放入结果缓存

客户端结果缓存还有其他限制。

3.结果集的查询参数要求

Cache results can be reused when they are parameterized with variable values when queries are equivalent and the parameter values are the same. Different values or bind variable names may cause cache misses. Results are parameterized if any of the following constructs are used in the query:
■Bind variables
■The SQL functions DBTIMEZONE, SESSIONTIMEZONE, USERENV/SYS_CONTEXT (with constant variables), UID, and USER
■NLS parameters


八、结果缓存信息的获取

V$RESULT_CACHE_STATISTICS :server result cache设置
V$RESULT_CACHE_MEMORY :服务器结果缓存的内存块列表和统计信息
V$RESULT_CACHE_OBJECTS:缓存对象的信息
V$RESULT_CACHE_DEPENDENCY:缓存的查询结果和缓存的查询所对应对象间的关系
CLIENT_RESULT_CACHE_STATS$:客户端结果缓存信息
dba_tables、user_tables、all_tables :result_cache列显示结果缓存模式

监控服务器结果缓存:
COLUMN NAME FORMAT A20
SELECT NAME, VALUE
FROM V$RESULT_CACHE_STATISTICS;

监控客户端结果缓存:
SELECT STAT_ID, SUBSTR(NAME,1,20), VALUE, CACHE_ID
FROM CLIENT_RESULT_CACHE_STATS$
ORDER BY CACHE_ID, STAT_ID;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26451536/viewspace-752406/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: tuning 第八章
请登录后发表评论 登录
全部评论

注册时间:2011-11-23

  • 博文量
    170
  • 访问量
    484890