Home Database Mysql Tutorial 【Oracle Database 12c New Feature】ILM – In-Database

【Oracle Database 12c New Feature】ILM – In-Database

Jun 07, 2016 pm 04:35 PM
database new oracle

本文介绍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中使用。

1

2

3

SQL> ALTER TABLE TV ROW archival;

  

TABLE altered.

Copy after login

Oracle仍然是使用隐藏列来实现这个功能的,在启用该特性以后,会自动在表上增加ORA_ARCHIVE_STATE字段,这是一个VARCHAR2(4000)的字段。

1

2

3

4

5

6

7

8

9

10

11

12

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.

Copy after login

先检查一下TV表中的数据分布,一共有9个不同的时间段,前面5个都只有1条记录,后面4个则有大量测试记录。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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.

Copy after login

尝试将所有20130811 09:50之后的记录全部设置为归档模式。直接使用UPDATE语句将ORA_ARCHIVE_STATE字段更新为任意非0的字符,0表示该记录是活跃的,任何非0字符都表示该记录被归档。

1

2

3

SQL> UPDATE TV SET ORA_ARCHIVE_STATE = '20' WHERE INSERT_TIME>to_date('20130811 09:50','YYYYMMDD HH24:MI');

  

77472 ROWS updated.

Copy after login

再次执行相同的查询语句,可以看到只存在活跃的5条记录了。

1

2

3

4

5

6

7

8

9

10

11

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.

Copy after login

可以在会话级别设置即使是记录被归档,也仍然显示出来。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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.

Copy after login

检查ORA_ARCHIVE_STATE值,可以看到所有活跃数据的ORA_ARCHIVE_STATE字段值均为0,这也是在表级别启用数据库内归档以后的默认值。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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.

Copy after login

将其中的一些记录的ORA_ARCHIVE_STATE字段更新为另外的非0字符。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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

Copy after login

在会话级别重新设置不显示归档数据,可以看到只要是ORA_ARCHIVE_STATE字段不为0的记录都不会显示。

1

2

3

4

5

6

7

8

9

10

11

12

13

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

Copy after login

性能考虑,这一点数据库内归档与时间有效性是相同的,都只是对隐藏字段进行了filter操作。即使是只显示活跃数据,也仍然需要扫描全表。这一点在真实应用中可以通过创建索引来避免全表扫描,可以参看MOS Note: Potential SQL Performance Degradation When In Database Row Archiving (Doc ID 1579790.1),也就是数据库内归档只应该在一个具备良好性能的SQL基础上对返回结果进行过滤,而不要期望归档的记录不参与扫描

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

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

Copy after login

数据库内归档可以跟时间有效性管理一起配合使用。在会话级别激活时间有效性,可以看到检索不再返回任何数据。执行计划中显示filter条件融合了数据库内归档跟时间有效性两层过滤。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

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

Copy after login

将时间有效期设置为20130811 09:39:50,根据上一篇文章我们设置的1分钟有效期,只有在20130811 09:39:40插入的这条活跃记录可以被显示出来。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

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

Copy after login

结论:数据库内归档是一个Oracle利用隐藏字段实现的非常简单的功能,但是数据架构人员在规划的时候一定要考虑性能因素。

Share/Save

Related posts:

  1. Oracle 11g new feature – Virtual Column
  2. How to Use DBMS_ADVANCED_REWRITE in Oracle 10g
  3. 【Oracle Database 12c New Feature】How to Learn Oracle (12c New Feature) from Error
YARPP
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check tablespace size of oracle How to check tablespace size of oracle Apr 11, 2025 pm 08:15 PM

To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

How to view instance name of oracle How to view instance name of oracle Apr 11, 2025 pm 08:18 PM

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

How to encrypt oracle view How to encrypt oracle view Apr 11, 2025 pm 08:30 PM

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

How to uninstall Oracle installation failed How to uninstall Oracle installation failed Apr 11, 2025 pm 08:24 PM

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

How to delete all data from oracle How to delete all data from oracle Apr 11, 2025 pm 08:36 PM

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

How to check invalid numbers of oracle How to check invalid numbers of oracle Apr 11, 2025 pm 08:27 PM

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

How to set up users of oracle How to set up users of oracle Apr 11, 2025 pm 08:21 PM

To create a user in Oracle, follow these steps: Create a new user using the CREATE USER statement. Grant the necessary permissions using the GRANT statement. Optional: Use the RESOURCE statement to set the quota. Configure other options such as default roles and temporary tablespaces.

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

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.

See all articles