Forum Moderators: open
I have the following query of sub-queries:
-----------------------------------------
SELECT *
FROM table1
WHERE
(
SELECT COUNT( col1 )
FROM table2
WHERE table1.col1 = table2.col2
) = 0
AND
(
SELECT COUNT( col1 )
FROM table3
WHERE table1.col1 = table3.col2
) = 0
AND
(
SELECT COUNT( col1)
FROM table4
WHERE table1.col1 = table4.col2
) < 2
ORDER BY table1.col3 ASC
-----------------------------------------
How can I make it A LOT more EFFICIENT?
It's terribly slow right now, although it does what I want.
I'm thinking JOIN's and Intersections, but I don't know how and it's beyond my level of expertise so I need expert help.
Thanks!
select
tb1.*,
count(tb2.col1) as tb2_count,
count(tb3.col1) as tb3_count,
count(tb4.col1) as tb4_count
from
table1 tb1, table2 tb2, table3 tb3, table4 tb4
where
tb1.col1 = tb2.col2
and tb1.col1 = tb3.col2
and tb1.col1 = tb4.col2
and tb2_count = 0
and tb3_count = 0
and tb4_count < 2
group by
tb2.col1,
tb3.col1,
tb4.col1
order by
tb1.col3 ASC
Another thing you can try playing around with is the "HAVING CLAUSE"
it would look something like:
group by
tb2.col1,
tb3.col1,
tb4.col1
HAVING COUNT(tb2.col1) = 0, COUNT(tb3.col1) = 0, COUNT(tb4.col1) < 2
SELECT *
FROM table1
WHERE NOT EXISTS (SELECT * FROM table2 WHERE table1.col1 = col2)
AND NOT EXISTS (SELECT * FROM table3 WHERE table1.col1 = col3)
AND ( SELECT COUNT( *) FROM table4 WHERE table1.col1 = col2) > 2
ORDER BY col3 ASC
What do you mean if it's indexed?
Also, would it be better if I just added a column to table3 (which is the biggest table) keeping track of col2's total so that then I can quickly check that column vs. doing a count? That would probably speed the query up a lot.
Any ideas?
An index simply creates sort of a "quick guide" to that field. It makes joining and performing functions on them quicker.
Most DBs will index a field if you make it the primary key but it also helps to index other fields that get used often for joining or functions.
Regardless of what solution you use you should index some of these fields.
Did you try HAVING?
It would look something like this:
select
tb1.*
from
table1 tb1, table2 tb2, table3 tb3, table4 tb4
where
tb1.col1 = tb2.col2
and tb1.col1 = tb3.col2
and tb1.col1 = tb4.col2
group by
tb2.col1,
tb3.col1,
tb4.col1
having
count(tb2.col1) = 0
and
count(tb3.col1) = 0
and
count(tb4.col1) < 2
order by
tb1.col3 ASC