简体   繁体   English

Oracle SQL使用变量分区名称

[英]Oracle SQL use variable partition name

I run a daily report that has to query another table which is updated separately. 我运行每日报告,该报告必须查询另一个单独更新的表。 Due to the high volume of records in the source table (8M+ per day) each day is stored in it's own partition. 由于源表中的记录量很大(每天8M +),因此每天都将数据存储在其自己的分区中。 The partition has a standard format as P ... 4 digit year ... 2 digit month ... 2 digit date , so yesterday's partition is P20140907 . 该分区的标准格式为P ... 4 digit year ... 2 digit month ... 2 digit date ,因此昨天的分区是P20140907

At the moment I use this expression, but have to manually change the name of the partition each day: 目前,我使用此表达式,但每天必须手动更改分区的名称:

select * from <source_table> partition (P20140907) where ....

By using sysdate , toChar and Concat I have created another table called P_NAME2 that will automatically generate and update a string value as the name of the partition that I need to read. 通过使用sysdatetoCharConcat我创建了另一个名为P_NAME2表,该表将自动生成并更新字符串值作为需要读取的分区的名称。 Now I need to update my main query so it does this: 现在,我需要更新主查询,以便执行此操作:

select * from <source_table> partition (<string from P_NAME2>) where ....

You are working too hard. 你太辛苦了 Oracle already does all these things for you. Oracle已经为您完成了所有这些事情。 If you query the table using the correct date range oracle will perform the operation only on the relevant partitions - this is called pruning . 如果您使用正确的日期范围查询表,则oracle将仅在相关分区上执行该操作-这称为修剪

I suggest reading the docs on that. 我建议阅读有关文档

If you'r still skeptic, Query all_tab_partitions.HIGH_VALUE to get each partitions high value (the table you created ... ). 如果您仍然持怀疑态度,请查询all_tab_partitions.HIGH_VALUE以使每个分区具有较高的价值(您创建的表...)。

I thought I'd pop back to share how I solved this in the end. 我以为我会回头分享我最终如何解决这个问题。 The source database has a habit of leaking dates across partitions which is why queries for one day were going outside a single partition. 源数据库习惯于跨分区泄漏日期,这就是为什么一天的查询不在单个分区内的原因。 I can't affect this, just work around it ... 我不能影响这一点,只是解决它...

begin    
execute immediate 
'create table LL_TEST as
select *
from SCHEMA.TABLE Partition(P'||TO_CHAR(sysdate,'YYYYMMDD')||')
where COLUMN_A=''Something''
and COLUMN_B=''Something Else''
';
end
;

Using the PL/SQL script I create the partition name with TO_CHAR(sysdate,'YYYYMMDD') and concatenate the rest of the query around it. 使用PL / SQL脚本,我使用TO_CHAR(sysdate,'YYYYMMDD')创建分区名称,并将其余的查询串联起来。

Note that the values you are searching for in the where clause require double apostrophes so to send 'Something' to the query you need ''Something'' in the script. 请注意,您在where子句中搜索的值需要双撇号,因此要将'Something'发送到查询中,您需要在脚本中使用''Something''

It may not be pretty, but it works on the database that I have to use. 它可能并不漂亮,但是可以在我必须使用的数据库上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM