简体   繁体   中英

Group by with sort on more than million rows

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:

  • license_no which has max count of different types
  • license_no which has max total counts
  • finally sort individual records by latest date.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM