I'm trying to build a leaderboard in which I'm using this query:

```sql
SELECT id, COUNT(type) FROM Data 
WHERE UPPER(type) = $1 AND id != '123123123123' 
GROUP BY id ORDER BY COUNT(type) DESC LIMIT 10;

```

Which takes about 8-10 seconds in my table `Data` consisting of approx. 4 million rows and growing by 100,000+ rows per day.

The table also has a UNIQUE INDEX which is unique when combining values from 2 different columns — `code` and `version`.

What can I do to increase the speed and performance of this query?

I've considered building a counter for each user's rows, but that wouldn't be very intuitive as the `type` can differ between 100+ values. I've also tried adding an index on `id` and `type` which still takes after 8-10 seconds if not more.