Created
March 27, 2024 13:26
-
-
Save CHERTS/c1cc36b119bb471aa6e45e5896310d37 to your computer and use it in GitHub Desktop.
Getting the number of records in all tables in MySQL (use count)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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