Quantcast
Viewing all articles
Browse latest Browse all 55

Answer by HoldOffHunger for Get record counts for all tables in MySQL database

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 on count(), and, hopefully, do it in one, single query.

First, make sure to enable massive group_concats:

SET SESSION group_concat_max_len = 1000000;

Then run this query to get the resultant query you'll run for your database.

SELECT CONCAT('SELECT ', GROUP_CONCAT(table1.count SEPARATOR ',\n')) FROM (    SELECT concat('(SELECT count(id) AS \'',table_name,' Count\'','FROM ',table_name,') AS ',table_name,'_Count') AS 'count'    FROM information_schema.tables     WHERE table_schema = '**YOUR_DATABASE_HERE**') AS table1

This will generate output such as...

SELECT (SELECT count(id) AS 'table1 Count' FROM table1) AS table1_Count,   (SELECT count(id) AS 'table2 Count' FROM table2) AS table2_Count,   (SELECT count(id) AS 'table3 Count' FROM table3) AS table3_Count;

This in turn gave the following results:

*************************** 1. row ***************************table1_Count: 1table2_Count: 1table3_Count: 0

Viewing all articles
Browse latest Browse all 55

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>