[Resolved] How to efficiently get multiple columns from a Row object?

Please ignore the question below, I should have done more testing before asking!

What I’m trying to do:

I have a Row object, from a link to that row in another data table. I want to get multiple columns from this row without having to make multiple round trips to the database.

What I’ve tried and what’s not working:

myRowReference = usersRow['linkToUserDataTable']
dataItem1 = myRowReference['item1']
dataItem2 = myRowReference['item2']
dataItem3 = myRowReference['item3']
dataItem4  = myRowReference['item4']
dataItem5  = myRowReference['item5']

This works fine but is slow. In practice I actually need up to 20 columns, which is becoming wildly inefficient. It feels like there should be a way to put those last five lines on a single line and cause only one round trip to the database.

[I know that if I had obtained the Row object via a search I could have used q.fetch_only, per:

myRowReference = app_tables.user_data.get(emailAddress=emailAddress, q.fetch_only(item1, item2, item3, item4, item5)

This would fetch all the columns at once, but involves a search of the data table which can also sometimes be slow. It also seems perverse that, when I already have the row reference, it should be quicker to simply find it again via a search because it allows me to use q.fetch_only().]

I would have thought myRowReference received a copy of the actual row, so no further round trips would be necessary – unless those columns are all media or SimpleObject type.

Have you measured (got timestamps between each usage), to see whether these are indeed multiple round trips?

Edit: this would be automatic with this FR:

2 Likes

Well this is embarrassing. You are right - it is extremely quick. Sorry!

I think I perhaps misunderstood the other recent discussion covering linked rows where we discussed the fact that when you get a Users row via get_user(), any linked rows arrive “empty”, and data has to be fetched.

Maybe that’s incorrect, or maybe in this case the link is fully populated because I fetched the linked row specifically in myRowReference = usersRow['linkToUserDataTable'].

The Accelerated Tables option changes cacheing behavior. Unfortunately, it’s easy to forget to mention that it’s turned on – or off – for a given Forum post. Especially when it’s a set-it-and-forget-it feature.

Edit: And please don’t feel embarrassed! Everybody (well, nearly everyone) gets surprised when they find out where the time is actually going – in all kinds of programs. Our intuition often works for simple systems, but with something of many layers, like Anvil, with its own internal optimizations, on top of databases, with their own optimizations, intuition just doesn’t cut it anymore.

1 Like