質問

I have an use case where i need to partition an Oracle table based on city. Now I do not know the city beforehand. So in this case how will i create the partition. (Btw I have already done hash partition(it does not ensure data of two city will be in same partition) but I want to have data of separate city to be in separate partition)

役に立ちましたか?

解決

The most sensible approach would be to use stored procedures for inserting / updating, to ensure the partitions exist (as suggested by Egor Skriptunoff).

If you don't want to use this approach, you can use list partitioning with a pre-defined list of cities and a default partition where all values for "unknown" cities will be stored. You can then set up a periodic cleanup job that splits the "unknown" partition and creates the partitions for the new cities:

Setup table with partitions for three known cities

create table shops (
  pk number not null primary key, 
  name varchar2(30) not null, 
  city varchar2(30) not null)
partition by list(city)
(partition shops_paris values ('Paris'),
 partition shops_berlin values ('Berlin'),
 partition shops_london values ('London'),
 partition shops_unknown values (default)
 );

Add some data (note that we have one "unknown" city, Munich)

insert into shops(pk, name, city) values (1, 'Manufactum', 'Munich');
insert into shops(pk, name, city) values (2, 'KaDeWe', 'Berlin');
insert into shops(pk, name, city) values (3, 'Harrods', 'London');
insert into shops(pk, name, city) values (4, 'Galeries Lafayette', 'Paris');  

Check our unknown partition -> we got a new city, 'Munich'

select * from shops partition (shops_unknown);

Create the new partition

alter table shops split partition shops_unknown 
  values ('Munich') into (partition shops_munich, partition shops_unknown);

他のヒント

New partition should be created explicitly.
Include all associated logic into a procedure:

  • check if partition exists
  • create new one if needed
  • insert value into table
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top