SQL >> Database >  >> RDS >> Oracle

Hoe bepaal ik bij welke partitie een rij terechtkomt met een bekende partitiesleutel in Oracle?

Met deze testdata:

INSERT INTO foos VALUES (1234, SYSDATE);
INSERT INTO foos VALUES (1235, SYSDATE);
INSERT INTO foos VALUES (1236, SYSDATE);

en zoals beschreven in Jonathan Lewis' artikel over ORA_HASH krijg je:

with hsh as (
  select BATCH_ID, ora_hash(BATCH_ID, 3) + 1 subpartition_position
  from foos
)
select BATCH_ID, SUBPARTITION_POSITION,
       (select subpartition_name
        from user_tab_subpartitions
        where table_name = 'FOOS'
          and SUBPARTITION_POSITION = hsh.SUBPARTITION_POSITION) subpartition_name
from hsh;

BATCH_ID  SUBPARTITION_POSITION  SUBPARTITION_NAME
1236      1                      R0_H0
1235      3                      R0_H2
1234      4                      R0_H3

De parameter 3 in ora_hash is het aantal (sub)partities min één (4 - 1). Is het aantal partities geen macht van twee (wat niet wordt aangeraden), dan is extra verwerking nodig, zoals in de naslagbeschrijving staat.

Controleer het resultaat met expliciete partitie-queries:

select * from foos subpartition( R0_H0 );  -- 1236
select * from foos subpartition( R0_H1 );  -- leeg
select * from foos subpartition( R0_H2 );  -- 1235
select * from foos subpartition( R0_H3 );  -- 1234

Het werkt ook voor nieuwe sleutels, bijvoorbeeld 1237 dat nog niet in de tabel staat:

with hsh as (
  select 1237 BATCH_ID, ora_hash(1237, 3) + 1 subpartition_position from dual
)
select BATCH_ID, SUBPARTITION_POSITION, ... 

BATCH_ID  SUBPARTITION_POSITION  SUBPARTITION_NAME
1237      2                      R0_H1

De "voorspelde" subpartitie is R0_H1. Even kijken waar de insert heen gaat:

INSERT INTO foos VALUES (1237, SYSDATE);
select * from foos subpartition( R0_H1 );  -- 1237

Wees voorzichtig: dit is naar mijn mening geen gedocumenteerde feature.

  1. Waarom MOD(ROWNUM,2) faalt en even rijen selecteren

  2. ISO8601-timestamps met fractionele seconden parsen naar Oracle-dates

  3. Een UTC-timestamp uit Oracle halen

  4. Één rij selecteren in Oracle SQL