TimesTen 应用层数据库缓存学习:8. 配置Sliding Window(滑动窗口)

本文主要是介绍TimesTen 应用层数据库缓存学习:8. 配置Sliding Window(滑动窗口),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Sliding Window(滑动窗口)是Cache Group常用的一种场景。
本文介绍滑动窗口的基本概念以及如何配置滑动窗口。

滑动窗口的描述如下:

In a sliding window configuration, new rows are inserted into and old rows are deleted from the cache tables on a regular schedule so that the tables contain only the data that satisfies a specific time interval.

如何实现:

You can configure a sliding window for a cache group by using incremental autorefresh mode and defining a time-based aging policy. The autorefresh operation checks the timestamp of the rows in the cached Oracle Database tables to determine whether new data should be refreshed into the TimesTen cache tables. The system time and the time zone must be identical on the Oracle Database and TimesTen systems.

有两点必须注意,一个是时区和时间必须一致(在我们的实验环境中没有问题,TimesTen和Oracle都在一台主机上),一个是使用基于时间的Aging和增量autorefresh。

滑动窗口最常用的配置是Explicitly load Read-only Cache Group

Explicitly load Read only Sliding Window

只读滑动窗口是最常用的,可以使用incremental auto refresh。

例如,下面的缓存组缓存近一个月的订单,然后每天将新的订单加入缓存组:

CREATE READONLY CACHE GROUP recent_shipped_orders
AUTOREFRESH MODE INCREMENTAL INTERVAL 1440 MINUTES STATE ON
FROM oratt.orders(ord_num      NUMBER(10) NOT NULL,cust_num     NUMBER(6) NOT NULL,when_placed  DATE NOT NULL,when_shipped DATE NOT NULL,PRIMARY KEY(ord_num))
AGING USE when_shipped LIFETIME 30 DAYS CYCLE 24 HOURS ON;

我们可以模拟一下这个场景,但时间可以短些。

# 先在Oracle中建立订单表
$ sqlplus tthr/oracle@ttorcl
create table orders(ord_num int primary key, ship_time timestamp not null);
insert into orders values(1, sysdate);
insert into orders values(2, sysdate);
commit;SQL> set linesize 100
SQL> select * from orders;ORD_NUM SHIP_TIME
---------- ---------------------------------------------------------------------------1 14-APR-16 07.10.03.000000 AM2 14-APR-16 07.10.04.000000 AMSQL> grant select on orders to cacheadm;Grant succeeded.ttisql -connstr "dsn=cachedb1_1122;uid=cacheadm;pwd=timesten;oraclepwd=oracle" -e "set prompt 'cacheadm>'"
CREATE READONLY CACHE GROUP "RO_SW" AUTOREFRESH MODE INCREMENTAL INTERVAL 10 SECONDSSTATE ONFROM"TTHR"."ORDERS" ("ORD_NUM"   NUMBER(38)   NOT NULL,"SHIP_TIME" TIMESTAMP(6) NOT NULL,PRIMARY KEY("ORD_NUM"))AGING USE SHIP_TIME LIFETIME 300 seconds CYCLE 10 seconds ONcacheadm>cachegroups;Cache Group CACHEADM.RO_SW:Cache Group Type: Read OnlyAutorefresh: YesAutorefresh Mode: IncrementalAutorefresh State: OnAutorefresh Interval: 10 SecondsAutorefresh Status: okAging: Timestamp based uses column SHIP_TIME lifetime 300 seconds cycle 10 seconds onRoot Table: TTHR.ORDERSTable Type: Read Only1 cache group found.tthr>select * from orders;
< 1, 2016-04-14 07:10:03.000000 >
< 2, 2016-04-14 07:10:04.000000 >
2 rows found.# Oracle数据库中
# 用户sys执行 grant execute on dbms_lock to public;
# 用户tthr执行以下过程,产生订单:
declare ord_num number;      
begin
for i in 1..1000 loop
select max(ord_num) into ord_num from orders;
insert into orders values(ord_num+1, sysdate);
commit;
dbms_lock.sleep( 10 );
end loop;
end;
/$ ttisql  -connstr "dsn=cachedb1_1122;uid=tthr;pwd=timesten;oraclepwd=oracle" -e "set prompt 'tthr>'"tthr>select ord_num, TIMESTAMPDIFF(SQL_TSI_SECOND,ship_time, sysdate) from orders;
< 12, 299 >
< 13, 289 >
< 14, 279 >
< 15, 269 >
< 16, 259 >
< 17, 249 >
< 18, 239 >
< 19, 229 >
< 20, 219 >
< 21, 209 >
< 22, 199 >
< 23, 189 >
< 24, 179 >
< 25, 169 >
< 26, 159 >
< 27, 149 >
< 28, 139 >
< 29, 129 >
< 30, 119 >
< 31, 109 >
< 32, 99 >
< 33, 89 >
< 34, 79 >
< 35, 69 >
< 36, 59 >
< 37, 49 >
< 38, 39 >
< 39, 29 >
< 40, 19 >
< 41, 9 >
30 rows found.
tthr>
过一小会
tthr>select ord_num, TIMESTAMPDIFF(SQL_TSI_SECOND,ship_time, sysdate) from orders;
< 18, 291 >
< 19, 281 >
< 20, 271 >
< 21, 261 >
< 22, 251 >
< 23, 241 >
< 24, 231 >
< 25, 221 >
< 26, 211 >
< 27, 201 >
< 28, 191 >
< 29, 181 >
< 30, 171 >
< 31, 161 >
< 32, 151 >
< 33, 141 >
< 34, 131 >
< 35, 121 >
< 36, 111 >
< 37, 101 >
< 38, 91 >
< 39, 81 >
< 40, 71 >
< 41, 61 >
< 42, 51 >
< 43, 41 >
< 44, 31 >
< 45, 21 >
< 46, 11 >
29 rows found.
tthr># 可以看到,时间差总是在300秒以内,订单从41滑动到46,老的订单12-17被淘汰

对于dynamic read-only以及AWT是否可以配置滑动窗口呢,答案是肯定的,只不过不是很常用。

If the cache group does not use incremental autorefresh mode, you can configure a sliding window by using a LOAD CACHE GROUP, REFRESH CACHE GROUP, or INSERT statement, or a dynamic load operation to bring new data into the cache tables.

Dynamic Load Read only Sliding Window

CREATE DYNAMIC READONLY CACHE GROUP "DRO_SW" AUTOREFRESH MODE INCREMENTAL INTERVAL 10 SECONDSSTATE PAUSEDFROM"TTHR"."ORDERS" ("ORD_NUM"   NUMBER(38)   NOT NULL,"SHIP_TIME" TIMESTAMP(6) NOT NULL,PRIMARY KEY("ORD_NUM"))AGING USE SHIP_TIME LIFETIME 300 seconds CYCLE 10 seconds ON
cacheadm>cachegroups;Cache Group CACHEADM.DRO_SW:Cache Group Type: Read Only (Dynamic)Autorefresh: YesAutorefresh Mode: IncrementalAutorefresh State: PausedAutorefresh Interval: 10 SecondsAutorefresh Status: okAging: Timestamp based uses column SHIP_TIME lifetime 300 seconds cycle 10 seconds onRoot Table: TTHR.ORDERSTable Type: Read Only1 cache group found.tthr>select count(*) from orders;
< 0 >
1 row found.
# 虽然可以通过dynamic load导入数据,不过批量导入还是得靠load。
# Load的作用是将新的数据导入缓存组,而autofresh同时也在工作,将已在缓存组的数据进行更新。
# 例如如果在TimesTen中的订单79,在Oracle端删除了,那么autorefresh也会将其删除# 用户tthr执行以下过程,产生订单:
declare ord_num number;      
begin
for i in 1..1000 loop
select max(ord_num) into ord_num from orders;
insert into orders values(ord_num+1, sysdate);
commit;
dbms_lock.sleep( 10 );
end loop;
end;
/
cacheadm>load cache group dro_sw commit every 256 rows;
3 cache instances affected.tthr>select count(*) from orders;
< 3 >
1 row found.cacheadm>load cache group dro_sw commit every 256 rows;
2 cache instances affected.tthr>select count(*) from orders;
< 5 >
1 row found.tthr>select ord_num, TIMESTAMPDIFF(SQL_TSI_SECOND,ship_time, sysdate) from orders;
< 78, 299 >
< 79, 289 >
< 81, 269 >
< 82, 259 >
< 83, 249 >
< 84, 239 >
< 85, 229 >
< 86, 219 >
< 87, 209 >
< 88, 199 >
< 89, 189 >
< 90, 179 >
< 91, 169 >
< 92, 159 >
< 93, 149 >
< 94, 139 >
< 95, 129 >
< 96, 119 >
< 97, 109 >
< 98, 99 >
< 99, 89 >
< 100, 79 >
< 101, 69 >
< 102, 59 >
< 103, 49 >
< 104, 39 >
< 105, 29 >
< 106, 19 >
28 rows found.
时间差总在300秒内

Explicitly Load AWT Sliding Window

前面两例为只读缓存,数据来源是Oracle。本例为AWT,数据来源在TimesTen。
使用场景为TimesTen作为数据收集的集中点,然后保存时间窗口的数据作为实时分析。
下面三个提示符分别是用以下三个命令登录的
* cacheadm - ttisql -connstr "dsn=cachedb1_1122;uid=cacheadm;pwd=timesten;oraclepwd=oracle" -e "set prompt 'cacheadm>'"

* tthr(schema user) - ttisql -connstr "dsn=cachedb1_1122;uid=tthr;pwd=timesten;oraclepwd=oracle" -e "set prompt 'tthr>'"

* Oracle DB - sqlplus tthr/oracle@ttorcl

SQL> grant select, insert, update, delete on orders to cacheadm;CREATE ASYNCHRONOUS WRITETHROUGH CACHE GROUP "AWT_SW" FROM"TTHR"."ORDERS" ("ORD_NUM"   NUMBER(38)   NOT NULL,"SHIP_TIME" TIMESTAMP(6) NOT NULL,PRIMARY KEY("ORD_NUM"))AGING USE SHIP_TIME LIFETIME 300 seconds CYCLE 10 seconds ONcacheadm>call ttrepstart;
tthr>select * from orders;
0 rows found.
cacheadm>cachegroups;Cache Group CACHEADM.AWT_SW:Cache Group Type: Asynchronous WritethroughAutorefresh: NoAging: Timestamp based uses column SHIP_TIME lifetime 300 seconds cycle 10 seconds onRoot Table: TTHR.ORDERSTable Type: Propagate1 cache group found.
cacheadm>repschemes;Replication Scheme TTREP._AWTREPSCHEME:Element: _1798096                       Type: Table TTHR.ORDERSMaster Store: CACHEDB1_1122 on TIMESTEN-HOL Transmit DurableSubscriber Store: _ORACLE from TIMESTEN-HOL Store: CACHEDB1_1122 on TIMESTEN-HOLPort: (auto)Log Fail Threshold: (none)Retry Timeout: 120 secondsCompress Traffic: DisabledStore: _ORACLE from TIMESTEN-HOLPort: (auto)Log Fail Threshold: (none)Retry Timeout: 120 secondsCompress Traffic: Disabled1 replication scheme found.
SQL> select count(*) from orders;COUNT(*)
----------134
# Oracle中有数据,但是一条也没load进来,原因是这些数据过老,不满足时间窗口的要求
cacheadm>load cache group awt_sw commit every 256 rows;
0 cache instances affected.
# 为了演示方便,我们删除掉Oracle中的数据
SQL> truncate table orders;Table truncated.
# 然后在Oracle中初始化一条数据
SQL> insert into orders values(1, sysdate);1 row created.SQL> commit;Commit complete.
cacheadm>load cache group awt_sw commit every 256 rows;
1 cache instance affected.
tthr>select * from orders;                                                                                                                                              < 1, 2016-04-14 22:28:35.000000 >
1 row found.# 然后在TimesTen端插入数据
tthr>ALTER SESSION SET PLSQL_TIMEOUT = 0; <- 缺省是30秒Session altered.
declare ord_num number;      
begin
for i in 1..1000 loop
select max(ord_num) into ord_num from orders;
insert into orders values(ord_num+1, sysdate);
commit;
dbms_lock.sleep( 10 );
end loop;
end;
/# 另起一个tthr会话
tthr>select * from orders;
< 2, 2016-04-14 22:29:59.000000 >
< 3, 2016-04-14 22:30:09.000000 >
< 4, 2016-04-14 22:30:19.000000 >
< 5, 2016-04-14 22:33:35.000000 >
< 6, 2016-04-14 22:33:45.000000 >
< 7, 2016-04-14 22:33:55.000000 >
6 rows found.
# 我们可以看到,TimesTen中的数据不断增加
# 而时间差在300秒以内
tthr>select ord_num, TIMESTAMPDIFF(SQL_TSI_SECOND,ship_time, sysdate) from orders;
< 16, 300 >
< 17, 290 >
< 18, 280 >
< 19, 270 >
< 20, 260 >
< 21, 250 >
< 22, 240 >
< 23, 230 >
< 24, 220 >
< 25, 210 >
< 26, 200 >
< 27, 190 >
< 28, 180 >
< 29, 170 >
< 30, 160 >
< 31, 150 >
< 32, 140 >
< 33, 130 >
< 34, 120 >
< 35, 110 >
< 36, 100 >
< 37, 90 >
< 38, 80 >
< 39, 70 >
< 40, 60 >
< 41, 50 >
< 42, 40 >
< 43, 30 >
< 44, 20 >
< 45, 10 >
30 rows found.# 而在Oracle中保留有所有的历史数据
SQL> select count(*) from orders;COUNT(*)
----------55

Dynamic Load AWT Sliding Window

就不讨论了,和上例差不多

这篇关于TimesTen 应用层数据库缓存学习:8. 配置Sliding Window(滑动窗口)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/677709

相关文章

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

Linux云服务器手动配置DNS的方法步骤

《Linux云服务器手动配置DNS的方法步骤》在Linux云服务器上手动配置DNS(域名系统)是确保服务器能够正常解析域名的重要步骤,以下是详细的配置方法,包括系统文件的修改和常见问题的解决方案,需要... 目录1. 为什么需要手动配置 DNS?2. 手动配置 DNS 的方法方法 1:修改 /etc/res

MyBatis延迟加载与多级缓存全解析

《MyBatis延迟加载与多级缓存全解析》文章介绍MyBatis的延迟加载与多级缓存机制,延迟加载按需加载关联数据提升性能,一级缓存会话级默认开启,二级缓存工厂级支持跨会话共享,增删改操作会清空对应缓... 目录MyBATis延迟加载策略一对多示例一对多示例MyBatis框架的缓存一级缓存二级缓存MyBat

mysql8.0.43使用InnoDB Cluster配置主从复制

《mysql8.0.43使用InnoDBCluster配置主从复制》本文主要介绍了mysql8.0.43使用InnoDBCluster配置主从复制,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录1、配置Hosts解析(所有服务器都要执行)2、安装mysql shell(所有服务器都要执行)3、

前端缓存策略的自解方案全解析

《前端缓存策略的自解方案全解析》缓存从来都是前端的一个痛点,很多前端搞不清楚缓存到底是何物,:本文主要介绍前端缓存的自解方案,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、为什么“清缓存”成了技术圈的梗二、先给缓存“把个脉”:浏览器到底缓存了谁?三、设计思路:把“发版”做成“自愈”四、代码

java程序远程debug原理与配置全过程

《java程序远程debug原理与配置全过程》文章介绍了Java远程调试的JPDA体系,包含JVMTI监控JVM、JDWP传输调试命令、JDI提供调试接口,通过-Xdebug、-Xrunjdwp参数配... 目录背景组成模块间联系IBM对三个模块的详细介绍编程使用总结背景日常工作中,每个程序员都会遇到bu

Java 缓存框架 Caffeine 应用场景解析

《Java缓存框架Caffeine应用场景解析》文章介绍Caffeine作为高性能Java本地缓存框架,基于W-TinyLFU算法,支持异步加载、灵活过期策略、内存安全机制及统计监控,重点解析其... 目录一、Caffeine 简介1. 框架概述1.1 Caffeine的核心优势二、Caffeine 基础2

使用Node.js和PostgreSQL构建数据库应用

《使用Node.js和PostgreSQL构建数据库应用》PostgreSQL是一个功能强大的开源关系型数据库,而Node.js是构建高效网络应用的理想平台,结合这两个技术,我们可以创建出色的数据驱动... 目录初始化项目与安装依赖建立数据库连接执行CRUD操作查询数据插入数据更新数据删除数据完整示例与最佳

JDK8(Java Development kit)的安装与配置全过程

《JDK8(JavaDevelopmentkit)的安装与配置全过程》文章简要介绍了Java的核心特点(如跨平台、JVM机制)及JDK/JRE的区别,重点讲解了如何通过配置环境变量(PATH和JA... 目录Java特点JDKJREJDK的下载,安装配置环境变量总结Java特点说起 Java,大家肯定都