Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Alternative to self-joins??

 
   Database Help (Home) -> mySQL RSS
Next:  SQLXML 3.0 SP2, SAX Parser error  
Author Message
nuked

External


Since: Feb 12, 2005
Posts: 1



(Msg. 1) Posted: Sat Feb 12, 2005 8:22 pm
Post subject: Alternative to self-joins??
Archived from groups: mailing>database>mysql (more info?)

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.

 >> Stay informed about: Alternative to self-joins?? 
Back to top
Login to vote
Bill Karwin1

External


Since: Jun 17, 2004
Posts: 42



(Msg. 2) Posted: Sun Feb 13, 2005 7:09 pm
Post subject: Re: Alternative to self-joins?? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

nuked.RemoveThis@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.

 >> Stay informed about: Alternative to self-joins?? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Best way to issue hundreds of inserts/updates??? - Using mysql 4.0.23- What is the best way to execute several (hundreds of) inserts and updates? Rather than issuing tons of individual inserts and updates, can I send the strings to a text file and then have mysql do them all?? IE : query.txt insert..

MySQL freezes, brings XP machine to a grinding halt - I've been using MySQL for a while for fairly light database development on my XP machine. Currently, I am just starting a new project and have experienced some big problems with MySQL today both on my office machine and at home where running a particular...

login as user 'root' but do not have root privlages and my.. - Hi gang: I'm experiencing a problem with MySQL -- I updated MySQL from version 4.1.0 to 4.1.10 and now when I login as root it doesn't show all the databases I should have access to, nor it doesn't recognize me being logged in as root (via..

FLUSH TABLES hangs if table is locked - Using FLUSH TABLES via the C query API mysql_query() hangs if the table is locked already. That is to say, nothing prevents me from running a LOCK TABLES twice; it won't tell me "it's already locked, don't try to run a FLUSH". Anyone know ...

Tool to convert DDL in graphical diagram? - Does anyone know of a tool that will create a graphical diagram from a DDL (SQL create script)? Preferable a free open-source tool. Thanks, A
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]