DBAtlas · Atlas de operações de banco de dados

Copiar dados entre tabelas / ambientes

Oracle

-- Into an existing table
INSERT INTO target_tbl (id, name) SELECT id, name FROM source_tbl WHERE status = 'ACTIVE';

-- By creating a new table (CTAS)
CREATE TABLE target_tbl AS SELECT * FROM source_tbl;

-- From a remote database via a database link
INSERT INTO target_tbl SELECT * FROM source_tbl@remote_db;

No mesmo banco de dados, use INSERT ... SELECT ou CREATE TABLE AS SELECT (CTAS). Para cópias entre ambientes, prefere-se um database link (@remote_db) ou o Data Pump. O hint APPEND acelera com direct-path.

PostgreSQL

-- Same database
INSERT INTO target_tbl (id, name) SELECT id, name FROM source_tbl;
CREATE TABLE target_tbl AS SELECT * FROM source_tbl;

-- Between environments (psql pipe)
-- pg_dump -t source_tbl sourcedb | psql targetdb
-- or read the remote table with postgres_fdw and INSERT

No mesmo DB, use INSERT ... SELECT / CTAS. Entre servidores, use um pipeline pg_dump | psql ou copie por uma foreign table com postgres_fdw.

MySQL

-- Same database
INSERT INTO target_tbl (id, name) SELECT id, name FROM source_tbl;
CREATE TABLE target_tbl AS SELECT * FROM source_tbl;

-- Between servers (mysqldump pipe)
-- mysqldump sourcedb source_tbl | mysql -h targethost targetdb

No mesmo DB, INSERT ... SELECT ou CREATE TABLE ... AS SELECT. Entre servidores diferentes, é comum encadear a saída do mysqldump para o cliente mysql. CTAS não copia índices nem constraints.

SQL Server

-- Into an existing table
INSERT INTO target_tbl (id, name) SELECT id, name FROM source_tbl;

-- By creating a new table
SELECT * INTO target_tbl FROM source_tbl;

-- Between servers: linked server
INSERT INTO target_tbl SELECT * FROM [REMOTE_SRV].shop.dbo.source_tbl;

No SQL Server, a cópia para uma nova tabela é feita com SELECT ... INTO (o equivalente ao CTAS). Entre servidores, use um linked server (nome de quatro partes), bcp out/in ou SSIS.

MongoDB

// Write an aggregation result to another collection
db.source_tbl.aggregate([
  { $match: { status: "ACTIVE" } },
  { $out: "target_tbl" }
]);

// Merge into an existing collection (upsert)
db.source_tbl.aggregate([ { $merge: { into: "target_tbl", whenMatched: "merge", whenNotMatched: "insert" } } ]);

$out substitui totalmente a coleção de destino; $merge mescla com/insere em documentos existentes (no mesmo ou em outro DB). Para movimentações entre servidores, use mongodump | mongorestore ou mongoexport/mongoimport.

Redis

# Move a single key to another server (deleted from the source)
MIGRATE target_host 6379 customer:1 0 5000

# To copy (without deleting): DUMP + RESTORE
DUMP customer:1          # returns the serialized value
RESTORE customer:1new 0 "<dump-data>"

MIGRATE move a chave atomicamente para a instância de destino e a exclui da origem (com a opção COPY, não exclui). Para copiar na mesma instância, use o comando COPY (6.0+) ou DUMP+RESTORE.

Elasticsearch

# Within the same cluster
POST _reindex
{ "source": { "index": "source_tbl" }, "dest": { "index": "target_tbl" } }

# From a remote cluster (remote reindex; requires the whitelist setting)
POST _reindex
{ "source": { "remote": { "host": "http://remote:9200" }, "index": "source_tbl" },
  "dest": { "index": "target_tbl" } }

_reindex copia entre índices; pode ser filtrado com uma consulta e transformado com um script. Entre clusters, use o remote reindex (reindex.remote.whitelist deve estar definido no destino). Trabalhos grandes são paralelizados com slices.

Abrir no DBAtlas →