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

마이그레이션 후 검증(행 수/체크섬)

Oracle

-- Compare row counts on source and target
SELECT 'customer' table_name, COUNT(*) row_count FROM customer;

-- To find content differences at object level, DBMS_COMPARISON (via db link)
BEGIN DBMS_COMPARISON.CREATE_COMPARISON(
  comparison_name => 'COMPARE_CUSTOMER',
  schema_name => 'SHOP', object_name => 'CUSTOMER',
  dblink_name => 'TARGET_DB'); END;
/
-- SELECT DBMS_COMPARISON.COMPARE('COMPARE_CUSTOMER', scan_info) FROM dual;

가장 빠른 확인은 양쪽에서 테이블별 COUNT(*)를 비교하는 것이다. 실제 행 수준 내용 차이를 찾으려면 DBMS_COMPARISON 패키지를 사용한다. database link를 통해 소스와 대상 테이블을 행 단위로 비교하고 차이를 보고/수렴할 수 있다. 체크섬은 ORA_HASH 또는 STANDARD_HASH로 컬럼 다이제스트를 구할 수 있다.

PostgreSQL

-- Row count
SELECT count(*) FROM customer;

-- Ordered digest (checksum) of the whole table content — compare on both sides
SELECT md5(string_agg(t.row_md5, '' ORDER BY t.row_md5)) AS digest
FROM (SELECT md5(customer::text) AS row_md5 FROM customer) t;

첫 단계는 각 테이블의 count(*)를 비교하는 것이다. 내용 검증은 각 행의 텍스트 표현을 md5하고 정렬 순서대로 이어 붙인 뒤 다시 해시한다. 이렇게 하면 양쪽에서 순서에 무관한 체크섬을 얻는다. 대용량 데이터는 변경 가능/키 컬럼에 대해서만 다이제스트를 구한다. 재현성을 위해 PK 기준 정렬이 중요하다.

MySQL

-- Built-in table checksum (run on both servers and compare)
CHECKSUM TABLE customer;

-- Row count
SELECT COUNT(*) FROM customer;

-- Preferred tool for live/large environments (compares row chunks online):
-- pt-table-checksum h=source,D=shop  (Percona Toolkit)

MySQL에서 CHECKSUM TABLE은 테이블 내용의 단일 체크섬을 반환한다. 소스와 대상에서 실행해 비교한다(엔진/버전이 같아야 함). 행 수는 COUNT(*)를 사용한다. 운영 중인 환경과 복제 일관성 확인에서는 Percona Toolkit의 pt-table-checksum이 업계 표준이며, chunk 단위로 온라인에서 낮은 영향으로 비교한다.

SQL Server

-- Full row-level comparison (official tool that produces differences)
tablediff -sourceserver S1 -sourcedatabase shop -sourcetable customer \
  -destinationserver S2 -destinationdatabase shop -destinationtable customer

-- Quick aggregate checksum (single query, compare on both sides):
SELECT COUNT_BIG(*) row_count, CHECKSUM_AGG(BINARY_CHECKSUM(*)) digest FROM dbo.customer;

SQL Server에서 행 수준 차이를 찾는 공식 명령줄 도구는 tablediff다(어떤 행이 추가/변경되었는지 보고하고, 선택 옵션 -f로 수정 스크립트를 작성). 빠른 전체 확인에는 CHECKSUM_AGG(BINARY_CHECKSUM(*))가 단일 테이블 다이제스트를 제공하며, COUNT_BIG(*)와 함께 양쪽을 비교한다(행 순서와 무관).

MongoDB

// Compare document counts (on source and target)
db.customer.countDocuments({});

// For a content checksum, dbHash (md5 digest at collection/DB level):
db.runCommand({ dbHash: 1, collections: ["customer"] });
// -> compare the md5 field across both environments.

첫 확인은 countDocuments()로 양쪽의 문서 수를 비교하는 것이다(estimatedDocumentCount는 빠르지만 메타데이터에 의존하므로 정확한 수는 countDocuments를 사용). 내용 검증에는 dbHash 명령이 컬렉션/데이터베이스 수준의 md5 다이제스트를 생성하므로 두 환경의 해시를 비교한다. 인덱스는 getIndexes()로 별도 확인한다.

Redis

# Compare key counts (on source and target)
redis-cli DBSIZE

# For a memory/data summary and key distribution:
redis-cli INFO keyspace     # db0:keys=... information
# For full content verification: DUMP each key and compare with the target
# (e.g. redis-cli --scan | xargs -I{} redis-cli DUMP {})

Redis의 기본 검증은 DBSIZE로 전체 키 수를 비교하는 것이며, 데이터베이스별 분포는 INFO keyspace가 보여 준다. Redis에는 기본 테이블 체크섬이 없다. 정확한 내용 검증이 필요하면 키별 DUMP 직렬화를 구해 양쪽을 비교한다(TTL 차이에 유의). riot compare 도구도 사용할 수 있다.

Elasticsearch

# Compare document counts (on source and target clusters)
GET customer/_count

# Details: for health/segment/storage comparison
GET _cat/indices/customer?v&h=index,docs.count,store.size
# Content by sampling: compare GET customer/_doc/<id> for the same _ids

Elasticsearch의 기본 검증은 GET <index>/_count로 두 클러스터의 문서 수를 비교하는 것이다(_cat/indices docs.count도 빠른 요약을 제공). 비교 기준은 _count로 한다. _cat/indices docs.count는 Lucene 수준에서 집계하므로(nested 하위 문서 포함, 예: nested 항목 두 개를 가진 문서 하나는 3으로 집계) _count와 다를 수 있다. 내용 검증은 같은 _id 집합에서 문서를 샘플링하거나 해시 필드로 비교한다.

DBAtlas에서 열기 →