2

I have a table with 4 columns (1 PK) from which I need to select 30 rows. Of these rows, two columns (col. A and B) must exists in another table (8 columns, 1 PK, 2 are A and B).

Second table is large, contains millions of records and it's enough for me to know if even a single row exists containing values of col. A and B of 1st table.

I am using the code below:

query = db.Session.query(db.Table_1).\
        filter(
            exists().where(db.Table_2.col_a == db.Table_1.col_a).\
                     where(db.Table_2.col_b == db.Table_2.col_b)
        ).limit(30).all()

This query gets me the results I desire however I'm afraid it might be a bit slow since it does not imply a limit condition to exists() function nor does it do select 1 but a select *.

exists() does not accept a .limit(1)

How can I put a limit to exists to get it not to look for whole table, hence making this query run faster?

  • I need n rows from Table_1, which 2 columns exist in a record in Table_2

Thank you

2 Answers 2

9

You can do the "select 1" thing using a more explicit form as it mentioned here, that is,

exists([1]).where(...)

However, while I've been a longtime diehard "select 1" kind of guy, I've since learned that the usage of "1" vs. "*" for performance is now a myth (more / more).

exists() is also a wrapper around select(), so you can get a limit() by constructing the select() first:

s = select([1]).where(
         table1.c.col_a == table2.c.colb
       ).where(
         table1.c.colb == table2.c.colb
       ).limit(30)
s = exists(s)
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, this is excellent! Thank you very much @zzzeek. In fact, this is fantastic! ;-))
One thing to note: to execute s, you need to generate a select for it: conn.execute(s.select()).
-1
query=select([db.Table_1])
query=query.where(
     and_(
           db.Table_2.col_a == db.Table_1.col_a,
           db.Table_2.col_b == db.Table_2.col_b
          )
     ).limit(30)

result=session.execute(query)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.