Very slow database performance

Where should you concentrate? It starts with your numbers.

By far, the largest part of the time lag seems to lie between “client function in” and “server function in”. Therefore, this is your biggest opportunity for time savings. You’d gain more by cutting this in half than by cutting all other lags by 100%. From what you’ve written, it looks like a good spot to concentrate on.

Because that lag is part of every single server call you make, you stand to gain on every call, not just the ones you’ve shown.

You’ve already shown that you can reduce that initial lag from around 1.8 seconds to around 1.5, or a saving of roughly 16%, with what sounds like fairly little effort. This is very promising, and suggests that more reduction may be available in this area.

Where this lag comes from: It’s not so much the total number of modules, as the total number of distinct import statements. import results are cached, so even if there are 40 import X statements, the first one executed does most of the work.

Specifically, import statements that bring in code that is not part of your server-side code. All of your server-side modules will be loaded anyway. It’s the other import statements that might potentially be postponed – in some cases, indefinitely.

Other server-side time-eaters are certainly possible. For example, setting up a global data structure, to cache frequently-read data. In a desktop-based program, this is often a good idea, because such an object will cost its construction time only once, and thereafter it gives fast data access throughout the life of the program. Thus, there’s a net savings in time.

Folks may do this in Anvil server-side code, under the assumption that the same advantage applies. And it can, if you’re paying to keep the server-side program running between calls. If not, then that server-side “program” keeps being closed and re-started. That initial construction cost, then, is paid over and over again, at each restart.

If you’ve got such a thing going on, then that may be another opportunity for savings. Anvil does offer a per-connection data cache, that does persist between calls. See Storing session data.

1 Like