I seem to have been ably to shave of about ~1 second from all queries, by only fetching the data I actually need with fetch_only()
:
@anvil.server.callable('filter_by_country_or_status', require_user=True)
def filter_by_country_or_status(filter_tuple: tuple=None):
result = None
tl = TimerLogger('Server timer')
tl.start('Server start')
if not any(filter_tuple):
try:
filter_tuple = load_filters()
except:
return result
else:
store_filters(filter_tuple)
tl.check('Server cookie check done')
if filter_tuple[0] and filter_tuple[1]:
customers = app_tables.customer.search(
q.fetch_only('name', 'status'),
status=filter_tuple[1]
)
tl.check('Server: Customers fetched.')
result = app_tables.location.search(
q.fetch_only('customer', 'address', 'country'),
country=filter_tuple[0],
customer=q.any_of(*customers)
)
tl.check('Server: Locations fetched.')
elif not filter_tuple[0] and filter_tuple[1]:
# Status chosen
customers = app_tables.customer.search(
q.fetch_only('name', 'status'),
status=filter_tuple[1]
)
result = app_tables.location.search(customer=q.any_of(*customers))
elif filter_tuple[0] and not filter_tuple[1]:
# Country chosen
result = app_tables.location.search(
q.fetch_only('customer', 'address', 'country'),
country=filter_tuple[0]
)
else:
result = app_tables.location.search()
tl.end('Server end')
return result