获取MySQL数据库中所有表的记录数
有没有一种方法可以获取 MySQL 数据库中所有表的行数,而无需在每个表上运行 SELECT count() ?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
你可以试试这个。 它对我来说工作得很好。
You can try this. It is working fine for me.
以下查询生成一个(另一个)查询,该查询将从 information_schema.tables 中列出的每个模式中获取每个表的 count(*) 值。 此处显示的查询的整个结果 - 所有行组合在一起 - 包含以分号结尾的有效 SQL 语句 - 没有悬空的“联合”。 通过在下面的查询中使用联合来避免悬空联合。
The following query produces a(nother) query that will get the value of count(*) for every table, from every schema, listed in information_schema.tables. The entire result of the query shown here - all rows taken together - comprise a valid SQL statement ending in a semicolon - no dangling 'union'. The dangling union is avoided by use of a union in the query below.
这就是我为获取实际计数所做的事情(不使用模式),
它速度较慢但更准确。
是一个两步过程
获取数据库的表列表 。 您可以使用
获取它
创建表列表并将其分配给此 bash 脚本中的数组变量来获取它(用单个空格分隔,就像下面的代码一样)
运行它:
This is what I do to get the actual count (no using the schema)
It's slower but more accurate.
It's a two step process at
Get list of tables for your db. You can get it using
Create and assign the list of tables to the array variable in this bash script (separated by a single space just like in the code below)
Run it:
和许多其他人一样,我很难使用 InnoDB 在
INFORMATION_SCHEMA
表上获得准确的值,并且能够从能够进行依赖于count()
的查询中获得无限的好处,并且希望在一个查询中完成。首先,请确保启用大规模 group_concats:
然后运行此查询以获得将为数据库运行的查询结果。
这将生成诸如...的输出,
这又给出了以下结果:
Like many others, I have difficulty getting an accurate value on the
INFORMATION_SCHEMA
tables with InnoDB, and would infinitely benefit from being able to make a query that depends oncount()
, and, hopefully, do it in one, single query.First, make sure to enable massive group_concats:
Then run this query to get the resultant query you'll run for your database.
This will generate output such as...
This in turn gave the following results:
大多数其他答案建议使用
INFORMATION_SCHEMA.TABLES
,但在 MySQL 8 中它不再存在。 行数已移至INFORMATION_SCHEMA.INNODB_TABLESTATS
。您可以通过以下方式查询:
请注意,它仍然是像以前一样的近似值,而不是精确的计数。
Most other answers suggest using
INFORMATION_SCHEMA.TABLES
, but in MySQL 8 it no longer exists. Rows count has been moved toINFORMATION_SCHEMA.INNODB_TABLESTATS
.You can query it with:
Note that it's still an approximation like before, not an exact count.
这是我使用 PHP 计算表和所有记录的方法:
This is how I count TABLES and ALL RECORDS using PHP:
海报想要行数而不计数,但没有指定哪个表引擎。 对于InnoDB,我只知道一种方法,那就是计数。
这就是我挑选土豆的方式:
我对此没有做出任何断言,只是这是一种非常丑陋但有效的方法,可以获取数据库中每个表中存在多少行,无论表引擎如何,并且无需安装权限存储过程,并且无需安装 ruby 或 php。 是的,它生锈了。 是的,这很重要。 count(*) 是准确的。
Poster wanted row counts without counting, but didn't specify which table engine. With InnoDB, I only know one way, which is to count.
This is how I pick my potatoes:
I am making no assertions about this other than that this is a really ugly but effective way to get how many rows exist in each table in the database regardless of table engine and without having to have permission to install stored procedures, and without needing to install ruby or php. Yes, its rusty. Yes it counts. count(*) is accurate.
基于@Nathan 上面的答案,但不需要“删除最终的联合”并且可以选择对输出进行排序,我使用以下 SQL。 它生成另一个 SQL 语句,然后运行:
您确实需要足够大的
group_concat_max_len
服务器变量值,但从 MariaDb 10.2.4 开始,它应该默认为 1M。Based on @Nathan's answer above, but without needing to "remove the final union" and with the option to sort the output, I use the following SQL. It generates another SQL statement which then just run:
You do need a sufficiently large value of
group_concat_max_len
server variable but from MariaDb 10.2.4 it should default to 1M.我不知道为什么这会如此困难,但这就是生活。
这是我执行实际计数的 bash 脚本。 只需将其保存为(例如 count_rows.sh ),使其可执行(例如 chmod 755 count_rows.sh ),然后运行它(例如 ./count_rows.sh )
I don't know why this has to be so hard but that's life.
Here's my bash script that performs actual counts. Just save this as (e.g. count_rows.sh ), make it executable (e.g. chmod 755 count_rows.sh ), and run it (e.g. ./count_rows.sh )
这是我的建议,很干净
This is my proposal, pretty clean
如果您想要确切的数字,请使用以下 ruby 脚本。 您需要 Ruby 和 RubyGems。
安装以下 Gems:
文件:count_table_records.rb
返回命令行:
输出:
If you want the exact numbers, use the following ruby script. You need Ruby and RubyGems.
Install following Gems:
File: count_table_records.rb
Go back to the command-line:
Output:
下面的代码生成所有故事的选择查询。 只需删除最后一个“UNION ALL”选择所有结果并粘贴一个新的查询窗口即可运行。
The code below generation the select query for all tales. Just delete last "UNION ALL" select all result and paste a new query window to run.
如果您知道表的数量及其名称,并假设它们都有主键,则可以将交叉联接与
COUNT(distinct [column])
结合使用来获取来自每个表的行表:这是一个 SQL Fiddle 示例。
If you know the number of tables and their names, and assuming they each have primary keys, you can use a cross join in combination with
COUNT(distinct [column])
to get the rows that come from each table:Here is an SQL Fiddle example.
文档注释: 对于 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).
您可能可以将一些内容与表格放在一起。 我从来没有这样做过,但看起来它有一列用于TABLE_ROWS,一列用于TABLE NAME。
要获取每个表的行,您可以使用如下查询:
You can probably put something together with Tables table. I've never done it, but it looks like it has a column for TABLE_ROWS and one for TABLE NAME.
To get rows per table, you can use a query like this:
像@Venkatramanan 和其他人一样,我发现 INFORMATION_SCHEMA.TABLES 不可靠(使用 InnoDB、MySQL 5.1.44),每次运行它时都会给出不同的行数,甚至在静态表上也是如此。 这是一种相对较老套(但灵活/适应性强)的生成大型 SQL 语句的方法,您可以将其粘贴到新查询中,而无需安装 Ruby gems 和其他东西。
它产生这样的输出:
复制并粘贴除了最后一个 UNION 以获得不错的输出,例如,
Like @Venkatramanan and others I found INFORMATION_SCHEMA.TABLES unreliable (using InnoDB, MySQL 5.1.44), giving different row counts each time I run it even on quiesced tables. Here's a relatively hacky (but flexible/adaptable) way of generating a big SQL statement you can paste into a new query, without installing Ruby gems and stuff.
It produces output like this:
Copy and paste except for the last UNION to get nice output like,
我只是运行:
这将为您提供每个表的行数以及一堆其他信息。
我曾经使用上面选定的答案,但这更容易。
我不确定这是否适用于所有版本,但我使用的是带有 InnoDB 引擎的 5.5。
I just run:
This will give you the row count for EVERY table plus a bunch of other info.
I used to use the selected answer above, but this is much easier.
I'm not sure if this works with all versions, but I'm using 5.5 with InnoDB engine.
简单方法:
结果示例:
Simple way:
Result example:
这就是你所需要的。
That's all you need.
此存储过程列出表、计算记录数并在最后生成记录总数。
添加此过程后运行它:
-
过程:
This stored procedure lists tables, counts records, and produces a total number of records at the end.
To run it after adding this procedure:
-
The Procedure:
对于这个估计问题有一些破解/解决方法。
Auto_Increment - 由于某种原因,如果您在表上设置了自动增量,这将为您的数据库返回更准确的行计数。
在探索为什么显示表信息与实际数据不匹配时发现了这一点。
然后,您可以轻松地使用 PHP 或其他方式返回 2 个数据列的最大值,以给出行数的“最佳估计”。
即
自动增量始终会减少 +1 *(表计数)行,但即使有 4,000 个表和 300 万行,其准确度也为 99.9%。 比估计的行数好得多。
这样做的好处是,在 Performance_schema 中返回的行计数也会被删除,因为 Maximum 不适用于空值。 但是,如果您没有具有自动增量的表,这可能是一个问题。
There's a bit of a hack/workaround to this estimate problem.
Auto_Increment - for some reason this returns a much more accurate row count for your database if you have auto increment set up on tables.
Found this when exploring why show table info did not match up with the actual data.
You could then easily use PHP or whatever to return the max of the 2 data columns to give the "best estimate" for row count.
i.e.
Auto Increment will always be +1 * (table count) rows off, but even with 4,000 tables and 3 million rows, that's 99.9% accurate. Much better than the estimated rows.
The beauty of this is that the row counts returned in performance_schema are erased for you, as well, because greatest does not work on nulls. This may be an issue if you have no tables with auto increment, though.
要查看当前正在使用的数据库的记录计数:
To see the record counts for the current DB that's in use:
还有一种选择:对于非 InnoDB,它使用 information_schema.TABLES 中的数据(因为它更快),对于 InnoDB - 选择 count(*) 来获取准确的计数。 它还忽略了视图。
如果您的数据库有很多大型 InnoDB 表,则计算所有行可能会花费更多时间。
One more option: for non InnoDB it uses data from information_schema.TABLES (as it's faster), for InnoDB - select count(*) to get the accurate count. Also it ignores views.
If your database has a lot of big InnoDB tables counting all rows can take more time.
如果您使用数据库 information_schema,则可以使用以下 mysql 代码(where 部分使查询不显示行具有空值的表):
If you use the database information_schema, you can use this mysql code (the where part makes the query not show tables that have a null value for rows):