42

I would like to know how many rows are in each table in my database. I've come so far as to having

select count(*) _tablename_; 

However i would need to do that on each and every table - and there are a lot. What would me the best way to get a print-out with the table name and it's row count?

3 Answers 3

117
SELECT table_name, table_rows
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA = '<your db>';

I also hope you realise there's an error in your query: it's missing a FROM.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a bunch. I've got the part that it's better to get the list from metadata!
Oops... I tried this query but it gives unstable results (each time different values)!
Yes, the inaccuracy of InnoDB row counts is a by-product of that engine as mentioned here: dev.mysql.com/doc/refman/5.5/en/innodb-restrictions.html
Specifically: "InnoDB does not keep an internal count of rows in a table because concurrent transactions might “see” different numbers of rows at the same time. [...] If an approximate row count is sufficient, SHOW TABLE STATUS can be used."
you can also improve this a bit by using format(table_rows) and getting some ,'s in there so you can read the row count easier.
|
4

This following query will return number of rows in each table but it doesn't seem to return exact value all the time

SELECT table_name, table_rows
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '<your db>';

TABLE_ROWS: The number of table rows in the partition. For partitioned InnoDB tables, the row count given in the TABLE_ROWS column is only an estimated value used in SQL optimization, and may not always be exact... for more https://dev.mysql.com/doc/mysql-infoschema-excerpt/5.5/en/partitions-table.html

1 Comment

how it is different from the above answer?
1

In addition to SQL queries by others, one can also use Workbench GUI to get the row-counts of each table. To do this, Launch Workbench -> Connect to Db -> right click Db and select "Schema Inspector" (as in Screenshot below - I have highlighted the "Rows" column):

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.