sql >> Database >  >> RDS >> Oracle

Hoe weet je in welke partitie een rij zou gaan, gegeven een bekende partitiesleutelwaarde in Oracle?

Met deze testgegevens

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

Zoals hier beschreven https://jonathanlewis.wordpress.com/2009/11 /21/ora_hash-functie/

je krijgt

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   

Merk op dat de parameter 3 in ora_hash is het aantal (sub)partities afgetrokken met 1. (=4-1). U zult extra bewerkingen moeten uitvoeren als het aantal partities geen macht van twee is (wat niet wordt aanbevolen) zoals beschreven in de referentie.

U kunt het resultaat verifiëren met een expliciete partitie-query zoals hieronder

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

En natuurlijk werkt het ook voor nieuwe sleutels, nieuw voor 1237 die 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,
(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            
---------- --------------------- ------------------------------
      1237                     2 R0_H1 

De "voorspelde" subpartitie is R0_H1 , laten we eens kijken waar de INSERT naartoe gaat:

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

Maar wees voorzichtig, want het is een IMO niet gedocumenteerde functie ...




  1. SQL Server-equivalent van MySQL's USING

  2. Selectievakjes gebruiken om specifieke gegevens in een database op te halen

  3. pijplijnfunctie met cursorparameter oracle

  4. Cannot SUM(TO_NUMBER(varchar2 field)) :ORA 01722 [ORACLE]