/    Sign up×
Community /Pin to ProfileBookmark

Row count for entire DB

I can do this for a single table, is there a simple code for every table in a DB without having to add to the code when a new table is added?

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@sibertMar 16.2021 — https://stackoverflow.com/questions/17112102/mysql-query-to-get-the-count-of-each-table-in-a-db

Or maybe (not tested)

``<i>
</i>SELECT TABLE_SCHEMA, count(1) FROM INFORMATION_SCHEMA.TABLES
GROUP BY 1<i>
</i>
``
Copy linkTweet thisAlerts:
@NogDogMar 16.2021 — I have to confess to a curiosity why you want/need to do this. But I was also curious how easy/difficult it would be to do in PostgreSQL, and found this bit of SQL on stackoverflow that seems to work fine for me. :)
[code=sql]
select table_schema,
table_name,
(xpath('/row/cnt/text()', xml_count))[1]::text::int as row_count
from (
select table_name, table_schema,
query_to_xml(format('select count(*) as cnt from %I.%I', table_schema, table_name), false, true, '') as xml_count
from information_schema.tables
where table_schema = 'public' --<< change here for the schema you want
) t
[/code]
Copy linkTweet thisAlerts:
@kiwisauthorMar 16.2021 — @NogDog#1629341

re your curiosity: it's simply a record to see when things have been added, data entry thing.

I could do this via user tracking but it's more a high level snapshot of how much stuff has gone into the database.
Copy linkTweet thisAlerts:
@kiwisauthorMar 16.2021 — @sibert#1629339

I tried this,

``<i>
</i>SELECT TABLE_ROWS, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DB_NAME'<i>
</i>
``


But there are some tables which are out by a few, one has two rows but this code returns 1. Any clues to why this might happen?
Copy linkTweet thisAlerts:
@sibertMar 16.2021 — This may work, but you have to find a way to only display "your" tables. This query shows ALL system tables as well. Some of them are empty...

``<i>
</i>SELECT TABLE_NAME, SUM(TABLE_ROWS)
FROM INFORMATION_SCHEMA.TABLES GROUP BY 1<i>
</i>
``

https://www.db-fiddle.com/f/eaQG8H4yqY9hnQBZjzJgz/14
Copy linkTweet thisAlerts:
@kiwisauthorMar 16.2021 — @sibert#1629346

I get a bunch of rows like "COLLATION_CHARACTER_SET_APPLICABILITY" in there?
Copy linkTweet thisAlerts:
@sibertMar 16.2021 — > @kiwis80#1629347 I get a bunch of rows like "COLLATION_CHARACTER_SET_APPLICABILITY" in there?

Yes, you have to filter out "system tables" as mentioned. Google is your friend...
Copy linkTweet thisAlerts:
@NogDogMar 17.2021 — > @kiwis80#1629345 But there are some tables which are out by a few

Hmm... from [this post](https://dba.stackexchange.com/questions/151769/mysql-difference-between-using-count-and-information-schema-tables-for-coun) it sounds like you can improve the accuracy by running analyze table?

> c) using the information_schema tables, as the linked question:
[code=sql]select table_rows
from information_schema.tables
where table_schema = 'database_name'
and table_name = 'table_name' ;[/code]

Accuracy: Only an approximation. If the table is the target of frequent inserts and deletes, the result can be way off the actual count. This can be improved by running ANALYZE TABLE more often.

Efficiency: Very good, it doesn't touch the table at all.


🤷
Copy linkTweet thisAlerts:
@sibertMar 17.2021 — Close to @NogDog#1629352 , but all tables:

``<i>
</i>SELECT TABLE_NAME,TABLE_ROWS
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test'<i>
</i>
``


https://www.db-fiddle.com/f/eaQG8H4yqY9hnQBZjzJgz/17
Copy linkTweet thisAlerts:
@tyagianubhav9Mar 17.2021 — I could do this via user tracking but it's more a high level snapshot of how much stuff has gone into the database.

Copy linkTweet thisAlerts:
@kiwisauthorMar 17.2021 — @sibert#1629357

Still doesn't work correctly, I've got a users table. This shows 1 row, but I have two in there.

Here's that table format

``<i>
</i>CREATE TABLE
users (
userId int(10) NOT NULL,
userName varchar(100) COLLATE utf8_unicode_ci NOT NULL,
firstName varchar(100) COLLATE utf8_unicode_ci NOT NULL,
lastName varchar(100) COLLATE utf8_unicode_ci NOT NULL,
gender enum('M','F','U') COLLATE utf8_unicode_ci NOT NULL,
dob date NOT NULL,
email varchar(100) COLLATE utf8_unicode_ci NOT NULL,
accessLevel int(10) NOT NULL DEFAULT '0',
password varchar(255) COLLATE utf8_unicode_ci NOT NULL,
registerStamp varchar(100) COLLATE utf8_unicode_ci NOT NULL,
firstStamp varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
latestStamp varchar(100) COLLATE utf8_unicode_ci NOT NULL,
validated int(10) NOT NULL DEFAULT '0',
ACCESS_KEY varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;<i>
</i>
``
Copy linkTweet thisAlerts:
@sibertMar 18.2021 — > @kiwis80#1629380 Still doesn't work correctly, I've got a users table. This shows 1 row, but I have two in there.

Which was your query?
Copy linkTweet thisAlerts:
@NogDogMar 18.2021 — It appears that what's in the INFORMATION_SCHEMA is not constantly updated. If you need an exact, at the moment count, I'd probably create a script that uses it to get a list of tables, and then run a query on each to get the count.
Copy linkTweet thisAlerts:
@tyagianubhav9Apr 28.2021 — I'd probably create a script that uses it to get a list of tables, and then run a query on each to get the count. [teatv app](https://teatv.ltd) [sbi hrms](https://hellodear.in/sbi-hrms-onlinesbi/)
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...