Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get record counts for all tables in MySQL database

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?

like image 930
Mark Avatar asked Nov 13 '08 01:11

Mark


People also ask

How do you get the count of all tables in a database?

The syntax is as follows. mysql> SELECT SUM(TABLE_ROWS) ->FROM INFORMATION_SCHEMA. TABLES ->WHERE TABLE_SCHEMA = 'business'; The following table returns the count of records.

How can I get table record count in MySQL?

SELECT TABLE_NAME,SUM(TABLE_ROWS) FROM INFORMATION_SCHEMA. TABLES WHERE TABLE_SCHEMA = 'your_db' GROUP BY TABLE_NAME; That's all you need.

What is Row_count () in MySQL?

In MySQL the ROW_COUNT() function is used to return the number of rows affected by the previous SQL statement. If the previous statement was not one that could potentially change data rows or you can say, it wasn't an INSERT, UPDATE, DELETE or other such statement this function will return -1.


Video Answer


2 Answers

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

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).

like image 76
Hates_ Avatar answered Sep 17 '22 12:09

Hates_


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:

SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '**YOUR SCHEMA**'; 
like image 28
gpojd Avatar answered Sep 17 '22 12:09

gpojd