Skip to content

Instantly share code, notes, and snippets.

@CHERTS
Created March 27, 2024 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CHERTS/c1cc36b119bb471aa6e45e5896310d37 to your computer and use it in GitHub Desktop.
Save CHERTS/c1cc36b119bb471aa6e45e5896310d37 to your computer and use it in GitHub Desktop.
Getting the number of records in all tables in MySQL (use count)
SET GROUP_CONCAT_MAX_LEN=131072;
SET @selects = NULL;
SELECT CONCAT('SELECT * FROM (\n', GROUP_CONCAT(single_select SEPARATOR ' UNION\n'), '\n ) Q ORDER BY Q.TABLE_ROWS DESC') AS sql_query
FROM (
SELECT CONCAT(
'SELECT "',
table_name,
'" AS TABLE_NAME, COUNT(1) AS TABLE_ROWS
FROM `',
table_schema,
'`.`',
table_name,
'`'
) AS single_select
FROM information_schema.tables
WHERE table_schema = DATABASE()
AND ENGINE = 'InnoDB'
AND table_type = 'BASE TABLE'
) Q INTO @selects;
PREPARE stmt FROM @selects;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment