ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2021-11-08 14:31:10  阅读:978  来源: 互联网

标签:count information action cert select report day 准确


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 "report_cert_action_day", count(*) from bigdata_1.report_cert_action_day union all                                          |
| select "report_cert_action_month", count(*) from bigdata_1.report_cert_action_month union all                                      |
| select "report_cert_day", count(*) from bigdata_1.report_cert_day union all                                                        |
| select "report_cert_month", count(*) from bigdata_1.report_cert_month union all                                                    |
+------------------------------------------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)

mysql> 

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

select "report_cert_action_day", count(*) from bigdata_1.report_cert_action_day union all                                          
select "report_cert_action_month", count(*) from bigdata_1.report_cert_action_month union all
select "report_cert_day", count(*) from bigdata_1.report_cert_day union all                                                        
select "report_cert_month", count(*) from bigdata_1.report_cert_month

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

mysql> 

标签:count,information,action,cert,select,report,day,准确
来源: https://blog.csdn.net/qyq88888/article/details/121206798

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有