在mysql中对所选表的大小进行分组

时间:2016-09-23 21:38:48

标签: mysql information-schema

我试图获取符合我标准的所有表格的总大小(表格整理不是拉丁语的latin列,包含text或varchar列),但是我得到的大小不是任何接近实际尺寸的地方。

mysql> select t.table_schema,sum(data_length + index_length)/1024/1024/1024 
    from tables t 
    inner join columns c on t.table_schema=c.table_schema and t.table_name=c.table_name 
    where t.table_schema in ('db1','db2') 
        and  (c.collation_name like '%latin%' or c.character_set_name like '%latin%') 
        and  (c.column_type like 'varchar%' or c.column_type like 'text') 
        and  t.table_collation not like '%latin%' 
    group by t.table_schema;
+--------------+------------------------------------------------+
| table_schema | sum(data_length + index_length)/1024/1024/1024 |
+--------------+------------------------------------------------+
| db1       |                                  233.021102905273 |
| db2        |                                  93.742004394531 |
+--------------+------------------------------------------------+
2 rows in set (0.54 sec)

2 个答案:

答案 0 :(得分:1)

您需要连接一个子查询,该子查询每个表只返回一行,与列约束匹配。

select t.table_schema,sum(data_length + index_length)/1024/1024/1024 
from tables t 
inner join (
    SELECT DISTINCT table_schema, table_name
    FROM columns  
    WHERE (collation_name like '%latin%' or character_set_name like '%latin%') 
    and (column_type like 'varchar%' or column_type like 'text') 
) c on t.table_schema=c.table_schema and t.table_name=c.table_name
where t.table_schema in ('db1','db2') 
    and  t.table_collation not like '%latin%' 
group by t.table_schema;

答案 1 :(得分:0)

如果您将自己更改为

,该怎么办?
select t.table_schema,
sum(data_length) + sum(index_length)/1024/1024/1024 
from tables t 
inner join columns c 
on t.table_schema=c.table_schema 
and t.table_name=c.table_name 
where t.table_schema in ('db1','db2') 
and  (c.collation_name like '%latin%' or c.character_set_name like '%latin%') 
and  (c.column_type like 'varchar%' or c.column_type like 'text%') 
and  t.table_collation not like '%latin%' 
group by t.table_schema;