简体   繁体   English

查询所有数据库的num_rows,大小,num_tables,default_charset,default_collat​​ion

[英]Query num_rows, size, num_tables, default_charset, default_collation for all databases

Right now I've been able to come up with this: 现在,我已经能够提出以下建议:

SELECT
    table_schema AS 'name_db', 
    sum(table_rows) AS 'num_rows_db', 
    SUM(data_length + index_length) / 1024 / 1024 AS 'size_db(MB)', 
    count(*) as 'num_tables_db' 
FROM
    information_schema.TABLES
GROUP BY table_schema;

And now I'm trying to figure out how to add the charset and collation for all the databases but I'm not sure how to do it. 现在,我试图弄清楚如何为所有数据库添加字符集和排序规则,但是我不确定该怎么做。

I've figured out information_schema.SCHEMATA table has DEFAULT_CHARACTER_SET_NAME and DEFAULT_COLLATION_NAME columns and I'd like to add those to my above query. 我已经找出了information_schema.SCHEMATA表具有DEFAULT_CHARACTER_SET_NAMEDEFAULT_COLLATION_NAME列,我想将它们添加到上面的查询中。 Also, I'd like to know whether there is any easy way to display the db size in Kb or MB depending the size (ie: like heidiSQL summary does). 另外,我想知道是否有任何简单的方法可以根据大小显示以Kb或MB为单位的数据库大小(即:像heidiSQL摘要一样)。

Guess I need to make some sort of join between these 2 tables using SCHEMA_NAME but the group by of my first query confuses me :/ 猜猜我需要使用SCHEMA_NAME在这2个表之间进行某种SCHEMA_NAME但是第一个查询的分组依据使我感到困惑:/

It turns out it is best to do the aggregates and GROUP BY separately for each of the two tables needed. 事实证明,最好为所需的两个表分别进行汇总和GROUP BY

SELECT  *
    FROM  
    (
        SELECT  table_schema AS 'name_db',
                sum(table_rows) AS 'num_rows_db',
                IF(SUM(data_length + index_length) > 1048576,
                    CONCAT(ROUND(SUM(data_length + index_length) / 1024 / 1024), ' MB'),
                    CONCAT(ROUND(SUM(data_length + index_length) / 1024), ' KB') ) AS 'size_db',
                count(*) as 'num_tables_db'
            FROM  information_schema.TABLES AS t
            GROUP BY  TABLE_SCHEMA 
    ) AS t
    JOIN  
    (
        SELECT  table_schema AS 'name_db',
                GROUP_CONCAT(DISTINCT CHARACTER_SET_NAME) AS charsets,
                GROUP_CONCAT(DISTINCT COLLATION_NAME) AS collations
            FROM  information_schema.COLUMNS
            GROUP BY  TABLE_SCHEMA
    ) AS c USING (name_db);

(There is some potential for users to be confused by table_schema vs name_db . Since one is the alias for the other, the two are interchangeable in some cases.) table_schemaname_db可能table_schema用户感到困惑。由于一个是另一个的别名,因此在某些情况下两者是可以互换的。)

You may want to tack ORDER BY name_db at the end. 您可能需要在末尾ORDER BY name_db You may want to say `WHERE TABLE_SCHEMA NOT IN ('mysql', 'information_schema', 'performance_schema') to avoid the system tables. 您可能想说“ WHERE TABLE_SCHEMA NOT IN”(“ mysql”,“ information_schema”,“ performance_schema”)以避免系统表。

Note that table_rows is only approximate in the case of InnoDB. 请注意, table_rows仅在InnoDB情况下为近似值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM