Recently, large tables need to be managed at work, so the first step is to count the data volume of each table in MySQL. Surely you can't do it for every table SELECT count(*) FROM table_name
, the efficiency is too low. How to use one SQL statement to query the data volume of all tables?
-
First query the database information:
SHOW DATABASES;
-
Then you can see that all libraries are included in the returned result, we can choose
information_schema
:USE information_schema;
-
Execute the following statement:
SELECT table_name, table_rows FROM tables ORDER BY table_rows DESC
-
The amount of data in each table can be counted.
-
The required
SELECT
columns andWHERE
conditions can be dynamically adjusted~
renew
Overturned, if it is an InnoDB storage engine, you should not use this method. For details, see the different values of count(*) in MySQL and table_rows in information_schema.tables