I have 3 tables:
- districts | id, zipcode, district
- subscription | id, userid, status, level
- profile | id, userid, zip
I am trying to count the amount of zip codes from active subscriptions and group them by district. ( I am using LEFT() so I can can include zips in the #####-#### format). The query works in 6.4 seconds locally, but on the server isn’t outputting in a timely manner. What can I do to speed this up?
I have written:
SELECT COUNT( d.zipcode ) total, d.district
FROM districts AS d
JOIN profile AS p ON d.zipcode = LEFT(p.zip, 5)
JOIN subscriptions AS s ON s.userid = p.userid
WHERE s.status = 1
GROUP BY d.district
Thanks!
create an index on:
d.zipcode
p.zip
s.userid
p.userid
s.status