MySQL: Summarize all table row-counts in a single query

前端 未结 1 1838
深忆病人
深忆病人 2020-12-06 11:19

When reviewing a database, it\'s highly useful to get an overview of all the tables, including their row counts:

TableName    Count
t1           1234
t2              


        
相关标签:
1条回答
  • 2020-12-06 12:03

    The first example code here is a stored procedure which performs the entire process in one step, so far as the user is concerned.

    BEGIN
    
    # zgwp_tables_rowcounts
    # TableName RowCount
    # Outputs a result set listing all tables and their row counts 
    # for the current database
    
    SET SESSION group_concat_max_len = 1000000;
    
    SET @sql = NULL;
    SET @dbname = DATABASE();
    
    SELECT
      GROUP_CONCAT( 
        CONCAT (
          'SELECT ''',table_name,''' as TableName, COUNT(*) as RowCount FROM ', 
           table_name, ' '
        ) 
        SEPARATOR 'UNION '  
      ) AS Qry
    FROM
      information_schema.`TABLES` AS t
    WHERE
      t.TABLE_SCHEMA = @dbname AND
      t.TABLE_TYPE = "BASE TABLE"
    ORDER BY
      t.TABLE_NAME ASC
    
    INTO @sql
    ;
    
    PREPARE stmt FROM @sql;
    
    EXECUTE stmt;
    
    END
    

    Notes:

    • The SELECT..INTO @sql creates the necessary query, and the PREPARE... EXECUTE runs it.

    • Sets the group_concat_max_len variable in order to allow a long enough result string from GROUP_CONCAT.

    The above procedure is useful for a quick look in an admin environment like Navicat, or on the command line. However, despite returning a result set, so far as I am aware it can't be referenced in another View or Query, presumably because MySQL is unable to determine, before running it, what result sets it produces, let along what columns they have.

    So, it is still useful to be able to quickly produce, without manual editing, the separate SELECT...UNION statement that can be used as a View. That is useful if you want to join the row counts to some other per-table info from another table. Herewith another stored procedure:

    BEGIN
    
    # zgwp_tables_rowcounts_view_statement
    # Output: SelectStatement
    # Outputs a single row and column, containing a (possibly lengthy)
    # SELECT...UNION statement that, if used as a View, will output
    # TableName RowCount for all tables in the current database.
    
    SET SESSION group_concat_max_len = 1000000;
    SET @dbname = DATABASE();
    
    SELECT
      GROUP_CONCAT( 
        CONCAT (
          'SELECT ''',table_name,''' as TableName, COUNT(*) as RowCount FROM ', 
          table_name, ' ', CHAR(10)) 
        SEPARATOR 'UNION '  
      ) AS SelectStatement
    FROM
      information_schema.`TABLES` AS t
    WHERE
      t.TABLE_SCHEMA = @dbname AND
      t.TABLE_TYPE = "BASE TABLE"
    ORDER BY
      t.TABLE_NAME ASC
    ;
    END
    

    Notes

    • Very similar to the first procedure in concept. I added a linebreak (CHAR(10)) to each subsidiary "SELECT...UNION" statement, for convenience in viewing or editing the statement.

    • You could create this as a function and return the SelectStatement, if that's more convenient for your environment.

    Hope that helps.

    0 讨论(0)
提交回复
热议问题