473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

UNION and optimization?

My question is about a UNION query to deal with an (annoying) JOIN
over
two tables. I am joining over a double column primary key (where the
order of the columns can be changed). This is sooo slooow using a
join, but very fast using a union. How come this is?

Here is more detail...

I have two tables of 'associations' between pairs of objects.

An example table looks like this (and both have the same format) ...

OBJECT_1 | OBJECT_2 | ASSOCIATION_PARAMETER_1 |
ASSOCIATION_PARAMETER_2
A | B | w | x
B | C | y | z
....

PK = (A,B)

I want to join two tables with the above format. The problem is that
one
table my have ...

TABLE_1,
OBJECT_1 = A
OBJECT_2 = B
And the other my have

TABLE_2,
OBJECT_1 = B
OBJECT_2 = A
Please beleive that I have no alternative but to put up with this
situation.
So, I do my join like this (using a sub-query)

SELECT
*
FROM
TABLE_1
INNER JOIN
(
SELECT
OBJECT_1,
OBJECT_2,
ASSOCIATION_PARAMETER_1,
ASSOCIATION_PARAMETER_2
FROM
TABLE_2
#
UNION
#
SELECT
OBJECT_2,
OBJECT_1,
ASSOCIATION_PARAMETER_1,
ASSOCIATION_PARAMETER_2
FROM
TABLE_2
) AS x
USING
(OBJECT_1, OBJECT_2)
;

This executes very fast, creating a 'virtual' table called x, which
combines all the unique rows of the UNION. NB: OBJECT_1 is never equal
to OBJECT_2 in any one row of any table.

The virtual table contains all (distinct) possible orderings of the
pairing OBJECT_1 - OBJECT_2, and the INNER JOIN works only for rows
with
the correct (matching) order of objects.

The above works fine, and is very fast (my primary key is (OBJECT_1,
OBJECT2), and I have an additional index on OBJECT_2).

The above is *bulky*, and when the individual queryies get more
complex it
is a pain to keep the two parts (the two halves of the UNION) 'in
sync' -
identical that is.

However, the following query is *very* slow...

SELECT
*
FROM
TABLE_1
INNER JOIN
TABLE_2
ON
(TABLE_1.OBJECT_1 = TABLE_2.OBJECT_1 AND
TABLE_1.OBJECT_2 = TABLE_2.OBJECT_2) OR
(TABLE_1.OBJECT_1 = TABLE_2.OBJECT_2 AND
TABLE_1.OBJECT_2 = TABLE_2.OBJECT_1)
;
I wan't to know why the first query is very fast, and the second
(which is
more compact and easier to read in my opinion) is soooo slooooow...

Is this an optimizer bug? Should I submit this as a bug?
I also tried this... (again messy)...

SELECT
IF(x.OBJECT_1<x.OBJECT_2,x.OBJECT_1,x.OBJECT_2) AS OBJECT_1,
IF(x.OBJECT_1>x.OBJECT_2,x.OBJECT_1,x.OBJECT_2) AS OBJECT_2,
x.ASSOCIATION_PARAMETER_1,
x.ASSOCIATION_PARAMETER_2,
y.ASSOCIATION_PARAMETER_1,
y.ASSOCIATION_PARAMETER_2,
FROM
TABLE_1 x
INNER JOIN
TABLE_2 y
ON
IF(x.OBJECT_1<x.OBJECT_2,x.OBJECT_1,x.OBJECT_2) =
IF(y.OBJECT_1<y.OBJECT_2,y.OBJECT_1,y.OBJECT_2)
AND
IF(x.OBJECT_1>x.OBJECT_2,x.OBJECT_1,x.OBJECT_2) =
IF(y.OBJECT_1>y.OBJECT_2,y.OBJECT_1,y.OBJECT_2)
;

But this isn't fast either (EXPLAIN looks the same as with the above,
and it runs in about the same time).

Why am I such a fool for SQL? Sorry for wasting your time. All
feedback
welcome,

Dan.
Jul 20 '05 #1
1 1998
dmb000006 wrote:
I want to join two tables with the above format. The problem is that
one table may have ...

TABLE_1,
OBJECT_1 = A
OBJECT_2 = B

And the other may have

TABLE_2,
OBJECT_1 = B
OBJECT_2 = A


Make sure you have indexes on OBJECT_1 and OBJECT_2 in both tables.
Even if you have a primary key index defined over the two columns, try
putting an additional index on just the OBJECT_2 column.

I'm thinking that the slowness is due to an unindexed search through the
OBJECT_2 columns. This might be a case that is a little too obscure for
the optimizer to know that it can benefit from the compound index.

Also, here's another possible design to make the query more compact,
similar to your IF() technique:

SELECT LEAST(x.OBJECT_1, x.OBJECT_2) AS OBJECT_1,
GREATEST(x.OBJECT_1, x.OBJECT_2) AS OBJECT_2
FROM TABLE_1 x INNER JOIN TABLE_2 y ON
(LEAST(x.OBJECT_1, x.OBJECT_2) = LEAST(y.OBJECT_1, y.OBJECT_2) AND
GREATEST(x.OBJECT_1, x.OBJECT_2) = GREATEST(y.OBJECT_1, y.OBJECT_2))

Regards,
Bill K.
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bryan Parkoff | last post by:
byte and word and double word are always in alignment. Do C++ Compiler fill the zero as padding inside byte's field and word's field to avoid misalignment? It looks like that union does not use...
6
by: Eugene | last post by:
Summary: ---------- Updates against UNION ALL view does't do branch elimination, but rather reads all the branches (partitions). The case scenario(DB2 V8.1.4a ESE, AIX 5.2):...
8
by: lyn.duong | last post by:
Hi, I have a large table (about 50G) which stores data for over 7 years. I decided to split this table up into a yearly basis and in order to allow minimum changes to the applications which...
13
by: Razmig K | last post by:
Dear mates, This is an another survey for the common (and uncommon) nontrivial uses of the aforementioned C construct. It's posted by the same average C programmer who's made a similar survey...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
2
by: jafastinger | last post by:
I have a large union. If I break it into its individual parts they all run quick. The longest is the last select it takes 2 minutes to fetch all rows. When I run the query below it does not...
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
29
by: Richard Harter | last post by:
There is probably a simple way to do what I want but I don't see it. Any suggestions are welcome. Suppose I have a function foo with an argument that can be any of several types and that I want...
18
by: Bryan Parkoff | last post by:
I hate using struct / union with dot between two words. How can I use one word instead of two words because I want the source code look reading clear. three variables are shared inside one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.