如何获取 Snowflake 中模式和数据库的总字节大小

How to get the total byte size of schemas & databases in Snowflake

我喜欢 SHOW TABLES 查询 return 列出的每个 table 的行和字节,但不幸的是,SHOW SCHEMAS 查询并非如此。

获取模式列表的总字节大小的最佳方法是什么?

我通过查询 information_schema.tables:

设法做到了这一点
SELECT
  table_schema AS schema,
  SUM(bytes) AS bytes
FROM information_schema.tables
GROUP BY schema;

请注意,这仅适用于当前数据库,因为每个数据库都有自己的 information_schema。因此,这无法获取数据库列表的大小。但是,可以单独获取每个数据库的总大小:

SELECT SUM(bytes) AS bytes
FROM [database_name].information_schema.tables;

对此的一个很好的补充是使用 UDF 很好地格式化字节大小,如 nicebytes 示例 here,从而在查询中得到 nicebytes(SUM(bytes)) AS bytes


编辑: 可以合并这两个查询,以便一次获得多个数据库的结果。它们也可以每天 运行 并将结果存储到另一个 table,这提供了一个很好的方法来监控数据库和模式随时间变化的大小!

SELECT
  table_catalog AS database,
  table_schema AS schema,
  SUM(bytes) AS bytes
FROM database_1.information_schema.tables
GROUP BY database, schema
UNION ALL
SELECT
  table_catalog AS database,
  table_schema AS schema,
  SUM(bytes) AS bytes
FROM database_2.information_schema.tables
GROUP BY database, schema;
SELECT
  table_catalog AS database,
  SUM(bytes) AS bytes
FROM database_1.information_schema.tables
GROUP BY database
UNION ALL
SELECT
  table_catalog AS database,
  SUM(bytes) AS bytes
FROM database_2.information_schema.tables
GROUP BY database;

如果您有帐户管理员访问权限或已被授予访问 Account Usage Views 的权限,您可以尝试使用 TABLE_STORAGE_METRICS。

select 
TABLE_CATALOG,
TABLE_SCHEMA, 
nicebytes(sum(ACTIVE_BYTES)) ACTIVE_STORAGE, 
nicebytes(sum(TIME_TRAVEL_BYTES)) TIME_TRAVEL_STORAGE, 
nicebytes(sum(FAILSAFE_BYTES)) FAILSAFE_STORAGE
from SNOWFLAKE.ACCOUNT_USAGE.TABLE_STORAGE_METRICS
where TABLE_DROPPED IS NULL
and SCHEMA_DROPPED IS NULL
and CATALOG_DROPPED IS NULL
group by 1,2
order by 1,2,3

这也利用了 nicebytes UDF