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.
Surely:
is what you’re trying to do?
If it’s the
LIMITthat’s causing the problem (i.e. only being applied at the end of the query), how about: