Oracle 中的动态表分区

发布于 2024-08-19 18:42:57 字数 237 浏览 8 评论 0原文

我正在为我的应用程序构建一个数据库存储,该存储由一个具有巨大数据量(数亿条记录)的表组成。我计划在日期字段上建立索引,因为我将不时地对给定时间段内的所有记录进行批量恢复(例如,检索第二天的所有记录,午夜) )。

由于记录数量巨大并且性能是该系统中的一个重要问题,我想知道是否有一种方法可以动态对表进行分区,以便我可以更快地检索记录,创建和截断分区,因为它们不是更需要。例如,在处理完今天的记录后,我将如何为第二天创建一个分区并用其余数据填充它?

I'm in the process of building a database storage for my app consisting on a single table with a huge data volume (hundreds of millions of records). I'm planning on having an index on the date field, since I'll be doing a batch recovery of all the records in a given period of time every now and then (for example, retrieving all records for the following day, at midnight).

Since the number of records is huge and performance is an important concern in this system, I would like to know if there is a way I can dynamically partition my table so that I can retrieve the records faster, creating and truncating partitions as they are no longer needed. For example, how would I go about creating a partition for the following day and populating it with the rest of the data after I'm done processing today's records?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

游魂 2024-08-26 18:42:57

在11g中,我们可以定义INTERVAL分区,当Oracle获得键不适合任何现有范围的新记录时,Oracle将自动创建新分区。这是一个非常酷的功能。 了解更多信息

需要记住的一件事是,分区是企业版许可证之外的一项额外收费。所以使用起来并不便宜。

In 11g we can define INTERVAL partitions, and Oracle will automatically create new partitions when it gets new records whose keys don't fit in any of the existing ranges. This is a very cool feature. Find out more.

One thing to bear in mind is that Partitioning is a chargeable extra on top of the Enterprise Edition license. So it is not cheap to use.

贱贱哒 2024-08-26 18:42:57

您可以通过使用 动态 SQL。您可以使用 EXECUTE IMMEDIATE 或 DBMS_SQL< /code>并且您可以使用 DBMS_JOBDBMS_SCHEDULERDBMS_SCHEDULER 是一项 10g 功能,比 DBMS_JOB 更通用)。

您可能希望首先手动构建分区语句,然后当您对 DDL 有信心时自动执行该过程。您可以在文档中找到所有合成ALTER TABLE 语句

you can automate the process of creating or truncating partitions through the use of dynamic SQL. You would write procedures with either EXECUTE IMMEDIATE or DBMS_SQL and you would schedule them with DBMS_JOB or DBMS_SCHEDULER (DBMS_SCHEDULER is a 10g feature and is more versatile than DBMS_JOB).

You probably want to build the partition statements manually first and automate the process later when you are confident with the DDL. You will find all the synthax in the documentation for the ALTER TABLE statement.

何以心动 2024-08-26 18:42:57

这是我想出的使用 SYSDATE 和偏移量创建分区的示例。我必须使用连接到 SYSDATE 的字符串创建替换参数:

COLUMN temp_var new_value partition_name_01;
SELECT 'TABLE_NAME_' || TO_CHAR(SYSDATE - 3, 'YYYYMMDD') AS temp_var FROM dual;
COLUMN temp_var new_value partition_date_01;
SELECT TO_CHAR(SYSDATE - 3, 'SYYYY-MM-DD') || ' 00:00:00' AS temp_var FROM dual;
COLUMN temp_var new_value partition_name_02;
SELECT 'TABLE_NAME_' || TO_CHAR(SYSDATE - 2, 'YYYYMMDD') AS temp_var FROM dual;
COLUMN temp_var new_value partition_date_02;
SELECT TO_CHAR(SYSDATE - 2, 'SYYYY-MM-DD') || ' 00:00:00' AS temp_var FROM dual;
COLUMN temp_var new_value partition_name_03;
SELECT 'TABLE_NAME_' || TO_CHAR(SYSDATE - 1, 'YYYYMMDD') AS temp_var FROM dual;
COLUMN temp_var new_value partition_date_03;
SELECT TO_CHAR(SYSDATE - 1, 'SYYYY-MM-DD') || ' 00:00:00' AS temp_var FROM dual;
COLUMN temp_var new_value partition_name_04;
SELECT 'TABLE_NAME_' || TO_CHAR(SYSDATE, 'YYYYMMDD') AS temp_var FROM dual;
COLUMN temp_var new_value partition_date_04;
SELECT TO_CHAR(SYSDATE, 'SYYYY-MM-DD') || ' 00:00:00' AS temp_var FROM dual;
CREATE TABLE TABLE_NAME
(
   SEQ_NO                 NUMBER NOT NULL,
   INSERT_DATE            DATE NOT NULL,
   FIRST_NAME             VARCHAR2 (256 BYTE),
   LAST_NAME              VARCHAR2 (256 BYTE),
   ID_NUM                 NUMBER,
   ID_STATUS              NUMBER
)

PARTITION BY RANGE
   (INSERT_DATE)
   SUBPARTITION BY LIST
      (ID_STATUS)
      SUBPARTITION TEMPLATE (
         SUBPARTITION SP1 VALUES (0) TABLESPACE &tblspce,
         SUBPARTITION SP2 VALUES (1) TABLESPACE &tblspce,
         SUBPARTITION SP3 VALUES (2) TABLESPACE &tblspce)

   (
   PARTITION &partition_name_01
      VALUES LESS THAN
      (TO_DATE ('&partition_date_01',
                   'SYYYY-MM-DD HH24:MI:SS',
                   'NLS_CALENDAR=GREGORIAN')),

   PARTITION &partition_name_02
      VALUES LESS THAN
      (TO_DATE ('&partition_date_02',
                   'SYYYY-MM-DD HH24:MI:SS',
                   'NLS_CALENDAR=GREGORIAN')),

   PARTITION &partition_name_03
      VALUES LESS THAN
      (TO_DATE ('&partition_date_03',
                   'SYYYY-MM-DD HH24:MI:SS',
                   'NLS_CALENDAR=GREGORIAN')),
sysdate

   PARTITION &partition_name_04
      VALUES LESS THAN
      (TO_DATE ('&partition_date_04',
                   'SYYYY-MM-DD HH24:MI:SS',
                   'NLS_CALENDAR=GREGORIAN')))

ENABLE ROW MOVEMENT;

Here is a sample of something I came up with for creating partitions using SYSDATE and an offset. I had to create replacement parameters with a string concatenated to the SYSDATE:

COLUMN temp_var new_value partition_name_01;
SELECT 'TABLE_NAME_' || TO_CHAR(SYSDATE - 3, 'YYYYMMDD') AS temp_var FROM dual;
COLUMN temp_var new_value partition_date_01;
SELECT TO_CHAR(SYSDATE - 3, 'SYYYY-MM-DD') || ' 00:00:00' AS temp_var FROM dual;
COLUMN temp_var new_value partition_name_02;
SELECT 'TABLE_NAME_' || TO_CHAR(SYSDATE - 2, 'YYYYMMDD') AS temp_var FROM dual;
COLUMN temp_var new_value partition_date_02;
SELECT TO_CHAR(SYSDATE - 2, 'SYYYY-MM-DD') || ' 00:00:00' AS temp_var FROM dual;
COLUMN temp_var new_value partition_name_03;
SELECT 'TABLE_NAME_' || TO_CHAR(SYSDATE - 1, 'YYYYMMDD') AS temp_var FROM dual;
COLUMN temp_var new_value partition_date_03;
SELECT TO_CHAR(SYSDATE - 1, 'SYYYY-MM-DD') || ' 00:00:00' AS temp_var FROM dual;
COLUMN temp_var new_value partition_name_04;
SELECT 'TABLE_NAME_' || TO_CHAR(SYSDATE, 'YYYYMMDD') AS temp_var FROM dual;
COLUMN temp_var new_value partition_date_04;
SELECT TO_CHAR(SYSDATE, 'SYYYY-MM-DD') || ' 00:00:00' AS temp_var FROM dual;
CREATE TABLE TABLE_NAME
(
   SEQ_NO                 NUMBER NOT NULL,
   INSERT_DATE            DATE NOT NULL,
   FIRST_NAME             VARCHAR2 (256 BYTE),
   LAST_NAME              VARCHAR2 (256 BYTE),
   ID_NUM                 NUMBER,
   ID_STATUS              NUMBER
)

PARTITION BY RANGE
   (INSERT_DATE)
   SUBPARTITION BY LIST
      (ID_STATUS)
      SUBPARTITION TEMPLATE (
         SUBPARTITION SP1 VALUES (0) TABLESPACE &tblspce,
         SUBPARTITION SP2 VALUES (1) TABLESPACE &tblspce,
         SUBPARTITION SP3 VALUES (2) TABLESPACE &tblspce)

   (
   PARTITION &partition_name_01
      VALUES LESS THAN
      (TO_DATE ('&partition_date_01',
                   'SYYYY-MM-DD HH24:MI:SS',
                   'NLS_CALENDAR=GREGORIAN')),

   PARTITION &partition_name_02
      VALUES LESS THAN
      (TO_DATE ('&partition_date_02',
                   'SYYYY-MM-DD HH24:MI:SS',
                   'NLS_CALENDAR=GREGORIAN')),

   PARTITION &partition_name_03
      VALUES LESS THAN
      (TO_DATE ('&partition_date_03',
                   'SYYYY-MM-DD HH24:MI:SS',
                   'NLS_CALENDAR=GREGORIAN')),
sysdate

   PARTITION &partition_name_04
      VALUES LESS THAN
      (TO_DATE ('&partition_date_04',
                   'SYYYY-MM-DD HH24:MI:SS',
                   'NLS_CALENDAR=GREGORIAN')))

ENABLE ROW MOVEMENT;
故乡的云 2024-08-26 18:42:57

有一种产品可以自动处理它。 PartitionManager for Oracle 提供基于组织保留的自动分区管理,包括清除和归档旧数据、统计复制等。您可以在 http://www.xyrosoft.com

There's a product that takes care of it automatically. PartitionManager for Oracle provides automatic partition management, based on the organization retention, including purging and archiving old data, statistics copy etc. You can try it out at http://www.xyrosoft.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文