Carregar ou arquivar dados em massa com exchange partition
OracleParticionamentoComando
-- 1) Staging table that exactly matches the target (12.2+):
CREATE TABLE customer_app.sale_stage
FOR EXCHANGE WITH TABLE customer_app.sale_part;
-- 2) Load the staging table and build indexes matching the local indexes:
INSERT /*+ APPEND */ INTO customer_app.sale_stage
SELECT * FROM customer_app.sale_load WHERE created_at >= DATE '2027-01-01';
COMMIT;
CREATE INDEX customer_app.sale_stage_customer_ix ON customer_app.sale_stage (customer_id);
-- 3) Swap (dictionary-only operation, no data movement):
ALTER TABLE customer_app.sale_part
EXCHANGE PARTITION p2027 WITH TABLE customer_app.sale_stage
INCLUDING INDEXES WITHOUT VALIDATION
UPDATE GLOBAL INDEXES;
-- 4) Verify:
SELECT COUNT(*) FROM customer_app.sale_part PARTITION (p2027);
SELECT index_name, partition_name, status
FROM dba_ind_partitions
WHERE index_owner = 'CUSTOMER_APP' AND status <> 'USABLE';O que faz
Troca no dicionário o segmento de uma partição pelo segmento de uma tabela comum; milhões de linhas "entram" na tabela ou "saem" dela para arquivamento em segundos. FOR EXCHANGE WITH TABLE cria uma staging table que garante compatibilidade de colunas e de colunas ocultas.
Quando usar
Para cargas em massa de DWH/ETL, mover uma partição antiga para uma tabela de arquivo e preparar índices e estatísticas sem afetar a tabela em uso durante a carga.
Atenção
Exige a opção Partitioning (EE com custo extra). WITHOUT VALIDATION não verifica se as linhas de staging cabem nos limites da partição; dados errados ficam na partição errada e as consultas podem retornar resultados incorretos; na dúvida use WITH VALIDATION (full scan). Sem UPDATE GLOBAL INDEXES os índices globais ficam UNUSABLE. Os estados das constraints unique/PK devem coincidir dos dois lados.