使用目录查询的行数错误

时间:2014-10-02 11:04:54

标签: amazon-redshift

我在下一页上收到了这个查询:

http://docs.aws.amazon.com/redshift/latest/dg/c_join_PG_examples.html

它列出了表名和行数。但行数似乎是错误的(是实际的10倍)

select datname, nspname, relname, sum(rows) as rows
from pg_class, pg_namespace, pg_database, stv_tbl_perm
where pg_namespace.oid = relnamespace
and pg_class.oid = stv_tbl_perm.id
and pg_database.oid = stv_tbl_perm.db_id
and datname ='tickit'
group by datname, nspname, relname
order by datname, nspname, relname;

总和(行)是否正确?

1 个答案:

答案 0 :(得分:2)

在这里,试试我的。它还包括数据大小。 :-D

/* 
   Ordered view of table sizes.
*/
CREATE VIEW my_schema.vw_table_summary
AS
SELECT db_name
      ,schema_name
      ,table_name
      ,TO_CHAR(rows_total,'999,999,999,999')  rows_total
      ,TO_CHAR(rows_sorted,'999,999,999,999') rows_sorted
      ,CASE WHEN rows_total > 0 
            THEN ROUND((CONVERT(NUMERIC,rows_sorted)/CONVERT(NUMERIC,rows_total))*100,2) 
       ELSE 0.00 END percent_sorted
      ,COALESCE(size_in_gb,0) size_in_gb
FROM (SELECT id        table_id
            ,datname   db_name
            ,nspname   schema_name
            ,relname   table_name
            ,SUM(rows) rows_total
            ,SUM(sorted_rows) rows_sorted
        FROM stv_tbl_perm
        JOIN pg_class
          ON pg_class.oid = stv_tbl_perm.id
        JOIN pg_namespace
          ON pg_namespace.oid = relnamespace
        JOIN pg_database
          ON pg_database.oid = stv_tbl_perm.db_id
       WHERE name NOT LIKE 'pg_%'
         AND name NOT LIKE 'stl_%'
         AND name NOT LIKE 'stp_%'
         AND name NOT LIKE 'padb_%'
         AND nspname <> 'pg_catalog'
       GROUP BY id, datname, nspname, relname
       ORDER BY id, datname, nspname, relname) tbl_det
LEFT
JOIN (SELECT tbl table_id
            ,ROUND(CONVERT(REAL,COUNT(*))/1024,2) size_in_gb
        FROM stv_blocklist bloc
       GROUP BY tbl) tbl_size
 ON tbl_size.table_id = tbl_det.table_id
 ORDER BY db_name
         ,schema_name
         ,table_name
;