mysql 查看某数据库中所有表的行数,information_schema.tables不准确。count(*)拼接准确。

博客介绍了在MySQL中,使用information_schema.tables统计表行数可能与实际count(*)结果不一致的情况,特别是针对InnoDB表。文章提供了一种通过拼接SQL语句的方法,快速获取数据库中所有表的准确记录数,即通过动态生成多个select count(*)语句并执行,从而得到每个表的实际行数。这种方法适用于需要快速统计大量表记录数的场景。
摘要由CSDN通过智能技术生成

mysql使用information_schema.tables统计表的行数,统计结果和count(*)的结果不一样。

select table_name,table_rows from information_schema.tables 
where TABLE_SCHEMA = 'qyqdb' 
order by table_rows desc; 

经查询:information_schema.tables 对于InnoDB表,table_rows行计数仅是大概估计值,不准确。

mysql使用select count(*) from table_name可以查询某个表的总记录数。比较准确!
想快速的知道数据库中所有表的记录数信息怎么办?

另外一种办法还是借助information_schema库的tables表,来拼接出一个条sql语句,例如:

统计qyqdb数据库下所有的表的行数,生产统计语句。
select concat(
    'select "', 
    TABLE_name, 
    '", count(*) from ', 
    TABLE_SCHEMA, 
    '.',
    TABLE_name,
    ' union all'
) from information_schema.tables 
where TABLE_SCHEMA='qyqdb';

把生成的结果手动加工一下。

举例如下:

统计bigData_1数据库下所有表的行数:

select concat(
    'select "', 
    TABLE_name, 
    '", count(*) from ', 
    TABLE_SCHEMA, 
    '.',
    TABLE_name,
    ' union all'
) from information_schema.tables 
where TABLE_SCHEMA in ('bigData_1');

结果:
+------------------------------------------------------------------------------------------------------------------------------------+
| concat(
    'select "', 
    TABLE_name, 
    '", count(*) from ', 
    TABLE_SCHEMA, 
    '.',
    TABLE_name,
    ' union all'
) |
+------------------------------------------------------------------------------------------------------------------------------------+
| select "AA_cert_action_day", count(*) from bigdata_1.AA_cert_action_day union all                                          |
| select "AA_cert_action_month", count(*) from bigdata_1.AA_cert_action_month union all                                      |
| select "AA_cert_day", count(*) from bigdata_1.AA_cert_day union all                                                        |
| select "AA_cert_month", count(*) from bigdata_1.AA_cert_month union all                                                    |
+------------------------------------------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)

mysql> 

对以上输出结果进行修改,如下:

select "AA_cert_action_day", count(*) from bigdata_1.AA_cert_action_day union all                                          
select "AA_cert_action_month", count(*) from bigdata_1.AA_cert_action_month union all
select "AA_cert_day", count(*) from bigdata_1.AA_cert_day union all                                                        
select "AA_cert_month", count(*) from bigdata_1.AA_cert_month

输出结果如下:
mysql> select "AA_cert_action_day", count(*) from bigdata_1.AA_cert_action_day union all                                          
    -> select "AA_cert_action_month", count(*) from bigdata_1.AA_cert_action_month union all
    -> select "AA_cert_day", count(*) from bigdata_1.AA_cert_day union all                                                        
    -> select "AA_cert_month", count(*) from bigdata_1.AA_cert_month
    -> ;
+--------------------------+----------+
| report_cert_action_day   | count(*) |
+--------------------------+----------+
| AA_cert_action_day   |      168 |
| AA_cert_action_month |      131 |
| AA_cert_day          |       82 |
| AA_cert_month        |       39 |
+--------------------------+----------+
4 rows in set (0.00 sec)

mysql> 

  • 13
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七七powerful

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值