简体   繁体   English

获取多个表中的行数Redshift SQL?

[英]Get count of rows from multiple tables Redshift SQL?

I have a redshift database that is being updated with new tables so I can't just manually list the tables I want. 我有一个用新表更新的redshift数据库,所以我不能只手动列出我想要的表。 I want to get a count of the rows of all the tables from my query. 我想从查询中获取所有表的行数。 So far I have: 到目前为止,我有:

select 'SELECT ''' || table_name || ''' as table_name, count(*) As con ' ||
       'FROM ' || table_name || 
        CASE WHEN lead(table_name) OVER (order by table_name ) IS NOT NULL 
        THEN ' UNION ALL ' END
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%results%'

but when I do this I get the error: 但是当我这样做时,我得到了错误:

Specified types or functions (one per INFO message) not supported on Redshift tables.

I've searched a lot but I can't seem to find a solution for my problem. 我进行了很多搜索,但似乎找不到解决我问题的方法。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

EDIT: I've changed my approach to this and decided to use a for loop in R to get the row counts of each but I'm running into the issue that 'row_counts' is only saving one number, not the count of each row like I want. 编辑:我已经改变了这种方法,并决定在R中使用for循环来获取每个行的计数,但是我遇到了一个问题,即“ row_counts”仅保存一个数字,而不是每个行的计数就像我想要的。 Here is the code: 这是代码:

schema <- "x"
table_prefix <- "results"
geos <- ad_districts %>% filter(geo != "geo")
row_count <- list()
i = 1

for (geo in geos){
  table_name <- paste0(schema, ".", table_prefix, geo)
  row_count[[i]] <- dbGetQuery(con, 
                             paste("SELECT COUNT(*) FROM", table_name))
  i = i + 1 
}

[EDIT] - I think this is the root cause - some sql functions are only supported on the leader node. [编辑]-我认为这是根本原因-一些SQL函数仅在领导节点上受支持。 Try connecting to that node and re-run your SQL. 尝试连接到该节点并重新运行SQL。 https://docs.aws.amazon.com/redshift/latest/dg/c_sql-functions-leader-node.html https://docs.aws.amazon.com/redshift/latest/dg/c_sql-functions-leader-node.html

Hope this helps. 希望这可以帮助。

select 'select count(*) as "' || table_schema || '.' || table_name || '" from ' || table_schema || '.' || table_name || ' ;' as sql_text
from information_schema.tables
;

[EDIT - refined this a bit to generate a series of statements that can be run at once] [编辑-对此进行了一些改进,以生成可以立即运行的一系列语句]

select rownum, case when rownum > 1 then sql_text else replace(sql_text, 'union all', '') end as sql_text
from
(
select rank() over (order by sql_text DESC) as rownum,
        sql_text
from
(
select 'select ''' || table_schema || ' ' || table_name || ''' , count(*) as "' || table_schema || '.' || table_name || '" from ' || table_schema || '.' || table_name || ' union all ' as sql_text
from information_schema.tables
where table_schema = 'public'
order by table_schema, table_name
)X
)Y 
order by rownum desc ;

Your query is doing a select * for all tables, this will take a lot of time and resources. 您的查询对所有表都执行select *,这将花费大量时间和资源。 Instead use a system table to get the same info 而是使用系统表获取相同的信息

select name, sum(rows) as rows
from stv_tbl_perm
where name like '%results%'
group by 1
SELECT ' Select count(*) , '''+ tablename + '''  from   '+'"' + tablename +'"' +'  Union ALL  '
FROM pg_table_def
GROUP BY tablename

Above query eliminates any table name with space. 上面的查询消除了任何带有空格的表名。 Remove UNION ALL at the end of the query and query will be ready to be executed. 在查询结束时删除UNION ALL,即可开始执行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM