获取 MySQL 数据库中所有表的记录数

2021-11-20 00:00:00 sql mysql rowcount

有没有办法在不对每个表运行 SELECT count() 的情况下获取 MySQL 数据库中所有表中的行数?

Is there a way to get the count of rows in all tables in a MySQL database without running a SELECT count() on each table?

推荐答案

SELECT SUM(TABLE_ROWS) 
     FROM INFORMATION_SCHEMA.TABLES 
     WHERE TABLE_SCHEMA = '{your_db}';

文档中的注释: 对于 InnoDB表,行数只是用于 SQL 优化的粗略估计.您需要使用 COUNT(*) 进行精确计数(这更昂贵).

Note from the docs though: For InnoDB tables, the row count is only a rough estimate used in SQL optimization. You'll need to use COUNT(*) for exact counts (which is more expensive).

相关文章