Most efficient way to fetch all rows with ids in a id list

What I’m trying to do:
I have a list of row ids and I want to retrieve these efficiently

Currently I’m doing this:

lc_ids = [id_1, id_2, ...]
for lc_id in lc_ids:
    lc = app_tables.learning_cards.get_by_id(lc_id)

I’m guessing that with accelerated tables and other tricks this should go better, but I dont know how :S

1 Like

I don’t know of a way to do that more efficiently.

I know some folks don’t like to use the built-in row ids and instead create their own ‘id’ columns, and maybe this is one reason why. If you restructured your tables that way, you could get all the rows in a single search call to the database with an any query on that ‘id’ column.

p.s. It would be easier to read your Python code excerpt if you format it properly as shown here: How to ask a good question

1 Like

As @hugetim said, I don’t think there is a better way to get all the rows with the id matching an element of a list, unless you have a plan that allows SQL searches.

A workaround, if you can use your own column as id, would be to use:

rows = app_tables.learning_cards.search(my_column_with_id=q.any_of(list_of_ids))
1 Like

thanks! Thats the conclusion I’ve also reached, just wanted to double-check. Will read the link!

1 Like