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

  • SEARCH
  • Home
  • 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 8967335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:13:11+00:00 2026-06-15T17:13:11+00:00

I can’t show you my exact problem and query due to its complexity, but

  • 0

I can’t show you my exact problem and query due to its complexity, but I will simplify the problem into another alike one.

Here goes:

There are 5 tables, containing information about apples, cars, books, computers and clothes. Each table from the above can have comments and all the comments are stored inside a single table called Comments (with a user_id column). And to link those 5 tables with their own comments I had to create 5 other link tables names AppleComments, CarComments, BookComments, and so on.

My query wants to retrieve all my comments from those 5 category types.

I can think of 2 ways, one faster than the other.

  1. I can join the 5 tables with its linking table and then with comments where comments.user_id = me and then UNION ALL the results into 1 result set:

    SELECT <column_names> FROM AppleComments
    INNER JOIN Comments ON AppleComments.comment_id = Comments.comment_id
    WHERE Comments.user_id = me
    
    UNION ALL
    
    SELECT <column_names> FROM CarComments
    INNER JOIN Comments ON CarComments.comment_id = Comments.comment_id
    WHERE Comments.user_id = me
    etc...
    
  2. I can select all the comment id-s from the linkin tables with union all and then inner join this subquery with the Comments table to get the other info about them.

    SELECT <column_names> 
    FROM (
       Select AppleComments.comment_id FROM AppleComments
       UNION ALL
       Select CarComments.comment_id FROM CarComments
       UNION ALL
       Select BookComments.comment_id FROM BookComments
       UNION ALL
       Select ComputerComments.comment_id FROM ComputerComments
       UNION ALL
       Select ClothesComments.comment_id FROM ClothesComments) AS items
    INNER JOIN Comments ON items.comment_id = Comments.comment_id
    WHERE Comments.user_id = me
    

The second one is faster and produces a smaller execution plan.

The problem is, since the subquery ‘items’ from ex. 2 selects all the comments from the database actually and then inner joins with the comments table, my concern is that it will poorly perform when dealing with too many rows. Maybe too much memory?

Right now I can’t tell since 90% of the comments are mine, but judging a little bit each example, I’d say the 1st one is the one that retrieves smaller amounts of data (per total).

What if there are 1 million comments in the DB and only 100 of mine…

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-15T17:13:12+00:00Added an answer on June 15, 2026 at 5:13 pm

    Your intuition is correct. UNION ALL will pretty much obliterate chances of optimisation down the road. Do everything you can before unioning; the first approach is the right one.

    Except it’s begging the question of why do you have similar things in five different tables, and why do you even have linking tables in what appears to be a 1-to-n relationship? Would a single table with a itemtype field not suffice? If you have extra data, then have optional foreign links to additional data tables, is what I’d do…

    Of course, this is a made-up universe, I don’t have a clue about your original problem 🙂

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

Sidebar

Related Questions

Can any one tell, how to get the result of LINQ query contains group
I am currently running into a problem where an element is coming back from
can you show me how to assign css properties to the button that is
Can we change the HTML or its attributes of the selected part of the
Can anyone let me know how can we change the value of kendo combobox
Can I order my users in the database, so I don't have to say
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can I authenticate with just Google account username and password instead of using OAuth?
Can we change the default action of the edit selected row button? Here is
Can someone thoroughly explain the last line of the following code: def myMethod(self): #

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.