Slow calculations

I have some fairly simple calculations on a page using server connect data. For some reason a few are taking around 10 secs to load showing “Invalid Number” until they do…

https://portal.rare.org/data-management/savings_clubs_stats/sc_totals.php

Here’s an example of one of the calculations…

dmx-text="((sc_bra.data.query_bra.total_savings.toNumber() * 0.19) + (sc_hnd.data.query_hnd.total_savings.toNumber() * 0.0419) + (sc_idn.data.query_idn.total_savings.toNumber() * 0.0000689) + (sc_moz.data.query_moz.total_savings.toNumber() * 0.0157) + (sc_phl.data.query_phl.total_savings.toNumber() * 0.02)).formatNumber(0, '.', ',')"

It’s basically pulling totals from the same dataset but multiplying by different values based on currency exchange rates, and then adding them together to get the total in US$. The MySQL View which generates the data only takes 2 secs to load so it looks like it’s the actual calculations.

Any help appreciated as always.

Maybe try using data views instead of server connects?

1 Like

Hi Heather, thanks for the suggestion - I’ll give that a go.

To save some of the client side work maybe consider doing the calculation inside the server action inside a set value step.
Each server action could do its own multiplication first and then you would only need the client side to do the additions and number formatting.

2 Likes

I ended up doing something similar and just did all the calculations in mysql - loads much faster.

Thank you for the suggestion though.

4 Likes

Yes, where to do calculations on large amounts of data is an interesting one.

I always start in the database, and if that is really slow then I collect all the data with a MySQL view then do some of the maths in a Server Action.

I never do it on the client side as that is definitely the slowest place!

3 Likes

When working with large datasets and you want to calculate for example the sum or average it is best to do that directly with the database query. Formatting data can be done on the client. Small calculations can also be done on the client. We are working on flows for the client-side that will work in web workers, that can be used for heavy calculations without it freezing your site, it doesn’t mean calculations will be faster and when possible you should do calculations on large database sets directly in the query.

3 Likes

Sounds cool. Looking forward to it.
I am always skeptical adding even uppercase formatter to ensure best performance. :sweat_smile:

1 Like