I have a table with million+ rows. Each of them have a license no.
The query I have right now, does a group by on license no and sorts by count(Distinct(type)) and count(license_no) and date.
All the fields with joins - license_no or date are indexed. But its taking me 5 seconds to return the results.
How do I speed up the performance. Ideally the results should not take more than a second. Query:
SELECT `license_no`,
COUNT(DISTINCT(type)) AS gdid,
COUNT(id) AS cdid,
max(updated_on) as maxdate
FROM `mytable`
WHERE `license_no` >0
GROUP BY `license_no`
ORDER BY `gdid` DESC, `cdid` DESC, maxdate DESC LIMIT 12
Logic I want to implement:
I have a list of cars (million + records).
I want to find all unique cars (unique by license_no) sorted by:
The only way you are going to make this run fast is by pre-aggregating. You can do this using triggers on mytable
. Your writes will be a little slower, but the query above will only need to scan a much smaller table.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.