一、开启result cache
enable_global_result_cache result_cache_max_size result_cache_max_result
复制
调整以上三个参数需要重启数据库
gs_guc set -N all -I all -c "enable_global_result_cache=on" gs_guc set -N all -I all -c "result_cache_max_size='128MB'" gs_guc set -N all -I all -c "result_cache_max_result=10"
复制
二、验证
t1=# select pw_result_cache_status(); pw_result_cache_status ------------------------ active (1 行记录)
复制
查看result cache功能状态
t1=# select * from emp; empno | ename | job | mgr | hiredate | sal | comm | deptno -------+--------+-----------+------+---------------------+------+------+-------- 7369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800 | | 20 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600 | 300 | 30 7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250 | 500 | 30 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975 | | 20 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250 | 1400 | 30 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850 | | 30 7782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450 | | 10 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000 | | 20 7839 | KING | PRESIDENT | | 1981-11-17 00:00:00 | 5000 | | 10 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500 | 0 | 30 7876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100 | | 20 7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950 | | 30 7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000 | | 20 7934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300 | | 10 (14 行记录) t1=# select * from pw_result_cache_memory_report; TotalMemory | UsedMemory | ElementCount | MaxResultSize -------------+------------+--------------+--------------- 33554432 | 0 | 0 | 0 (1 行记录)
复制
t1=# select /*+ result_cache*/* from emp; empno | ename | job | mgr | hiredate | sal | comm | deptno -------+--------+-----------+------+---------------------+------+------+-------- 7369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800 | | 20 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600 | 300 | 30 7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250 | 500 | 30 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975 | | 20 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250 | 1400 | 30 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850 | | 30 7782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450 | | 10 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000 | | 20 7839 | KING | PRESIDENT | | 1981-11-17 00:00:00 | 5000 | | 10 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500 | 0 | 30 7876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100 | | 20 7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950 | | 30 7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000 | | 20 7934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300 | | 10 (14 行记录) t1=# select * from pw_result_cache_memory_report; TotalMemory | UsedMemory | ElementCount | MaxResultSize -------------+------------+--------------+--------------- 33554432 | 1032 | 1 | 1032 (1 行记录) t1=# select * from pg_catalog.pw_result_cache_items; cache_id | query_string | ref_count ----------+--------------------------------------+----------- 1 | select /*+ result_cache*/* from emp; | 2 (1 行记录)
复制
执行计划
t1=# explain select /*+result_cache*/ * from emp; QUERY PLAN -------------------------------------------------------------- ResultCache on Query (cost=0.00..13.00 rows=300 width=242) -> Seq Scan on emp (cost=0.00..13.00 rows=300 width=242) (2 行记录)
复制
三、清理
1、方法一
postgres=# select pw_result_cache_flush(); pw_result_cache_flush ----------------------- (1 行记录) t1=# select * from pw_result_cache_memory_report; TotalMemory | UsedMemory | ElementCount | MaxResultSize -------------+------------+--------------+--------------- 33554432 | 0 | 0 | 0 (1 行记录) t1=# select * from pg_catalog.pw_result_cache_items; cache_id | query_string | ref_count ----------+--------------+----------- (0 行记录)
复制
2、方法二
t1=# select /*+ result_cache*/* from emp; empno | ename | job | mgr | hiredate | sal | comm | deptno -------+--------+-----------+------+---------------------+------+------+-------- 7369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800 | | 20 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600 | 300 | 30 7521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250 | 500 | 30 7566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975 | | 20 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250 | 1400 | 30 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850 | | 30 7782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450 | | 10 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000 | | 20 7839 | KING | PRESIDENT | | 1981-11-17 00:00:00 | 5000 | | 10 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500 | 0 | 30 7876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100 | | 20 7900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950 | | 30 7902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000 | | 20 7934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300 | | 10 (14 行记录) t1=# select * from pw_result_cache_memory_report; TotalMemory | UsedMemory | ElementCount | MaxResultSize -------------+------------+--------------+--------------- 33554432 | 1032 | 1 | 1032 (1 行记录) t1=# select * from pg_catalog.pw_result_cache_items; cache_id | query_string | ref_count ----------+--------------------------------------+----------- 2 | select /*+ result_cache*/* from emp; | 2 (1 行记录) t1=# select pw_result_cache_invalidate(2); pw_result_cache_invalidate ---------------------------- (1 行记录) t1=# select * from pw_result_cache_memory_report; TotalMemory | UsedMemory | ElementCount | MaxResultSize -------------+------------+--------------+--------------- 33554432 | 0 | 0 | 0 (1 行记录) t1=# select * from pg_catalog.pw_result_cache_items; cache_id | query_string | ref_count ----------+--------------+----------- (0 行记录)
复制
四、说明
1、函数解释
- pw_result_cache_status
查看查询缓存的状态。ACTIVE: 查询缓存特性是激活的。DISABLED:查询缓存特性是不可用的。
t1=# select pw_result_cache_status(); pw_result_cache_status ------------------------ active (1 行记录)
复制
- pw_result_cache_memory_report
列出结果缓存内存利用的一个概要(默认)或详细的报表。
t1=# select pw_result_cache_memory_report(); pw_result_cache_memory_report ------------------------------- (33554432,0,0,0) (1 行记录)
复制
t1=# select * from pw_result_cache_memory_report(); TotalMemory | UsedMemory | ElementCount | MaxResultSize -------------+------------+--------------+--------------- 33554432 | 0 | 0 | 0 (1 行记录)
复制
TotalMemory:查询缓存总共可用内存(字节)。
UsedMemory:查询缓存当前已用内存(字节)。
CacheCo:当前缓存个数。
MaxCacheSize:当前缓存中使用的最大内存(字节)。
- pw_result_cache_flush
清理整个查询缓存,清理期间,查询缓存不可用。
postgres=# select pw_result_cache_flush(); pw_result_cache_flush ----------------------- (1 行记录)
复制
- result_cache_invalidate
使指定缓存失效。cache_id可通过函数pw_result_cache_items查询得到。
select pw_result_cache_invalidate(2);
复制
- pw_result_cache_items
查询已经生效的缓存的相关信息。
select * from pg_catalog.pw_result_cache_items;
复制
2、参数解释
-
enable_global_result_cache
全局参数,是否启用result_cache,因为缓存失效涉及所有的表所以必须设置为全局参数。 -
result_cache_mode
MANUAL(这也是默认情况),只有指定hint result_cache的时候才能使用查询缓存。
FORCE,所有不包含hint no_result_cache的查询语句都会使用查询缓存,前提是开启了全局的查询缓存。
AUXILIARY
若主库查询缓存关闭,备库开启查询缓存,则主库必须配置为此参数,这样主库上的ddl/dml操作才会令备库上的相应缓存失效,否则备库上的缓存不能失效。此参数值的目的是关闭查询缓存的时候也能在备库正常使用查询缓存,以节约性能。 -
result_cache_max_size
全局参数,查询缓存所有缓存结果的总大小限制,以字节为单位,不受到额外控制,需要用户自己确认配置是否合理。 -
result_cache_max_result
全局参数,单个缓存的最大行数。 -
result_cache_max_tables
全局参数,单个缓存涉及的最大表数量。默认值: 10 -
result_cache_whitelist
可以用于查询缓存的表oid的白名单。不为空时,查询语句中使用的表必须完全在此参数指定的列表中存在。
-
query_cache_size
配给缓存查询结果的内存。 -
query_cache_type
询缓存的类型。默认值: 0