【Oracle Database 12c New Feature】ILM – In-Database
本文介绍Oracle Database 12c中关于数据生命周期管理多个新特性中相对最简单的一个,数据库内归档(In-Database Archiving)。使用的测试表是上一篇介绍数据时间有效期管理中使用的TV表(包括表结构和测试数据),如果你还没有看过上一篇文章,可以先阅读【O
本文介绍Oracle Database 12c中关于数据生命周期管理多个新特性中相对最简单的一个,数据库内归档(In-Database Archiving)。使用的测试表是上一篇介绍数据时间有效期管理中使用的TV表(包括表结构和测试数据),如果你还没有看过上一篇文章,可以先阅读【Oracle Database 12c New Feature】ILM – Temporal Validity。
相比起数据时间有效期管理而言,数据库内归档非常简单,只有一个开关,对于一条数据,要不就是活跃的允许显示,要不就是归档掉不显示,这是由数据库管理员来人工操作的。
在设置数据库内归档之前,必须要在表级别启用该特性。如上一篇文章提到的,In-Database Archiving支持多租户架构,可以在PDB中使用。
SQL> ALTER TABLE TV ROW archival; TABLE altered.
Oracle仍然是使用隐藏列来实现这个功能的,在启用该特性以后,会自动在表上增加ORA_ARCHIVE_STATE字段,这是一个VARCHAR2(4000)的字段。
SQL> SELECT COLUMN_NAME,DATA_TYPE,HIDDEN_COLUMN FROM USER_TAB_COLS WHERE TABLE_NAME='TV'; COLUMN_NAME DATA_TYPE HID -------------------- -------------------- --- ORA_ARCHIVE_STATE VARCHAR2 YES SYS_NC00005$ RAW YES VALID_TIME_END DATE YES VALID_TIME_START DATE YES INSERT_TIME DATE NO VALID_TIME NUMBER YES 6 ROWS selected.
先检查一下TV表中的数据分布,一共有9个不同的时间段,前面5个都只有1条记录,后面4个则有大量测试记录。
SQL> SELECT INSERT_TIME,COUNT(*) FROM TV GROUP BY INSERT_TIME ORDER BY 1; INSERT_TIME COUNT(*) ----------------- ---------- 20130811 09:04:30 1 20130811 09:08:27 1 20130811 09:22:30 1 20130811 09:39:40 1 20130811 09:45:22 1 20130811 09:50:44 19368 20130811 09:50:46 19368 20130811 09:50:47 19368 20130811 09:50:48 19368 9 ROWS selected.
尝试将所有20130811 09:50之后的记录全部设置为归档模式。直接使用UPDATE语句将ORA_ARCHIVE_STATE字段更新为任意非0的字符,0表示该记录是活跃的,任何非0字符都表示该记录被归档。
SQL> UPDATE TV SET ORA_ARCHIVE_STATE = '20' WHERE INSERT_TIME>to_date('20130811 09:50','YYYYMMDD HH24:MI'); 77472 ROWS updated.
再次执行相同的查询语句,可以看到只存在活跃的5条记录了。
SQL> SELECT INSERT_TIME,COUNT(*) FROM TV GROUP BY INSERT_TIME ORDER BY 1; INSERT_TIME COUNT(*) ----------------- ---------- 20130811 09:04:30 1 20130811 09:08:27 1 20130811 09:22:30 1 20130811 09:39:40 1 20130811 09:45:22 1 5 ROWS selected.
可以在会话级别设置即使是记录被归档,也仍然显示出来。
SQL> ALTER SESSION SET ROW ARCHIVAL VISIBILITY = ALL; SESSION altered. SQL> SELECT INSERT_TIME,COUNT(*) FROM TV GROUP BY INSERT_TIME ORDER BY 1; INSERT_TIME COUNT(*) ----------------- ---------- 20130811 09:04:30 1 20130811 09:08:27 1 20130811 09:22:30 1 20130811 09:39:40 1 20130811 09:45:22 1 20130811 09:50:44 19368 20130811 09:50:46 19368 20130811 09:50:47 19368 20130811 09:50:48 19368 9 ROWS selected.
检查ORA_ARCHIVE_STATE值,可以看到所有活跃数据的ORA_ARCHIVE_STATE字段值均为0,这也是在表级别启用数据库内归档以后的默认值。
SQL> SELECT ORA_ARCHIVE_STATE,INSERT_TIME,COUNT(*) FROM TV GROUP BY ORA_ARCHIVE_STATE,INSERT_TIME ORDER BY 2; ORA_ARCHIVE_STATE INSERT_TIME COUNT(*) -------------------- ----------------- ---------- 0 20130811 09:04:30 1 0 20130811 09:08:27 1 0 20130811 09:22:30 1 0 20130811 09:39:40 1 0 20130811 09:45:22 1 20 20130811 09:50:44 19368 20 20130811 09:50:46 19368 20 20130811 09:50:47 19368 20 20130811 09:50:48 19368 9 ROWS selected.
将其中的一些记录的ORA_ARCHIVE_STATE字段更新为另外的非0字符。
SQL> UPDATE TV SET ORA_ARCHIVE_STATE='ARCHIVING' WHERE INSERT_TIME='20130811 09:50:48'; 19368 ROWS updated. SQL> SELECT ORA_ARCHIVE_STATE,INSERT_TIME,COUNT(*) FROM TV GROUP BY ORA_ARCHIVE_STATE,INSERT_TIME ORDER BY 2; ORA_ARCHIVE_STATE INSERT_TIME COUNT(*) -------------------- ----------------- ---------- 0 20130811 09:04:30 1 0 20130811 09:08:27 1 0 20130811 09:22:30 1 0 20130811 09:39:40 1 0 20130811 09:45:22 1 20 20130811 09:50:44 19368 20 20130811 09:50:46 19368 20 20130811 09:50:47 19368 ARCHIVING 20130811 09:50:48 19368
在会话级别重新设置不显示归档数据,可以看到只要是ORA_ARCHIVE_STATE字段不为0的记录都不会显示。
SQL> ALTER SESSION SET ROW ARCHIVAL VISIBILITY = ACTIVE; SESSION altered. SQL> SELECT INSERT_TIME,COUNT(*) FROM TV GROUP BY INSERT_TIME ORDER BY 1; INSERT_TIME COUNT(*) ----------------- ---------- 20130811 09:04:30 1 20130811 09:08:27 1 20130811 09:22:30 1 20130811 09:39:40 1 20130811 09:45:22 1
性能考虑,这一点数据库内归档与时间有效性是相同的,都只是对隐藏字段进行了filter操作。即使是只显示活跃数据,也仍然需要扫描全表。这一点在真实应用中可以通过创建索引来避免全表扫描,可以参看MOS Note: Potential SQL Performance Degradation When In Database Row Archiving (Doc ID 1579790.1),也就是数据库内归档只应该在一个具备良好性能的SQL基础上对返回结果进行过滤,而不要期望归档的记录不参与扫描。
SQL> SELECT * FROM TV; INSERT_TIME ----------------- 20130811 09:04:30 20130811 09:08:27 20130811 09:22:30 20130811 09:39:40 20130811 09:45:22 Execution Plan ---------------------------------------------------------- Plan hash VALUE: 1723968289 -------------------------------------------------------------------------- | Id | Operation | Name | ROWS | Bytes | Cost (%CPU)| TIME | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 4 | 8044 | 102 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| TV | 4 | 8044 | 102 (0)| 00:00:01 | -------------------------------------------------------------------------- Predicate Information (IDENTIFIED BY operation id): --------------------------------------------------- 1 - FILTER("TV"."ORA_ARCHIVE_STATE"='0') Note ----- - dynamic statistics used: dynamic sampling (level=2) Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 375 consistent gets 0 physical reads 0 redo SIZE 648 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) 5 ROWS processed
数据库内归档可以跟时间有效性管理一起配合使用。在会话级别激活时间有效性,可以看到检索不再返回任何数据。执行计划中显示filter条件融合了数据库内归档跟时间有效性两层过滤。
SQL> EXEC dbms_flashback_archive.enable_at_valid_time('CURRENT'); PL/SQL PROCEDURE successfully completed. SQL> SELECT * FROM tv; no ROWS selected Execution Plan ---------------------------------------------------------- Plan hash VALUE: 1723968289 -------------------------------------------------------------------------- | Id | Operation | Name | ROWS | Bytes | Cost (%CPU)| TIME | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 3 | 6087 | 102 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| TV | 3 | 6087 | 102 (0)| 00:00:01 | -------------------------------------------------------------------------- Predicate Information (IDENTIFIED BY operation id): --------------------------------------------------- 1 - FILTER("T"."ORA_ARCHIVE_STATE"='0' AND ("T"."VALID_TIME_START" IS NULL OR SYS_EXTRACT_UTC(INTERNAL_FUNCTION("T"."VALID_TIME_START"))SYS_EXTRACT_UTC (SYSTIMESTAMP(6)))) Statistics ---------------------------------------------------------- 34 recursive calls 8 db block gets 397 consistent gets 0 physical reads 0 redo SIZE 347 bytes sent via SQL*Net TO client 532 bytes received via SQL*Net FROM client 1 SQL*Net roundtrips TO/FROM client 0 sorts (memory) 0 sorts (disk) 0 ROWS processed
将时间有效期设置为20130811 09:39:50,根据上一篇文章我们设置的1分钟有效期,只有在20130811 09:39:40插入的这条活跃记录可以被显示出来。
SQL> EXEC dbms_flashback_archive.enable_at_valid_time('ASOF',to_date('20130811 09:39:50','YYYYMMDD HH24:MI:SS')); PL/SQL PROCEDURE successfully completed. SQL> SELECT * FROM TV; INSERT_TIME ----------------- 20130811 09:39:40 Execution Plan ---------------------------------------------------------- Plan hash VALUE: 1723968289 -------------------------------------------------------------------------- | Id | Operation | Name | ROWS | Bytes | Cost (%CPU)| TIME | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 3 | 6087 | 102 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| TV | 3 | 6087 | 102 (0)| 00:00:01 | -------------------------------------------------------------------------- Predicate Information (IDENTIFIED BY operation id): --------------------------------------------------- 1 - FILTER("T"."ORA_ARCHIVE_STATE"='0' AND ("T"."VALID_TIME_START" IS NULL OR INTERNAL_FUNCTION("T"."VALID_TIME_START")TIMESTAMP' 2013-08-11 09:39:50.000000000')) Statistics ---------------------------------------------------------- 35 recursive calls 6 db block gets 398 consistent gets 0 physical reads 0 redo SIZE 550 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
结论:数据库内归档是一个Oracle利用隐藏字段实现的非常简单的功能,但是数据架构人员在规划的时候一定要考虑性能因素。
Share/Save
Related posts:
- Oracle 11g new feature – Virtual Column
- How to Use DBMS_ADVANCED_REWRITE in Oracle 10g
- 【Oracle Database 12c New Feature】How to Learn Oracle (12c New Feature) from Error


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.

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

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

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.

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

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.
