Help: Counting DB items most efficient way WU

It’s driving me mad all day, the entire day I’ve been stuck on it. I’m trying to show a summary page of totals of contacts, files, photos etc. for files I’m using a multi uploaded plugin called Files&Multi Uploader. The author doesn’t allow the meta data to be accessed which is making it difficult.

I’m trying to avoid “Do a search” as that’ll bog down my app and be intensive on WU.

How the hell do i search 10,000 files for example to get the count without doing a search for Files:count. Irrelevant of the constraints Bubble still pulls the entire row before filtering further.

So I tried API backend but creating a file in the API backend and then another action “Current Users Photo Count / File Count / Video Count etc = Current Users (one of the above) + 1 but there’s race condition issues where if I upload 20 files, 20 files will upload but the count will be off by 4 or something. It’s driving me nuts.

I’ve managed to get it working after the API call > make a change to current user > doc count = this multi uploaders bubble files : filtered by file type contains doc or docs etc” but there’s only good for <100 files. It’ll bog my site down doing that for say 500 files.

How the hell do Google Drove and OneDrive and Dropbox count the files each user has, file types etc.

Surely one of you knows how to COUNT the data without doing a search every time, without using :filtered And ideally saving the count in the Current User for MULTI uploads?

3 Likes

This is exactly how to do it.

If you have a filing cabinet with lots of files, how do you count them? You go through them all (search), and count them as you go along.

For WU, this costs 0.2 WU. It is an ‘aggregate’ search. If you are searching 10,000 items, it will only return the count, not each item. It’s efficient.

Do a search for is the backbone of any Bubble app - it lets you interact with your data. Don’t arbitrarily restrict yourself to save a couple WU, as your workarounds will likely cost more WU, and definitely cost more in your time than it would ever cost in WU.

4 Likes

Thank you George, would you still say the same for pulling the count for Photo Files, and Doc files? (Constraint: Type = Photo) for example.

I tried to think of having a dataType per file type but it got too much and hard to pull all into one table.

I want a dashboard with the total file count, photo count and doc count etc. Every time the page loads it’s going to cost me WU. If I had 20 dashboard summaries it’ll be 20 searches per page load per user.

There’s got to be a more efficient way of doing it? Ideally adding counts to the current user. Current users doc count etc but I can’t figure a way of doing it with multi files.

I’d imagine every time you put a file in your filing cabinet (knowing you’ll need to search the count by photo or doc) you’d either write the count each time you add a file and save that number elsewhere in a notepad. I can’t imagine Do a search of files > Constraint: Type = photo > Count on every single page load is the most efficient way to do it.

I can’t help but believe that keeping counts (number) under current user is the most efficient way. It’ll be two writes to the db per file to add, or remove but the user would refresh the page constantly as it’s a home page with the counts on. It’s much quicker to do a search of the current users doc count than searching 10,000 files and sifting through to figure out which is a photo or a video or a file and then doing that every single time a page loads. That seems like such a waste on WU.

I thought about keeping a separate table for counts but again, the system would have to Do a search constraint current user and then further constraint on the type.

It must be the most efficient to save it in current user surely?

I didn’t know this, wow that’s crazy. Thanks!

Total file count = Do a search for Files:count

Have a repeating group of file types.

In each cell, Do a search for Files where type = Current cell’s Type : count

That’s the right way to do it.

Then don’t have 20 summaries, or hide some by default. Yes, if you want to display more stats then yes, it will cost more WU.

Basically I have a home page that’s like an iPhone Lock Screen. Instead of notifications it shows you summaries of each page. In the Contacts notification bubble when you pull up from the bottom of the screen it says:

“Storage Drive”
“You have 32 Files, 13 of those are photos, 1 is a video, 1 is a zip and the rest are documents”

In the second notification

“Contacts”
“You have 13 investors, 102 clients, 6 partners and 18 suppliers”

And so on.

So I need to keep the count of everything. I can’t think of the right way to store the count of every single data type across my website. I could make this notification system an RG of Counts.

If I did a search for on each of those I’d probably be touching 80 searches on one page.

Would you say that RG of counts would be the most efficient way going off above?

Have you considered Do a search:group by Type with a count aggregate? That would allow you to get the count for multiple file types with just one search and would cost 0.6 WU in total (0.3 search + 0.3 group by)

1 Like

Great idea, thank you George. I never thought about Grouped by which will be handy in situations.

Any comment to the above message? :slightly_smiling_face:

George, Now i’m paronoid that what i had assumed was performat is actually not. up until now ive been making changes to fields in the current user thing to keep track of totals. Of course there is WU consumed every time i add or delete items as these running totals are updated, would i assume it best practice to remove this complexity and simply use do a seach count instead?

I have a different opinion than @georgecollier albeit I appreciate his input and advice.

When you save totals to your current user it wouldn’t bog down your app because bubble only fetches the fields that you need on the page. If your page doesn’t want the “Total payments received” field it won’t fetch that data.

Fetching from the current user is more performant and cheaper on WU.

George said above if you have a filing cabinet you’d do a search through them all and then count them as you go along. He’s correct on the latter not the former. You wouldn’t search them all, you’d only count them as you put them in and take them out and maybe a periodic audit. If somebody asked you the count, you’d never recount them all every time which is what a search would do. You’d only ever check the count. That’s what keeping the count in the current user does and add or remove the count when you add or remove a file.

So I’m pretty sure that you keep the counts in the current user especially for very high counts or at least keep a Counts data type of just numbers so it’s very quick to search. You can keep the counts in a separate datatype with a User field and a Count field but it’s extra searches. Makes no sense.

Bubble downloads the entire current user.

2 Likes

That is not accurate…when you fetch data, such as User, bubble returns ALL fields on that data type, so the total payments received field will always be fetched and returned whenever a search of user data type is performed.

If you do not need to update the counts often it might make more sense to incur the WU costs with updating the User data type each time you need to update the count…but it likely is better to just do a search:count when needed to display the counts.

There are tradeoffs on either approach and both will incur WUs

1 Like

Just a general advise, if you cannot afford let’s say $10 a month for each user on WU costs or when you have lots and lots of data to work with, consider another solution or use an external db like Supabase.

In any Postgres db (which is what Supabase is using), searching millions of records will take less than a second and fetching a count of the rows will be faster than you can blink. All this on a $5 month hosting plan.

1 Like

Yeah, in all but some pretty rare cases.

1 Like

Ah thanks @ihsanzainal84 and @boston85719, now @georgecollier makes sense. Apologies George, I was under the assumption it didn’t fetch all date (used Chat GPT to ask that lesson learned lol).

Yes it sounds like a search on files or a search on its own count table would be the answer thank you all.

I think about the only time you’d want to store the count on the data is if you wanted to search that data by the count.

ie search users where file count is more than 10

Or in a graph

This topic was automatically closed after 70 days. New replies are no longer allowed.