View the number of records of all tables in the database in mysql

Source: Internet
Author: User
This article will give you a brief introduction to the implementation method of viewing the number of records of all tables in the database in mysql. For more information, see.

This article will give you a brief introduction to the implementation method of viewing the number of records of all tables in the database in mysql. For more information, see.

If you use mysql 5.0 or later, you can query the tables Table in the information_schema database. This table uses table_rows to record the number of rows of the table. For example, to view the number of records of all tables in the database testdb:

The Code is as follows:

Use information_schema;

Table_name, table_rows from tables
Where TABLE_SCHEMA = 'testdb'
Order by table_rows desc;

However, for InnoDB tables, table_rows row counts are only approximate estimates.

Another method is to concatenate an SQL statement using the tables Table of the information_schema database, for example:

The Code is as follows:

Use information_schema;

Select concat (
'Select "',
TABLE_name,
'", Count (*) from ',
TABLE_SCHEMA,
'.',
TABLE_name,
'Union all'
) From tables
Where TABLE_SCHEMA = 'testdb ';

Record the number of records in all tables in mysql:

TABLE_SCHEMA: Name
TABLE_NAME: Table Name

ENGINE: The storage ENGINE used
TABLES_ROWS: number of records

DATA_LENGTH: The data size is measured in bytes, except 1024 for K and 1048576 (= 1024*1024) for M.
INDEX_LENGTH: Index size

The Code is as follows:
Use information_schema;

Select table_schema, table_name, table_rows from tables order by table_rows desc;


View the specified size:

The Code is as follows:
SELECT sum (DATA_LENGTH) + sum (INDEX_LENGTH) FROM information_schema.TABLES where

TABLE_SCHEMA = 'database name ';

The result is in bytes. Except 1024 for K, except 1048576 (= 1024*1024) for M.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.