shadedecho
Programmer
Code:
mytable1: mytable2:
+-----+-----+ +-----+-----+
| A | B | | A | B |
+-----+-----+ +-----+-----+
| 1 | 2 | | 5 | 6 |
| 3 | 4 | | 6 | 7 |
| 5 | 6 | | 3 | 3 |
| 7 | 8 | +-----+-----+
+-----+-----+
I need to constuct a SQL query that basically tells me if a test value i am putting into the query is (or is not) in the `B` column of either table.
My first thought is to do a cross join on the two tables, and a WHERE clause which checks mytable1.B or mytable2.B... in fact, this works.
SELECT myt1.`B`,myt2.`B` FROM `mytable1` myt1, `mytable2` myt2 WHERE myt1.`B` = '$$test$$' OR myt2.`B` = '$$test$$'
the cross join (before the WHERE clause) would obviously look like:
Code:
+-----+-----+
| 2 | 6 |
| 4 | 6 |
| 6 | 6 |
| 8 | 6 |
| 2 | 7 |
| 4 | 7 |
| 6 | 7 |
| 8 | 7 |
| 2 | 3 |
| 4 | 3 |
| 6 | 3 |
| 8 | 3 |
+-----+-----+
But, I am concerned that as the length of my tables grows, the cross join memory consumption will grow exponentially.
If i understand the process of JOINS correctly, it creates that temporary JOIN'd table, THEN applys the WHERE clause to each row to see if its included in the result set. This means that as the data grows, the temp table grows BIG as well.
However, I think the ON clause is applied row by row in the creation of the JOIN'd temp table. {please someone correct me if i am wrong, here}
Which leads to my first idea: If I moved the conditions that are in the WHERE clause to an ON condition in the INNER (cross) JOIN, like
Code:
SELECT myt1.`B`,
myt2.`B`
FROM `mytable1` myt1
INNER JOIN `mytable2` myt2
ON (myt1.`B` = '$$test$$' OR myt2.`B` = '$$test$$')
would that limit the JOIN table that is created to only rows that satisfied the ON condition? or is ON applied AFTER the creation of the JOIN'd temp table, as is WHERE? if so, anyone with ideas on how to construct a query who's memory consumption/complexity doesn't grow so rapidly as the data set(s) grow?