I'm working on selecting locations (city, state) out of a database. The problem is that the query is running a tad slow and I'm not sure how to speed it up. For example:
SELECT CONCAT_WS(', ', city, state) as location, AVG(latitude), AVG(longitude)
FROM places
WHERE city='New York' AND state='NY'
GROUP BY location
There's going to be a CONCAT on the location regardless, because I want the database to return a pretty, concatenated version of the location (unless there's reason to do this in the code instead). For example, "New York, NY". In reality, a third column is sometimes thrown into the mix (zipcode). I'm running on MySQL.
What would be the best way to optimize this query?
Also, as a secondary question, would adding "DISTINCT" slow down the query in any way? For example:
SELECT DISTINCT CONCAT_WS(', ', city, state) as location, AVG(latitude), AVG(longitude)
FROM places
WHERE city='New York' AND state='NY'
GROUP BY location
(I'm currently doing this now, but in the process of asking this question, I realized that DISTINCT was not necessary due to the GROUP BY clause; however, since it is unnecessary I wonder if it makes any difference at all and if I should bother rocking the boat in order to speed up the query.)
Edit: There's already an index on city, state and zipcode; plus their combinations therein (city, zipcode; and state/zipcode alone).