Batch select with SQLAlchemy

Posted by muckabout on Stack Overflow See other posts from Stack Overflow or by muckabout
Published on 2010-03-13T14:36:17Z Indexed on 2010/03/13 14:45 UTC
Read the original article Hit count: 707

Filed under:
|

I have a large set of values V, some of which are likely to exist in a table T. I would like to insert into the table those which are not yet inserted. So far I have the code:

for value in values:
        s = self.conn.execute(mytable.__table__.select(mytable.value == value)).first()      
        if not s:
            to_insert.append(value)

I feel like this is running slower than it should. I have a few related questions:

  1. Is there a way to construct a select statement such that you provide a list (in this case, 'values') to which sqlalchemy responds with records which match that list?
  2. Is this code overly expensive in constructing select objects? Is there a way to construct a single select statement, then parameterize at execution time?

© Stack Overflow or respective owner

Related posts about python

Related posts about sqlalchemy