Project

General

Profile

Mysql Nested Subqueries Problem » History » Version 3

Amber Herold, 03/04/2010 01:38 PM

1 1 Amber Herold
h1. Mysql Nested Subqueries Problem
2
3 3 Amber Herold
This is the query that I wanted to do because it is easy to understand (= easier to maintain).
4
Unfortunatly there is a bug in mysql (http://bugs.mysql.com/bug.php?id=10312) which makes this 
5
so slow that no one has seen it complete. The subsequent query is a bit more difficult to follow
6
but gets around this problem. 
7 1 Amber Herold
			
8 3 Amber Herold
SELECT ".DB_PROJECT.".projectexperiments.projectId 
9
FROM ".DB_PROJECT.".projectexperiments 
10
WHERE ".DB_PROJECT.".projectexperiments.name IN 
11
( 
12
    SELECT ".DB_LEGINON.".SessionData.name 
13
    FROM ".DB_LEGINON.".SessionData 
14
    WHERE ".DB_LEGINON.".SessionData.`DEF_id` IN 
15
    ( 
16
        SELECT ".DB_PROJECT.".shareexperiments.`REF|leginondata|SessionData|experiment` 
17
        FROM ".DB_PROJECT.".shareexperiments 
18
        WHERE ".DB_PROJECT.".shareexperiments.`REF|leginondata|UserData|user` = ".$userId." 
19
    )
20
);
21 1 Amber Herold
			
22 3 Amber Herold
SELECT amberproject.projectexperiments.projectId 
23
FROM amberproject.projectexperiments 
24
INNER JOIN 
25
(
26
    SELECT amberdbemdata.SessionData.name AS SessionName 
27
    FROM amberdbemdata.SessionData 
28
    INNER JOIN 
29
    ( 		
30
        SELECT amberproject.shareexperiments.`REF|leginondata|SessionData|experiment` AS SessionId 
31
        FROM amberproject.shareexperiments 
32
        WHERE amberproject.shareexperiments.`REF|leginondata|UserData|user` = ".$userId." 
33
    ) AS SessionIds 
34
    ON amberdbemdata.SessionData.`DEF_id` = SessionIds.SessionId 
35
) AS SessionNames 
36
ON amberproject.projectexperiments.name = SessionNames.SessionName 
37 1 Amber Herold
38
39 2 Amber Herold
See another example "here":http://forums.mysql.com/read.php?24,54721,54721#msg-54721.