mysql 查看数据库中所有表的行数(2)

之前写过了,如果需要查询某个数据库中所有表的行数, 可以通过information_schema.tables来统计,但是由于mysql8.0的缓存机制,导致这个数据和count(*)的结果不一致

sql 复制代码
SELECT
	TABLE_name,
	table_rows 
FROM
	information_schema.TABLES 
WHERE
	TABLE_SCHEMA IN ( 'table_data' ) 
ORDER BY
	table_rows DESC;

table_data 为所需要查询的数据库

因此我们需要使用count(*)来统计数据,但是逐一表来统计比较麻烦,下面还有一种快捷方式

首先通过information_schema.tables 来组合语句

sql 复制代码
select concat(
    'select "', 
    TABLE_name, 
    '", count(*) from ', 
    TABLE_SCHEMA, 
    '.',
    TABLE_name,
    ' union all'
) from information_schema.tables 
where TABLE_SCHEMA in ('table_data');

将输出的结果拷贝出来,将最后一行的union all 删除,就可以运行统计了。

相关推荐
嵌入式×边缘AI:打怪升级日志5 小时前
编写 Bootloader 实现烧录功能
数据库
砚边数影6 小时前
模型持久化(二):从 KingbaseES 加载模型,实现离线预测
数据库·机器学习·kingbase·模型推理·数据库平替用金仓·金仓数据库
·云扬·7 小时前
MySQL基于位点的主从复制完整部署指南
android·mysql·adb
Ama_tor7 小时前
Navicat学习01|初步应用实践
数据库·navicat
山岚的运维笔记7 小时前
SQL Server笔记 -- 第65章:迁移 第66章:表值参数
数据库·笔记·sql·microsoft·sqlserver
番茄去哪了8 小时前
苍穹外卖day05----店铺营业状态设置
java·数据库·ide·redis·git·maven·mybatis
暮色妖娆丶9 小时前
Spring 源码分析 事务管理的实现原理(下)
数据库·spring boot·spring
暮色妖娆丶9 小时前
Spring 源码分析 事务管理的实现原理(上)
数据库·spring boot·spring
LaughingZhu10 小时前
Product Hunt 每日热榜 | 2026-02-18
大数据·数据库·人工智能·经验分享·搜索引擎
是吕先森10 小时前
mysql基础
数据库·mysql·oracle