Here is my sample query:
SELECT userid,count(*)
FROM hits
GROUP BY userid
My table is something like this
id | userid | time …etc
Where id is the primary key and I use this table to store every visit on a page.
Which means my table has 200,000+ rows.
For a userid lets say X i want to find out on which rank it is in the query that means how many users have visited that page more than the user with that userid.
I know there are many questions LIKE this but they aren’t same because
- My Query has group by
- I tried quite a few answers here some don’t even return anything while others take 5-10 mins. I need it to be faster.
for any further doubts pls clarify in comments
Thanks
Count/Group by in a query that’s expected to return multiple rows will get progressively slower because the query will still have to touch every row in the table. Generally, if you expect to do reports like this often, and you expect your table to continue to grow, you should begin rolling that value into a cached value (so you should store every result still, but you should also add to a counter on that user’s user record). Of course, this also begs the question of whether you have an index on your user_id column and a foreign key into your users table, which should speed that query up considerably.