Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Using results from one query to use in another query

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.

like image 625
TheCarver Avatar asked Sep 16 '11 00:09

TheCarver


People also ask

How can we use one query result in another query in MySQL?

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.

How do I use one query in another SQL query?

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.

Why we use CTE instead of subquery?

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.


1 Answers

  1. 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.

  2. 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?

    • Instead of storing it in a temporary table, just do a JOIN to start with (see example below). In some cases databases will join an IN(select * from foo) for you.

  • Using IN(subselect):

    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
    
  • Using JOIN

    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
    
like image 89
vol7ron Avatar answered Oct 12 '22 20:10

vol7ron