How to get the record count of all tables in athena aws

0 votes

I'm trying to figure out how to get the total number of records across all tables (in all table schemas) in my aws Athena. The following is what I tried, but it appears that the information schema does not offer the record count. Someone please assist.

SELECT t.table_schema, t.table_name, t.table_rows
FROM   "information_schema"."schemata" s
INNER JOIN "information_schema"."tables" t on s.schema_name = t.table_schema
INNER JOIN "information_schema"."columns" c on c.table_name = t.table_name AND c.table_schema = t.table_schema
WHERE c.table_catalog = 'awsdatacatalog'

Dec 13, 2022 in AWS by Ashwini
• 5,430 points
2,823 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I would argue that it is not part of the schema information for two rather obvious reasons: first, it does not affect performance; second, in order to offer record counts, Athena, Presto, and Trino must process all data files and sources.

AFAIK The only alternative is to construct the query using SQL or another language, and then execute it because Presto/Trino does not support any type of procedural query execution (such PL/SQL paired with something allowing to run SQL from string). Let's start with this:

with tables(full_name) as(
    SELECT '"' || t.table_schema || '"."' || t.table_name || '"' as full_name
    FROM "information_schema"."tables" t
)

select array_join(array_agg('select ''' || full_name || ''' as table_name, count(*) as rows_count from ' || full_name), ' union all ')
from tables
group by true;

As an alternative, you can write a custom Athena function using lambda, which will execute the necessary SQL statement on the fly.

answered Dec 14, 2022 by Tej

edited Mar 5

Related Questions In AWS

0 votes
1 answer

how to get the list of aws services i am used in aws my account by using the lambda function

Hi@shalk, You can create a session in your ...READ MORE

answered Sep 24, 2020 in AWS by MD
• 95,460 points
2,590 views
0 votes
1 answer

How to get the contents of a folder in an AWS CodeCommit repository?

Hi@akhtar, The following get-folder example demonstrates how to get the ...READ MORE

answered Nov 25, 2020 in AWS by MD
• 95,460 points

edited Aug 4, 2023 by Khan Sarfaraz 2,974 views
0 votes
1 answer

How to delete / drop multiple tables in AWS athena?

It is not possible to run multiple ...READ MORE

answered Jan 17, 2019 in AWS by Edureka
• 4,220 points
8,518 views
0 votes
1 answer

How to get the number of pages in a pdf file present in s3?

Hi@Manav, To know about information about pdf file, ...READ MORE

answered May 3, 2020 in AWS by MD
• 95,460 points
3,418 views
0 votes
1 answer

Pivot rows into columns in AWS Athena?

You can use the following method :- It ...READ MORE

answered Oct 29, 2018 in AWS by Priyaj
• 58,020 points
8,527 views
0 votes
1 answer

AWS Redshift failed to make a valid plan when trying to run a complicated query

-The Original query was an Oracle query ...READ MORE

answered Oct 3, 2018 in AWS by Priyaj
• 58,020 points
1,355 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP