Newtang
Newtang

Reputation: 6544

Joining 2 Select statements and limiting the 2nd to the results of the first

I'm trying to join the first 100 results fron one inner select to a second.

My example is a little contrived, but I have a table of email addresses with a number of them that get marked as dirty, and a table of observations that I map emails and names to (they both comprise the primary key in this table). I want the result to be the number of distinct names per email address.

SELECT ea.email, tmp.uniques FROM 
  (SELECT email FROM emails WHERE dirty = 1 LIMIT 100) ea
  INNER JOIN 
  (SELECT email, COUNT(DISTINCT(name)) as uniques FROM nameEmailObservations GROUP BY email ) tmp 
  ON (tmp.email = ea.email);

The query appears to be valid, but it's taking a long time. Any idea why or what I can do? I suspect it's because the 2nd query isn't having its results limited to the first query. The second query does take a long time by itself, because nameEmailObservations is huge.

Upvotes: 0

Views: 102

Answers (1)

paulbailey
paulbailey

Reputation: 5346

Surely:

SELECT 
    e.email,
    COUNT(DISTINCT neo.name)
FROM 
    emails e
INNER JOIN
    nameEmailObservations neo
ON
    e.email = neo.email
WHERE
   e.dirty = 1
GROUP BY
    e.email
LIMIT 100

is what you're trying to do?

If it's the LIMIT that's causing the problem (i.e. only being applied at the end of the query), how about:

SELECT 
    email,
    COUNT(DISTINCT name)
FROM 
    nameEmailObservations
WHERE
    email IN
    (SELECT email FROM emails WHERE dirty = 1 LIMIT 100)
GROUP BY
    email

Upvotes: 1

Related Questions