【问题标题】:Can we have a where clause for delete from <tab> partition <part.>?我们可以有一个从 <tab> 分区 <part.> 删除的 where 子句吗?
【发布时间】:2011-07-16 12:47:04
【问题描述】:
我有一个基于时间戳分区的表(比如partition1 将有 9 个月大的数据,partition2 有 6 个月大的数据,partition3 有 3 个月大的数据等等)
我需要根据特定分区的条件删除数据。
delete from table1 partition partition3
where group id in ( select * from table1 where group_code='pilot');
这个操作是否只会从partiton3删除?
【问题讨论】:
标签:
sql
oracle
oracle10g
partitioning
【解决方案1】:
首先语法是:
delete from table1 partition(partition3)
其次,您不应引用分区名称。它创建了对表的物理布局的依赖。它可能今天每月分区,但它可能会在未来的某个时间或几周或几天重新分区。发生这种情况时,您的代码将中断。 Oracle 可以从分区列上的谓词很好地推断出分区。尝试忘记正在分区的表。
第三,由于select * 与 ID 进行比较,您的子选择将失败。
您要查找的语句可能如下所示:
delete
from table1
where group_code = 'pilot'
and partition_column = 'some value';
【解决方案2】:
分区是透明的,这意味着你可以简单地做一个常规
delete table1
where group_id in (select group_id from table2 where group_code = 'pilot')
and your_date_column
between trunc(sysdate,'mm') - interval '3' month
and trunc(sysdate,'mm') - interval '2' month
优化器会知道第二个谓词意味着只有partition3中的数据需要删除。
问候,
抢。