Dear all,
I have a problem with one SQL query. I want to list all records from one table (in example bellow: "table1") matching some conditions - which are stored in "table2".
The structure is attached to this post:
The problematic query follows:
SELECT DISTINCT table1.*
FROM table1
INNER JOIN resttyperest ON table2.table1_id=table1.id
WHERE table2.table3_id IN (6, 10, 15)
ORDER BY restaurant.name;
This query uses temporary table and also filesort.
So I tried to rewrite it to:
SELECT table1.*
FROM table1
WHERE
(SELECT id FROM table2
WHERE table2.table3_id IN (6, 10, 15)
AND table2.table1_id=table1.id
) IS NOT NULL
ORDER BY table1.name
This query uses only filesort and also runs faster (10-times) ).
1) Are there any cons if I decide to use the second query with subqueries (on MySQL 5.0.x)?
2) Do you have any ideas how to improve the second query?
3) Do you use similar table-structure, if you have records (table1), types (table3) and their relation is N:M?
4) Any other suggestions, notes? )
Thanks...
I have a problem with one SQL query. I want to list all records from one table (in example bellow: "table1") matching some conditions - which are stored in "table2".
The structure is attached to this post:
The problematic query follows:
SELECT DISTINCT table1.*
FROM table1
INNER JOIN resttyperest ON table2.table1_id=table1.id
WHERE table2.table3_id IN (6, 10, 15)
ORDER BY restaurant.name;
This query uses temporary table and also filesort.
So I tried to rewrite it to:
SELECT table1.*
FROM table1
WHERE
(SELECT id FROM table2
WHERE table2.table3_id IN (6, 10, 15)
AND table2.table1_id=table1.id
) IS NOT NULL
ORDER BY table1.name
This query uses only filesort and also runs faster (10-times) ).
1) Are there any cons if I decide to use the second query with subqueries (on MySQL 5.0.x)?
2) Do you have any ideas how to improve the second query?
3) Do you use similar table-structure, if you have records (table1), types (table3) and their relation is N:M?
4) Any other suggestions, notes? )
Thanks...
Comment