Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9213123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:40:34+00:00 2026-06-18T01:40:34+00:00

I have a table with 4 columns (1 PK) from which I need to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:40:35+00:00Added an answer on June 18, 2026 at 1:40 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the query to select some columns from two tables, which must contain
I have a table which looks like this: mysql> SHOW COLUMNS FROM Users; +------------+--------------+------+-----+---------+----------------+
I have table with two columns (varchar from, varchar to). This table represents connections
I have the following Execution statement which creates a table (using data from another
I have an API where I need to log which ids from a table
I have a city table that has two columns from_city to_city now lets assume
I have a table with 4 columns. I'm trying to bulk insert from a
I have three mysql table from same database Db1. Three tables have following columns.
I'm looking for a query to select rows from two different tables, keeping the
I have a SQL select statement which is comparing two tables. I am getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.