【Oracle Database 12c New Feature】ILM – Temporal Va
ILM全称是Information Lifecycle Management,意思是信息生命周期管理,听上去很高端洋气的一个词,但是实际上几乎每个稍微大些的系统都已经在做ILM了,比如说将生产表中的数据定期插入到历史表中,并把生产表中的这些数据删除,这就是数据生命周期管理;又
ILM全称是Information Lifecycle Management,意思是信息生命周期管理,听上去很高端洋气的一个词,但是实际上几乎每个稍微大些的系统都已经在做ILM了,比如说将生产表中的数据定期插入到历史表中,并把生产表中的这些数据删除,这就是数据生命周期管理;又比如使用了分区,定期将过期的数据分区删除掉,或者置为READONLY,让RMAN不再备份,这也是数据生命周期管理。
因此ILM由来已久,只要数据存在活跃-不活跃-静止这样的周期变化,那么ILM就必不可少,Oracle Database 12c中提供了很多新功能用来方便地进行数据生命周期管理,有些功能甚至是我们期盼已久的。
本文先介绍时间有效期管理(Temporal Validity),下两篇文章会介绍数据库内归档(In-Database Archiving)以及数据热度图(Heat Map)。注意:Temporal Validity和Heat Map目前还不支持多租户架构的数据库,因此想要使用,必须是一个NON-CDB,In-Database Archiving则支持多租户架构,可以在PDB中使用。
一. 时间有效期管理(Temporal Validity)
以下简称TV,TV的功能大致上可以这样描述:在表中手动或者自动建两个时间类型的字段,一个表示有效期的开始时间,一个表示有效期的结束时间,就可以通过设置让只有在有效期内的记录才会被选择出来。
以下这个场景是我构想出来的,一张表里不断地INSERT数据,但是每条数据有效期只有1分钟,过了1分钟再查就看不见了,如果加以仔细策划,应该会是很有趣的功能。直接进入测试。
设置TV,需要使用dbms_flashback_archive包,需要该包的执行权限。
SQL> GRANT EXECUTE ON dbms_flashback_archive TO kamus;
创建测试表,period for关键字是TV新功能的关键字,valid_time是TV策略的名字,可以随便写。valid_time_start和valid_time_end字段可以不手工定义,只要指定了period for关键字,Oracle会自动创建两个不可见字段。我这里之所以手工定义开始和结束时间字段,是为了能够指定DEFAULT值。有效期开始时间valid_time_start是记录插入的当前时间,有效期结束时间valid_time_end是当前时间的后一分钟。由此定义出了一个跨度1分钟的有效期。
SQL> conn kamus/oracle SQL> CREATE TABLE TV (insert_time DATE, valid_time_start DATE invisible DEFAULT sysdate, valid_time_end DATE invisible DEFAULT sysdate+1/1440, period FOR valid_time(valid_time_start,valid_time_end) );
可以看到明确定义的INSERT_TIME字段用于演示,VALID_TIME_START和VALID_TIME_END是明确定义的不可见字段。之外,Oracle还自动创建了VALID_TIME字段,也是隐藏字段。
SQL> SELECT COLUMN_NAME,DATA_TYPE,HIDDEN_COLUMN FROM USER_TAB_COLS WHERE TABLE_NAME='TV'; ? COLUMN_NAME DATA_TYPE HID -------------------- -------------------- --- VALID_TIME_END DATE YES VALID_TIME_START DATE YES INSERT_TIME DATE NO VALID_TIME NUMBER YES
插入一行当前时间
SQL> INSERT INTO TV VALUES (sysdate); ? 1 ROW created.
正常情况选择,这行记录总是存在的
SQL> SELECT * FROM TV; ? INSERT_TIME ----------------- 20130811 09:04:30
为了应对这个新功能,在Flashback Query中新增了as of period for关键字。as of period for valid_time sysdate+1表示我们想查询在明天都还有效的数据,因为根据我们的设定,所有数据都只在插入以后1分钟内有效,因此自然无法找到在明天还有效的数据,返回零条记录。
SQL> SELECT * FROM TV AS OF period FOR valid_time sysdate+1; ? no ROWS selected
再插入一条测试数据。
SQL> INSERT INTO TV VALUES (sysdate); ? 1 ROW created. ? SQL> SELECT * FROM TV; ? INSERT_TIME ----------------- 20130811 09:04:30 20130811 09:08:27
我们想查询昨天就有效的数据,但是所有的数据有效期开始都是插入数据的那个时间点,自然无法找到昨天就有效的数据,返回零条记录。
SQL> SELECT * FROM TV AS OF period FOR valid_time sysdate-1; ? no ROWS selected
除了使用as of这种闪回查询的语法,还可以直接在会话级别设置有效时间点。CURRENT表示设置为当前时间点。
SQL> EXEC dbms_flashback_archive.enable_at_valid_time('CURRENT'); ? PL/SQL PROCEDURE successfully completed. ? SQL> SELECT * FROM TV; ? no ROWS selected
在我的测试过程中,TV并不稳定,有时候即使设置了as of,也仍然会返回所有记录,但是过一会儿再次执行完全相同的语句,又能够返回符合条件的记录。没有详细跟踪不稳定的原因,但是猜测与cursor执行计划重用有关,毕竟Oracle的实现只是增加了一个filter条件,如果由于某种原因,之前cursor的执行计划被重用,那么很可能这个filter条件就没有加上,随之而来的也就会返回所有记录。
接下来,我们通过显示执行计划,看看Oracle是如何增加这个filter条件的。
首先禁用TV。执行计划是很正常的全表扫描。
SQL> EXEC dbms_flashback_archive.DISABLE_ASOF_VALID_TIME; ? PL/SQL PROCEDURE successfully completed. ? SQL> SELECT COUNT(*) FROM tv; ? COUNT(*) ---------- 77477 ? ? Execution Plan ---------------------------------------------------------- Plan hash VALUE: 4129329588 ? ------------------------------------------------------------------- | Id | Operation | Name | ROWS | Cost (%CPU)| TIME | ------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 102 (0)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | | | | 2 | TABLE ACCESS FULL| TV | 76349 | 102 (0)| 00:00:01 | ------------------------------------------------------------------- ? Note ----- - dynamic statistics used: dynamic sampling (level=2) ? ? Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 333 consistent gets 0 physical reads 0 redo SIZE 544 bytes sent via SQL*Net TO client 543 bytes received via SQL*Net FROM client 2 SQL*Net roundtrips TO/FROM client 0 sorts (memory) 0 sorts (disk) 1 ROWS processed
重新在会话级别启用TV,可以看到在第二步,也就是全表扫描之后增加了一个filter,由于指定的有效期是CURRENT,因此filter条件是VALID_TIME_START 小于等于 当前时间 小于 VALID_TIME_END,也就是只要指定的有效期落在VALID_TIME_START和VALID_TIME_END之间,这条记录就可以被显示出来。同时也可以看到如果这两个限制条件为空,也都作为开放区间,也就是为空就表示不做限制。
由于测试的记录都只有1分钟有效期,因此此时已经没有一条记录可以显示了。
SQL> EXEC dbms_flashback_archive.enable_at_valid_time('CURRENT'); ? PL/SQL PROCEDURE successfully completed. ? SQL> SELECT COUNT(*) FROM tv; ? COUNT(*) ---------- 0 ? ? Execution Plan ---------------------------------------------------------- Plan hash VALUE: 4129329588 ? --------------------------------------------------------------------------- | Id | Operation | Name | ROWS | Bytes | Cost (%CPU)| TIME | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 18 | 103 (1)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | 18 | | | |* 2 | TABLE ACCESS FULL| TV | 287 | 5166 | 103 (1)| 00:00:01 | --------------------------------------------------------------------------- ? Predicate Information (IDENTIFIED BY operation id): --------------------------------------------------- ? 2 - FILTER(("T"."VALID_TIME_START" IS NULL OR SYS_EXTRACT_UTC(INTERNAL_FUNCTION("T"."VALID_TIME_START"))SYS_EXTRACT_UTC (SYSTIMESTAMP(6)))) ? ? Statistics ---------------------------------------------------------- 33 recursive calls 4 db block gets 354 consistent gets 0 physical reads 0 redo SIZE 541 bytes sent via SQL*Net TO client 543 bytes received via SQL*Net FROM client 2 SQL*Net roundtrips TO/FROM client 0 sorts (memory) 0 sorts (disk) 1 ROWS processed
通过执行计划显示后台机制是一方面,另一方面我们也可以看到实际上TV是会有性能问题的,如果WHERE条件中无法使用到索引而执行了全表扫描(我这里因为没有WHERE条件所以只能是全表扫描),那么无论最终符合有效期的记录是多少,总要先进行所有记录的扫描,我们可以通过前后两次的consistent gets基本相同来获得这个结论。
更直白的说,如果作为系统设计人员不去考虑索引的构建,而仅仅是启用了TV,那么哪怕根据有效期限制,有10万记录的表只有1条会被显示出来,也仍然需要先扫描10万记录,然后再filter掉99999条,这对于程序员来说,如果不仔细阅读执行计划,就可能会造成很大的困扰,程序员会很奇怪,为什么这张表里面看上去只有1条记录,但是却要扫描那么长时间呢?
结论:数据有效期是Oracle利用隐藏字段和Flashback Query技术作的一个有趣的功能,但是数据架构人员在规划的时候一定要考虑性能因素。
Share/Save
Related posts:
- Oracle 11g new feature – Virtual Column
- SPM default feature in Oracle 11g
- How to Use DBMS_ADVANCED_REWRITE in Oracle 10g


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.
