69人参与 • 2026-05-09 • Oracle
create table test_record (
id varchar(255) primary key ,
message_id varchar(555) not null,
receive_time timestamp not null,
message_type varchar(555),
client_id varchar(555),
smsc varchar(555),
calling_number varchar(555),
called_number varchar(555),
message_content varchar(4000),
response_time timestamp,
response_command_status integer,
interval_ms bigint,
match_strategy_id varchar(555),
match_strategy_name varchar(555),
monitoring_strategy varchar(555),
action_time timestamp,
action_type varchar(555),
action_desc varchar(555),
match_blacklist_handle_type varchar(255) null,
match_strategy_violation_reason varchar(255) null
)这是一张记录表,核心字段说明:
id:主键,唯一标识每条记录receive_time:分区键,消息接收时间,用于按时间分区calling_number/called_number:主叫 / 被叫号码,用于高频查询索引tablespace biz_data
partition by range (receive_time)
interval (numtodsinterval(1, 'day'))
(
partition p_init values less than (to_date('2026-01-01', 'yyyy-mm-dd'))
);
| 配置项 | 含义 | 关键说明 |
|---|---|---|
| tablespace biz_data | 表存储在 biz_data 表空间 | 需提前创建该表空间,用于数据隔离与管理 |
| partition by range (receive_time) | 按 receive_time 做范围分区 | 时间范围分区是日志 / 流水表的标准方案,按时间维度快速归档 / 查询 |
| interval (numtodsinterval(1, 'day')) | 自动按天创建分区 | oracle 11g+ 特性,无需手动建分区,插入数据时自动生成新分区 |
| partition p_init values less than (to_date('2026-01-01')) | 初始分区 | 存储 2026-01-01 00:00:00 之前的所有数据,作为兜底分区 |
receive_time >= 2026-01-01 的数据时,oracle 会自动创建第一个日分区,例如 sys_pxxxx(系统生成名),对应 2026-01-01 当天的数据1 day,即每个分区存储完整一天的所有数据create index idx_receive_time_desc_composite on test_record (receive_time desc) local; create index idx_receive_calling_number_index on test_record (calling_number) local; create index idx_receive_called_number_index on test_record (called_number) local;
local 关键字:本地分区索引,索引会跟随表的分区,每个分区对应一个索引分区receive_time desc:按时间倒序,适配 “查最近 n 天数据” 的高频场景calling_number/called_number:按号码查询,适配反垃圾短信的号码溯源需求p_init,固定不变,存储 2026-01-01 之前的所有数据test_record(主表名)+ 分区名,即 test_record 本身是逻辑表,物理数据存储在各个分区中oracle 会自动生成系统命名的分区名,格式为:
sys_p<唯一数字>
例如:sys_p12345、sys_p67890
user_tab_partitions 查看分区名与对应时间范围select partition_name, high_value from user_tab_partitions where table_name = 'test_record';
oracle 12cr2+ 支持 interval 分区自定义命名模板,通过 store in + 模板实现,示例如下:
-- 12cr2+ 支持的自定义分区名语法
create table test_record (
-- 表结构不变,省略...
)
tablespace biz_data
partition by range (receive_time)
interval (numtodsinterval(1, 'day'))
store in (biz_data) -- 指定分区存储的表空间
(
partition p_init values less than (to_date('2026-01-01', 'yyyy-mm-dd'))
);
-- 自定义分区名模板(12cr2+ 特性)
alter table test_record
set interval partition template 'p_yyyy_mm_dd';
p_yyyy_mm_dd 会自动替换为分区对应的日期,例如:
2026-01-01 分区 → p_2026_01_012026-01-02 分区 → p_2026_01_0211g 无法自动生成自定义名,只能通过事后重命名实现:
-- 1. 先查询自动生成的分区名与对应时间 select partition_name, high_value from user_tab_partitions where table_name = 'test_record'; -- 2. 手动重命名(例如将 sys_p12345 重命名为 p_2026_01_01) alter table test_record rename partition sys_p12345 to p_2026_01_01;
receive_time 符合要求)receive_time 设为 not null,符合要求)delete
-- 删除 2026-01-01 之前的历史数据(删除 p_init 分区) alter table test_record drop partition p_init;
localp_2026_01_01 对应索引分区 p_2026_01_011 day 调整为 12 hour 或 6 hour,提升单分区查询性能analyze,但需确保数据库统计信息自动收集任务开启-- 1. 查看表的分区信息(分区名、时间范围、行数)
select
partition_name,
high_value,
num_rows
from user_tab_partitions
where table_name = 'test_record'
order by partition_position;
-- 2. 查看本地索引的分区信息
select
index_name,
partition_name,
status
from user_ind_partitions
where index_name in ('idx_receive_time_desc_composite', 'idx_receive_calling_number_index', 'idx_receive_called_number_index');
-- 3. 查看分区表的分区键与间隔配置
select
partitioning_type,
interval,
partition_key
from user_part_tables
where table_name = 'test_record';
sys_pxxxx),12cr2+ 支持自定义日期格式的分区名,11g 需手动重命名到此这篇关于oracle 自动分区表(interval partition)的使用的文章就介绍到这了,更多相关oracle 自动分区表内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论