mysql 中所选表格的分组大小

Grouping size of selected tables in mysql

我正在尝试获取符合我的标准的所有 table 的总大小(其中 table 排序规则不是拉丁文的拉丁文列,带有 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)

如果你改变你的样子会怎么样

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;

您需要加入一个子查询,每个 table 一行仅 returns 匹配列约束。

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;