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.
-
I can join the 5 tables with its linking table and then with comments
where comments.user_id = meand 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... -
I can select all the comment id-s from the linkin tables with union all and then inner join this subquery with the
Commentstable 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.
Your intuition is correct.
UNION ALLwill 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
itemtypefield 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 🙂