I have written a query which joins three different tables. Rough descriptions of the tables are as below:
Table 1: account_data
columns: id(pk), property_id(fk), account_id(fk), value, status
No Of Data: around 30 milions
Table 2: account
columns: id(pk), service_id(fk), status
No Of Data: around 5 milions
Table 3: property
columns: id(pk), create_analytics(index), status, uri
No Of Data: few hundreds
The query is as below:
SELECT ad.value, p.uri, count(ad.id)
FROM account_data ad
INNER JOIN property p ON ad.property_id = p.id AND (p.status = 1)
INNER JOIN account ac ON ad.account_id = ac.id AND (ac.status = 1)
WHERE (p.create_analytics = '1' AND ac.service_id = ?) AND (ad.status = 1)
GROUP BY ad.property_id, ad.value
At the beginning the query was taking too long. after creating index on 'create_analytics' column, it came down noticeably.
But still, the query is taking too long(more than 3 minutes). I tried by creating index on group by columns(property_id and value), creating indexes on 'status' columns, but neither didn't show up as improvement.
Just wondering, is there any other way to rewrite this query to make it faster? Or did I miss anything where creating index, change order would help?
Looking forward to all of your thoughts/suggestions to solve this issue. Thanks in advance.