【技术分享】Oracle分区使用详解
发布作者:微思网络 发布时间:2026-02-10 浏览量:0次

oracle分区使用
又开始了一年一度的踩坑环节,刚刚和公司的一个大佬讨论了一下数据采集的表结构,打算按月分表,按天分区。这样在查询的时候会大大的减少工作量:
1、首先选表时,就把选择的月份拼接在前面,所以这里的查询其实需要判断,不能查询未到的日期。
2、查询表时,将指定的分表字段带进查询中。
按月自动分表
create table fq_test (id number,name varchar2(32),create_time date)partition by range (create_time) interval (numtoyMinterval (1,'MONTH'))(partition p_2024_07_12 values less than (to_date('2024-07-12', 'yyyy-mm-dd')));
按照天分表:
create table fq_test (id number,name varchar2(32),create_time date)partition by range (create_time) interval (NUMTODSINTERVAL (1,'day'))(partition p_2024_07_12 values less than (to_date('2024-07-12', 'yyyy-mm-dd')));
按照指定的数据分区,如果不存在,就创建一个新的分区
select table_name, partition_name ,HIGH_VALUE from user_tab_partitions where TABLE_NAME='ENMOTECH'CREATE TABLE enmotech (PartID integer not null,CretTm date not null,PartCD varchar2(2) not null) partition by list (partcd) automatic (partition pBJ values ('a'),partition pCD values ('b'),partition pGZ values ('c'));
oracle自动创建分区表,自动间隔分区(分区频率一年一次)
1.分区表必须有个date类型的字段,根据date类型的字段进行分区;
2.分区sql
create table CCMMESPRD.JM_TEST_DATA(object_rrn NUMBER(19) not null,org_rrn NUMBER(19),is_active VARCHAR2(1),created DATE,--日期格式created_by VARCHAR2(32),updated DATE,updated_by VARCHAR2(32),lock_version NUMBER(19),component_id VARCHAR2(128),lot_id VARCHAR2(32),barcode VARCHAR2(32),part_name VARCHAR2(32),line_id VARCHAR2(32),component_alias VARCHAR2(32),step_name VARCHAR2(32),mac VARCHAR2(32),ip VARCHAR2(32),equipment_id VARCHAR2(32),test_user VARCHAR2(32),test_date DATE,test_result VARCHAR2(32),test_data CLOB,action_type VARCHAR2(32),fix_code VARCHAR2(50),fix_location VARCHAR2(10),testt2 VARCHAR2(10),defect_code VARCHAR2(512))PARTITION BY RANGE (created)INTERVAL (NUMTOYMINTERVAL(1, 'YEAR')) -- 自动按年扩展分区(PARTITION p_start VALUES LESS THAN (DATE '2026-01-01'));--起始时间是20260101
3.插入2027年数据时,系统自动创建分区 SYS_P2027的分区



