How do I make my app load faster?

Does nesting column panels add to load time? It seems that since I’ve started nesting column panels on my app, that it has doubled or tripled the load time (from 4 seconds to 7-12 seconds) . I’m trying to figure out if it’s that or something else. The other thing seems to be starting the app with column panels not visible may be another culprit?

Hi Chris,

I’ve moved this to a fresh topic. Nested containers are quick to load - they won’t be causing that kind of delay.

The slowest part of any web app is usually retrieving data from the server (eg anvil.server.call() calls, or fetching things from tables). My guess is that you’re doing a lot of these - is that part of it?

I can’t give detailed suggestions without a bit more information on what your app is doing, but a common trick is to batch loads from the server - ie, have one server function that loads lots of things at once, and call it once, rather than making dozens of calls from the client. Is there an obvious place you could do this?

Meredydd, Chris,

That’s a great suggestion about the single server call. I have a form that needs data from 12 lookup tables (hence, 12 queries) which are used to populate 12 dropdowns. At first, I was making 12 server calls, one for each table. I found this to be too slow. I then refactored the code to make 1 server call and return 12 table objects, and it was WAY faster.

1 Like

I bet too many server calls is what my problem is. I’m probably at 4 or 5 server calls. I’ll see if I can reconfigure it.

4 or 5 server calls shouldn’t cause a problem. The call itself usually costs about 0.1-0.2 seconds round-trip, so even 5 of them probably won’t take more than a second. If you’re seeing 10-second loading times, it’s probably a question of what you’re doing in those calls.

Here’s a tip: put print statements into your code, throughout its loading. Then run your app and see how long they take to appear on the Output window, so you can see what’s taking so much time.

You can even measure the elapsed time yourself with the time module:

start_time = time.time()
print "Calling foo..." 
anvil.server.call("foo")
print "Calling bar at %s" % (time.time() - start_time)
anvil.server.call("bar")
print "Calling baz at %s" % (time.time() - start_time)
anvil.server.call("baz")
print "Done at %s" % (time.time() - start_time)

If it prints something like this in the output window:

Calling foo...
Calling bar at 0.33
Calling baz at 5.48
Done at 6.02

Then you know it’s the call to bar that’s causing you problems. This can help you isolate the part of the code that’s causing these loading delays.

2 Likes

I found the major culprit to my loading time woes and want to share in case someone else is having trouble. In my case, I have a moderately-sized Postgres table (IMO) of ~25,000 rows and around 20 columns. I update one column frequently, every time the app loads or the user clicks a button. Otherwise, it’s all SELECT statements. But the loading times have grown from under 4 seconds at first, to over 25 seconds!

An Engineer at ElephantSQL took a look at my DB and suggested I VACUUM FULL the database (for more info on VACUUM FULL, click here: https://www.postgresql.org/docs/9.1/static/sql-vacuum.html). When he did it, my app load time dropped back to 3.5 seconds instantly. He then sent me this link on setting your auto vacuum and analyze settings: https://lob.com/blog/supercharge-your-postgresql-performance.

Essentially, when you update or delete tuples, the DB won’t actually delete them. In my case, my 15MB table had ballooned to 1.1GB over the last few months. You have to run a VACUUM and ANALYZE to fix it. Changing the auto vacuum settings on my DB now…

1 Like

This analysis method was what helped me pinpoint that a query to the DB was taking way longer than it should.

Do you know how long the actual query takes to run?
Even a couple of seconds on a 25k db is rather long.

Sorry, i didn’t read that properly - sounds like you might have sorted it already!

I think I’m good now. 0.53099 seconds! I just make a number of sql calls and calls to data tables that add up to 4-5 seconds total.

2 Likes

is this possible to do from anvil?

Hello,

Could you please be more specific about what you are trying to do with Anvil?

You can quote other posts to make it clear what you are referring to (I have done that above as an example).

Are you asking how to make apps load faster in general, or are you asking about how to make fewer server calls to improve performance?

Please clarify and someone will probably be able to provide better assistance.

sorry my bad… i was referring to the Vacuum instruction to a postgres database. i believe the built in DB used by anvil is postgres

I personally don’t think this is possible with the standard Anvil DataTables API as I see no reference to VACUUM in the documentation. See here for using external databases.

1 Like

Hi there,

Thanks @alcampopiano, that’s right. In particular, you don’t need to do this in Anvil - we take care of all of that for you, including things like vacuuming.

1 Like