SQLAlchemy filter in_ operator

前端 未结 2 616
日久生厌
日久生厌 2020-12-29 06:38

I am trying to do a simple filter operation on a query in sqlalchemy, like this:

q = session.query(Genotypes).filter(Genotypes.rsid.in_(inall))
2条回答
  •  -上瘾入骨i
    2020-12-29 07:34

    If the table where you are getting your rsids from is available in the same database I'd use a subquery to pass them into your Genotypes query rather than passing the one million entries around in your Python code.

    sq = session.query(RSID_Source).subquery()
    q = session.query(Genotypes).filter(Genotypes.rsid.in_(sq))
    

    The issue is that in order to pass that list to SQLite (or any database, really), SQLAlchemy has to pass over each entry for your in clause as a variable. The SQL translates roughly to:

    -- Not valid SQLite SQL
    DECLARE @Param1 TEXT;
    SET @Param1 = ?;
    DECLARE @Param2 TEXT;
    SET @Param2 = ?;
    -- snip 999,998 more
    
    SELECT field1, field2, -- etc.
    FROM Genotypes G
    WHERE G.rsid IN (@Param1, @Param2, /* snip */)
    

提交回复
热议问题