I have two tables, Listings and Products. The Listings table has approx 1.2M records and the product table has approx 600K records. When I run the following query on the Listings table, the results return in 56.6 ms.
SELECT    l.field_name
FROM      Listing l
WHERE     l.deletedAt IS NULL
GROUP BY  l.field_name HAVING COUNT(l.field_name) > 1
When I add in a LEFT JOIN, it takes nearly 50 seconds to return the results..
SELECT    l.field_name, p.name
FROM      Listing l
LEFT JOIN Product p
 ON       l.product_id = p.id
WHERE     l.deletedAt IS NULL
GROUP BY  l.field_name HAVING COUNT(l.field_name) > 1
ORDER BY  l.field_name
In the Listings table there may be duplicates of the field_name, thus the HAVING statement.
How can I optimize this query to return results quicker. Thanks
Explain output
+----+-------------+-------+--------+---------------+---------+---------+---------------------+---------+----------------------------------------------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref                 | rows    | Extra                                        |
+----+-------------+-------+--------+---------------+---------+---------+---------------------+---------+----------------------------------------------+
|  1 | SIMPLE      | l     | ALL    | field_name    | NULL    | NULL    | NULL                | 1022146 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | p     | eq_ref | PRIMARY       | PRIMARY | 4       | dbName.l.product_id | 1       | Using index"                                 |
+----+-------------+-------+--------+---------------+---------+---------+---------------------+---------+----------------------------------------------+
 
     
     
    