473,395 Members | 1,554 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,395 software developers and data experts.

Alternative to self-joins??

I have a table that has values of variables for certain entities. The
columns of interest are targetID, variableID, and valueID. A row (1, 5,
9) means that target number 1 has a value of 9 for variable 5. Being
denormalized, target number one will have many possible rows in this
table, one for each variable for which it has a value.

My problem occurs when I want to find out what targets match a certain
set of variable values. For instance, I want to find out what targets
have a value of 9 for variable 5 and a value of 25 for variable 10. I'm
thinking that this can be a simple self-join:

SELECT mya.targetID from mytable as mya
LEFT JOIN mytable as myb
ON mya.targetID=myb.targetID
WHERE (mya.variableID=5 AND mya.valueID=9)
AND (myb.variableID=10 AND myb.valueID=25)

Does this make sense so far? The problem is that this doesn't scale.
When I have more than 31 variables and I need to evaluate them all,
MySQL breaks: I can't do more than 31 joins.

My design calls for perhaps 80-100 variables, so even 64-bit
architecture with a limit of 64 joins won't get me there.

I need another data structure that won't get me stuck on too many
joins. Any suggestions? If I have to scrap this approach in favor of
another, I can do that; even some clues on what direction to head out
on would be helpful. I'm stuck at the present. Thanks.

Jul 23 '05 #1
1 2778
nu***@bway.net wrote:
My design calls for perhaps 80-100 variables, so even 64-bit
architecture with a limit of 64 joins won't get me there.

I need another data structure that won't get me stuck on too many
joins. Any suggestions?


Not every SQL task can be done in a single statement. Or at least,
doing it in a single statement is so difficult that it's not worth the
time spent to develop and maintain the solution.

You could do this in a straightforward and scalable way by iterating
through the tests:

CREATE TEMPORARY TABLE joinSat (
targetID INT NOT NULL PRIMARY KEY,
join_satisfied TINYINT NOT NULL DEFAULT 1
);

Prime the temp table with the list of all targetID's:

INSERT INTO joinSatisfied (targetID, join_satisfied)
SELECT DISTINCT targetID, 1 FROM mytable;

Then for each variable test, change the join_satisfied field to 0 if you
can't find matching targetID's associated with the value you're looking
for. You could use MySQL's multi-table UPDATE syntax.

UPDATE joinSatisfied LEFT OUTER JOIN myTable
ON (j.targetID = m.targetID AND m.variableID = 5 AND m.valueID = 9);
SET joinSatisfied.bool = 0
WHERE myTable.targetID IS NULL;

UPDATE joinSatisfied LEFT OUTER JOIN myTable
ON (j.targetID = m.targetID AND m.variableID = 10 AND m.valueID = 25);
SET joinSatisfied.bool = 0
WHERE myTable.targetID IS NULL;

....etc.

Repeat the UPDATE statement for each of your variables that you're
looking for, and in the end your temp table has a 1 for each targetID
that satisfied all the tests, and 0 otherwise.

SELECT t.*
FROM targetMasterTable AS t INNER JOIN joinSatisfied as j
ON (t.targetID = j.targetID)
WHERE j.bool = 1;

Regards,
Bill K.
Jul 23 '05 #2

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

Similar topics

46
by: Robin Becker | last post by:
It seems that the rotor module is being deprecated in 2.3, but there doesn't seem to be an obvious alternative. I'm using it just for obfuscation. It seems we have ssl available in 2.3 for sockets,...
99
by: Paul McGuire | last post by:
There are a number of messages on the python-dev mail list that indicate that Guido is looking for some concensus to come from this list as to what *one* alternative syntax for decorators we would...
7
by: Henry Ludemann | last post by:
I've been writing an optparse alternative (using getopt) that is at a stage where I'd be interested in people's opinions. It allows you to easily creating command line interfaces to existing...
6
by: Steve | last post by:
I'm looking at a different means of creating a popup to calling js. I'd like to use the standard <a href... target="_new"> syntax, so that I can dynamically control the content of the popup using...
6
by: Pablo | last post by:
Hello all, sorry if this is a faq... Problem: The intended effect is to override the method 'getattr' in a way that i dont need to override the property explicitly too. class Base(object):...
11
by: Francine.Neary | last post by:
I've read that as well as "normal" Java-like function definitions, e.g. int main(int argc, char **argv), you can also choose to use an alternative syntax, i.e. int main(argc, argv) int argc;...
8
by: sturlamolden | last post by:
On Mar 27, 4:44 pm, Jean-Paul Calderone <exar...@divmod.comwrote: This is technically not correct. PyPy is hosted by RPython, which is not Python but a different language all together.
12
by: Frank Millman | last post by:
Hi all I have a standard requirement for a 'decimal' type, to instantiate and manipulate numeric data that is stored in a database. I came up with a solution long before the introduction of the...
7
by: kretik | last post by:
I'm sure this is a popular one, but after Googling for a while I couldn't figure out how to pull this off. Let's say I have this initializer on a class: def __init__(self, **params): I'd...
37
by: Chris Becke | last post by:
I know this isn't the place for suggestions, but I just need to vent. Why dont c++ compilers let us use . as way of saying "this->", and hence an alternative for m_ struct foo { int a; void...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.