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 8117573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:13:06+00:00 2026-06-06T04:13:06+00:00

Here are two queries that return the same resultset, but which is the optimal

  • 0

Here are two queries that return the same resultset, but which is the optimal statement or doesn’t it matter?

SELECT A.id, B.somefield FROM (
   SELECT id from table1
   UNION
   SELECT id from table2
) A LEFT JOIN table3 B on A.id = B.id

or

SELECT A.id, B.somefield FROM table1 A LEFT JOIN table3 B on A.id = B.id
UNION
SELECT A.id, B.somefield FROM table2 B LEFT JOIN table3 B on A.id = B.id

I realise I could pump them full of data and run some tests, but I am as much interested in the ‘why’ if one is faster? (I am using postgresql, in case it influences things).

Thanks.

  • 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-06T04:13:08+00:00Added an answer on June 6, 2026 at 4:13 am

    OK, first off, id in the select list is ambiguous; do we want A.id or B.id?

    Second, assuming id is an indexed field in all tables, de-duping and joining are both NlogM operations, where N is the number of rows on the “left” side and M the number of rows on the “right” side. For each row in N, a matching row in M must be found or not found (when joining, rows found in M are included in the results; when unioning, rows found in M are excluded). This would mean that minimizing the cardinality of the left side will give the greatest performance.

    So, the complexity of either query pretty much depends on how many shared IDs there are between table 1 and table 2. With zero commonality (no rows IDs the same) and 100 rows per table, the first query will perform one 100log100 union and then a 200log100 join, and the second query will perform two 100log100 joins and then a 100log100 union, which would execute in equivalent time. However, with 100% commonality (every row in table 1 is also in 2), the first query will perform a 100log100 union, then a 100log100 join (as the UNION of 1 and 2 would be equivalent to table 1), while the second query will still perform two 100log100 joins and a 100log100 union. As the worst-cases are the same but the best-case of query 1 is two-thirds that of query 2, I’d go for query 1.

    However, as the commenter said, if you don’t expect any dupes, a UNION ALL will perform better in both queries. The result of a UNION ALL of A and B is A+B, which is bound only by the access time of each set (which I haven’t been considering). By not expecting dupes, both of the queries can be cut to the best-case performance of the first query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's two screen shots, showing the effect with a small viewport that has to
Here is two my variants of method, which returns a string, associated with enum
I have a menu bar that is rotated slightly. Here are two buttons as
Here is the two classes that I'm use to express a bidirectional OneToMany relationship.
I have 2 sql queries that return results, both contain a contract number, now
How could I optimize this MySQL queries that is accessing two tables with more
Two queries that should result in a list of account numbers (strings). However the
I'm trying to parse results from queries over HTTP that can return up to
I am trying to use a prepared statement that deletes from two (temporary) tables:
I am doing two queries SELECT * FROM datedim WHERE year = YEAR(now()); and

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.