I recently had a painful realisation that by default, when you get a data table row, you actually read all columns of that row even before you do anything. The pain came in when I added a column to a key data table which had large amounts of data in it, and everything slowed to a crawl.
For example, this code:
userDataRow = app_tables.user_data.get(user=user)
print(user["email_address"])
becomes much slower if you add a data-filled column to the user_data data table, even though you’re not actually looking for the bulky data.
This can be addressed using q.fetch_only, as in this helper function I made:
import anvil.tables.query as q
def getUserDataEntry(column):
userDataRow = app_tables.user_data.get(q.fetch_only(column), uid=getUid())
returnValue = userDataRow[column]
So I’ve solved the problem of accessing a single data value without needing to get the whole row.
Now what I’m stuck on is how to write a data value without accessing the whole row.
By default I would do this:
userDataRow = app_tables.user_data.get(user=user)
user["email_address"] = "test@test.com"
But again, the first line is very expensive. Any suggestions?