I have sequence of IDs I want to retrieve. It's simple:
session.query(Record).filter(Record.id.in_(seq)).all()
Is there a better way to do it?
I have sequence of IDs I want to retrieve. It's simple:
session.query(Record).filter(Record.id.in_(seq)).all()
Is there a better way to do it?
Your code is absolutety fine.
IN is like a bunch of X=Y joined with OR and is pretty fast in contemporary databases.
However, if your list of IDs is long, you could make the query a bit more efficient by passing a sub-query returning the list of IDs.
I'd recommend to take a look at the SQL it produces. You can just print str(query) to see it.
I'm not aware of an ideal way of doing it with standard SQL.