Trying to run the following query on a mysql table that has over 3 million rows. Its very slow to the point it pretty much hangs until the script times out. Below is the query and the explain from that query, any suggestions?
SELECT SQL_CALC_FOUND_ROWS
listing_track.listingid,
listing_track.commid,
listing.listingname,
listing_package.packagename,
listing.active,
community.commname,
SUM( listing_track.impression ) AS listing_impressions,
SUM( listing_track.view ) AS listing_views,
SUM( listing_track.phone ) AS listing_phones,
SUM( listing_track.forward ) AS listing_forward,
SUM( listing_track.coupon ) AS listing_coupons,
SUM( listing_track.email ) AS listing_emails
FROM listing_track
INNER JOIN listing ON listing_track.listingid = listing.id
INNER JOIN community ON listing_track.commid = community.id
INNER JOIN listing_package ON listing.packageid = listing_package.id
WHERE listing_track.commid =2
GROUP BY listing_track.commid, listing_track.listingid, listing_track.trackip
LIMIT 0 , 25
Here is the explain:

The problem here is that the LIMIT is applied at the end of the query, after all the costly table scans are complete. The cost is not from returning lots of rows, but instead from scanning lots of rows.
The easiest way to speed up queries like this is with a covering index. This will allow you to scan through the rows you want, but require fewer bytes of I/O per row (since you’re only scanning a portion of each row’s data, not the entire row). Furthermore, if your index is sorted in the same way that your query is, you can avoid the cost of a sort and can scan vastly fewer rows.
Your index should have these columns below. The first three columns must be in the same order as your GROUP BY– this allows your
GROUP BYandWHEREto be vastly cheaper to execute. The second line allows the index to “cover” the query, which means that MySQL will be able to satisfy the entire listing_track part of the query from the index alone:With this index in place, you should be able to run the exact same query but see vastly better performance.