DBAtlas · 데이터베이스 운영 아틀라스

테이블 / 환경 간 데이터 복사

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;

같은 데이터베이스 안에서는 INSERT ... SELECT 또는 CREATE TABLE AS SELECT(CTAS)를 사용한다. 환경 간 복사에는 database link(@remote_db)나 Data Pump가 권장된다. APPEND 힌트는 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

같은 DB 안에서는 INSERT ... SELECT / CTAS를 사용한다. 서버 간에는 pg_dump | psql 파이프라인을 쓰거나 postgres_fdw로 foreign table을 통해 복사한다.

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

같은 DB 안에서는 INSERT ... SELECT 또는 CREATE TABLE ... AS SELECT. 다른 서버 간에는 mysqldump 출력을 mysql 클라이언트로 파이프하는 방법이 일반적이다. CTAS는 인덱스와 제약 조건을 복사하지 않는다.

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;

SQL Server에서 새 테이블로의 복사는 SELECT ... INTO(CTAS에 해당)로 수행한다. 서버 간에는 linked server(4부분 이름), bcp out/in 또는 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은 대상 컬렉션을 통째로 교체하고, $merge는 기존 문서와 병합/삽입한다(같은 DB 또는 다른 DB). 서버 간 이동에는 mongodump | mongorestore 또는 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는 키를 대상 인스턴스로 원자적으로 옮기고 소스에서 삭제한다(COPY 옵션을 쓰면 삭제하지 않음). 같은 인스턴스 안에서 복사하려면 COPY 명령(6.0+)이나 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는 인덱스 간 복사를 수행하며, query로 필터링하고 script로 변환할 수 있다. 클러스터 간에는 remote reindex를 사용한다(대상에 reindex.remote.whitelist 정의 필요). 대규모 작업은 slices로 병렬화한다.

DBAtlas에서 열기 →