返回表的列表并在单个查询中计数

时间:2017-08-11 10:08:33

标签: sql vertica vsql

我知道describe命令\dselect count(*) from my_schema_1.my_table_1;。但是我想得到整个数据库的整洁列表,我有很多表。像下面这样的东西会很好。

my_schema_1   | mytable_1   | 12323
my_schema_2   | mytable_2   | 0

我基本上想要遍历所有表格。

1 个答案:

答案 0 :(得分:2)

可能是这样的(不需要为每个表执行COUNT(*)):

编辑新版本,以考虑没有投影的表:

collapse_key

示例输出:

SELECT
    t.table_schema AS schema,
    t.table_name AS table,
    ZEROIFNULL(
        CASE WHEN p.is_segmented IS TRUE 
            THEN SUM(ps.row_count) * COUNT(DISTINCT ps.node_name) // COUNT(ps.node_name) 
            ELSE MAX(ps.row_count)
        END
    ) AS row_count,
    CASE WHEN p.is_segmented THEN 'Yes' ELSE 'No' END AS segmented,
    COUNT(DISTINCT p.projection_id) AS num_proj
FROM
    v_catalog.tables t
    LEFT OUTER JOIN v_monitor.projection_storage ps
        ON t.table_id = ps.anchor_table_id 
    LEFT OUTER JOIN v_catalog.projections p
        ON t.table_id = p.anchor_table_id
        AND p.is_super_projection IS TRUE
GROUP BY
    t.table_schema, t.table_name, p.is_segmented
ORDER BY
    t.table_schema, t.table_name
;

我确实添加了几列: schema | table | row_count | segmented | num_proj --------+------------------------+-----------+-----------+---------- mauro | city | 5 | Yes | 2 mauro | employees | 1000000 | Yes | 2 mauro | empty | 0 | No | 0 mauro | fnames | 20 | Yes | 2 ... tpch | customer | 0 | Yes | 2 tpch | lineitem | 54010935 | Yes | 2 tpch | nation | 25 | No | 1 tpch | orders | 718277000 | Yes | 2 (是/否)和segmented。如果需要,可以删除它们。