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 包:它通过数据库链接逐行比较源表和目标表,可以报告/收敛差异。校验和方面,可用 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 中打开 →