0

这是我的示例查询:

SELECT userid,count(*)
FROM hits
GROUP BY userid

我的桌子是这样的

编号 | 用户名 | 时间...等

其中 id 是主键,我使用此表来存储页面上的每次访问。这意味着我的表有 200,000 多行。

对于用户 ID,假设 X 我想找出它在查询中的排名,这意味着有多少用户访问了该页面,而不是具有该用户 ID 的用户。

我知道有很多这样的问题,但它们不一样,因为

  1. 我的查询有分组依据
  2. 我在这里尝试了很多答案,有些甚至不返回任何东西,而另一些则需要 5-10 分钟。我需要它更快。

如有任何进一步的疑问,请在评论中澄清

谢谢

4

1 回答 1

0

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.

于 2012-06-22T20:20:25.013 回答