直接查information_schema.tables表是最常用高效方式,但table_rows为估算值,InnoDB误差达±40%~50%,MyISAM较准;精确统计需用COUNT(*),但大表慎用。
直接查 information_schema.tables 表是最常用、最高效的方式,但要注意 table_rows 是估算值,尤其对 InnoDB 表可能有较大偏差。
适用于日常巡检或粗略评估:
SELECT table_name, table_rows FROM information_schema.tables WHERE table_schema = 'your_db' AND table_name = 'your_table';
table_rows 字段返回的是优化器统计值,非实时精确值便于识别大表、定位数据分布:
SELECT table_name, table_rows, TRUNCATE(data_length/1024/1024,2) AS data_mb, TRUNCATE(index_length/1024/1024,2) AS idx_mb FROM information_schema.tables WHERE table_schema = 'your_db' AND table_type = 'BASE TABLE' ORDER BY table_rows DESC;
AND table_type = 'BASE TABLE' 可排除视图干扰data_mb 和 idx_mb 分别反映数据和索引占用的磁盘空间(单位 MB)适合做容量规划或迁移前评估:
SELECT SUM(table_rows) AS total_rows FROM information_schema.tables WHERE table_schema = 'your_db' AND table_type = 'BASE TABLE';
table_rows 的加总,仍属估算,不等于 SELECT COUNT(*) FROM ... 累加COUNT(*),但大表会锁表、耗时长,慎用仅在审计、校验等强一致性场景下才推荐:
SELECT COUNT(*) FROM your_table;
COUNT(*),并把结果存入临时表或导出isdel),记得加条件过滤,例如 COUNT(*) WHERE isdel = '0'