Count the number rows in your Data Tables

I was bumping against a row count quota and I was searching for the biggest violator.

From a Jupyter Notebook, I connected to my app using the uplink, and here is a snippet of code to get the row count for all the tables in your app.

import anvil.tables as tables


t_cache = tables.AppTables.cache
total_rows = 0
for table_name, table_ref in t_cache.items():
    row_count = len(table_ref.search())
    total_rows += row_count
    print(f'{row_count:>10,}  {table_name:s}')
    
print(f'\n\nTotal Rows in App: {total_rows:,}') 

Here is the sample output:

        55  user_services
        67  user_org
        91  sprint_card
        32  content_emails
        13  users
        11  user_preferences
       832  user_responses
        96  user_notes
        82  checkpoints
        19  user_priority

Total Rows in App: 1,298

I discovered the AppTables.cache by probing various classes in Anvil packages. Which is my way of saying, this solution has not been run by the the Anvil team, so in addition to hitting the database repeatedly in a loop, there might be other unintended consequences of using stuff that is not explicitly made public. So my recommendation is to tread lightly.

However, I was puzzled as to how I was exceeding the quota, and this snippet helped me hunt down the culprit.

Cheers,
Tyler

1 Like

In case it is relevant, to count rows in a single table you can use len(app_tables.my_table.search()).

2 Likes

Very helpful, thanks @mcmasty

However, I am a bit confused, do you encounter a row count quota issue with just 1,298 rows?

LOL @Tony.Nguyen …

The snippet above is from my “primary” app, so I was super puzzled about how I could possibly exceed the quota, then I speculated quota is at the account level spanning ALL apps, not merely a single app quota. Turns out the culprit was in fact a different app, that I hadn’t touched in a few months, so was out of sight and out of mind.

FWIW: I was playing around with some portfolio analysis and was thinking about building an algorithmic trading bot … so started up a new app, and imported historical data files to experiment with security selection, etc, then I had to put the project down and forgot to purge the market data.

Great to hear that, I just learnt something new