Project

General

Profile

Mysql Nested Subqueries Problem » History » Version 5

Amber Herold, 03/04/2010 01:42 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 4 Amber Herold
<pre>
9 5 Amber Herold
SELECT DB_PROJECT.projectexperiments.projectId 
10
FROM DB_PROJECT.projectexperiments 
11
WHERE DB_PROJECT.projectexperiments.name IN 
12 3 Amber Herold
( 
13 5 Amber Herold
    SELECT DB_LEGINON.SessionData.name 
14
    FROM DB_LEGINON.SessionData 
15
    WHERE DB_LEGINON.SessionData.`DEF_id` IN 
16 3 Amber Herold
    ( 
17 5 Amber Herold
        SELECT DB_PROJECT.shareexperiments.`REF|leginondata|SessionData|experiment` 
18
        FROM DB_PROJECT.shareexperiments 
19
        WHERE DB_PROJECT.shareexperiments.`REF|leginondata|UserData|user` = ".$userId." 
20 3 Amber Herold
    )
21
);
22 4 Amber Herold
</pre>
23 1 Amber Herold
			
24 4 Amber Herold
<pre>
25 5 Amber Herold
SELECT DB_PROJECT.projectexperiments.projectId 
26
FROM DB_PROJECT.projectexperiments 
27 3 Amber Herold
INNER JOIN 
28
(
29 5 Amber Herold
    SELECT DB_LEGINON.SessionData.name AS SessionName 
30
    FROM DB_LEGINON.SessionData 
31 3 Amber Herold
    INNER JOIN 
32
    ( 		
33 5 Amber Herold
        SELECT DB_PROJECT.shareexperiments.`REF|leginondata|SessionData|experiment` AS SessionId 
34
        FROM DB_PROJECT.shareexperiments 
35
        WHERE DB_PROJECT.shareexperiments.`REF|leginondata|UserData|user` = ".$userId." 
36 3 Amber Herold
    ) AS SessionIds 
37 5 Amber Herold
    ON DB_LEGINON.SessionData.`DEF_id` = SessionIds.SessionId 
38 1 Amber Herold
) AS SessionNames 
39 5 Amber Herold
ON DB_PROJECT.projectexperiments.name = SessionNames.SessionName;
40 4 Amber Herold
</pre>
41 1 Amber Herold
42
43 2 Amber Herold
See another example "here":http://forums.mysql.com/read.php?24,54721,54721#msg-54721.