Let's say I have this, that produces 50,000 rows:
SELECT photoID FROM photoSearch WHERE photoID BETWEEN 1 AND 50000;
I was going to run this query against those photoID's just returned.
SELECT COUNT(people) AS totalPeople, people
FROM people
INNER JOIN photopeople ON photoPeople.peopleID = people.PeopleID
WHERE photoid IN ('ID's from results')
GROUP BY people
ORDER BY totalPeople DESC
But I understand from others and resources, that the IN clause will not perform well, especially as I could have 100,000 plus photoID's.
Is it a good idea to store the photoID's from the top query in another table (resultsTbl) or in a very long string? If yes to either, do I use a join or a sub-select to query those ID's (in the bottom query), instead of using IN? Or... is there another way that will do the job keeping performance in mind?
Any help on this would be gratefully received.
Use the results of a query as a field in another query. You can use a subquery as a field alias. Use a subquery as a field alias when you want to use the subquery results as a field in your main query. Note: A subquery that you use as a field alias cannot return more than one field.
A subquery is a query nested inside another query. It can be placed anywhere in a query, even within another subquery. The syntax is very simple – you just put your subquery inside parentheses and insert it into the main query, wherever it is needed.
Advantage of Using CTECTE can be more readable: Another advantage of CTE is CTE are more readable than Subqueries. Since CTE can be reusable, you can write less code using CTE than using subquery. Also, people tend to follow the logic and ideas easier in sequence than in a nested fashion.
Is it a good idea to store the photoID's from the top query in another table (resultsTbl) or in a very long string?
In another table: Generally, no. If there are a lot of IDs and you perform the top query in other places, then storing it in a cache-ing table could be okay. Though, for this case, the "top query" is most likely going to remain in memory, so you should probably use a subselect.
In a very long string: No. String operations are generally highly CPU intensive.
If yes to either, do I use a join or a sub-select to query those ID's (in the bottom query), instead of using IN?
IN(select * from foo)
for you.SELECT count(people) AS totalPeople
, people
FROM people
INNER JOIN photopeople ON photoPeople.peopleID = people.PeopleID
WHERE photoid IN (select photoID
from photoSearch
where photoID
between 1 AND 50000)
GROUP BY people
ORDER BY totalPeople DESC
SELECT count(people) AS totalPeople
, people
FROM people
INNER JOIN photopeople ON photoPeople.peopleID = people.PeopleID
INNER JOIN photoSearch ON photopeople.photoid = photoSearch.photoID
WHERE photoID between 1 AND 50000
GROUP BY people
ORDER BY totalPeople DESC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With