如何实现Foursquare的“市长”功能功能 - 找到最近 N 天内得分最高的用户?

发布于 2024-09-18 16:41:55 字数 231 浏览 5 评论 0原文

在 Foursquare 中,过去 N 天内在某个地点得分最高的用户将被授予该地点的市长职位。

实现这一目标最有效的方法是什么?

用户可能已经检查了数百个地方。要显示某个用户所属的所有市长职位,需要将这数百个地点一一遍历,并查看每个地点最近60天内他的得分是否最高,这听起来非常低效。

是否有任何 SQL 或算法魔法可以快速执行该任务?

更新:我正在使用 MySQL 和 Django

In Foursquare, the user who has the highest score for a place in the last N days is awarded the Mayorship of that place.

What is the most efficient way to implement that?

A user could have checked into hundreds of places. To display all the mayorships that belong to a user, it'd be necessary to go through all those hundreds of places one by one and check if he has the highest score in the last 60 days for each place-- that sounds very inefficient.

Is there any SQL or algorithmic magic that could perform the task quickly?

UPDATE: I'm using MySQL and Django

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2024-09-25 16:41:55

我会将“当前专业”保留在位置表中,并不时更新。示例(我不知道数据模型是否正确):

drop table place;
create table place(name varchar(20) primary key, major varchar(20));
insert into place values('NY', null), ('LA', null);
create index idx_p_m on place(major);

drop table visits;
create table visits(user varchar(20), place varchar(20), points int, day int);
create index idx_v_p on visits(place, day desc);
insert into visits values
  ('Ben', 'NY', 1, 100), 
  ('Ben', 'LA', 3, 102), 
  ('Joe', 'NY', 2, 103), 
  ('Joe', 'LA', 1, 104);

-- just to prove this is efficient  
explain  select user from visits v where v.place = 'NY' 
  and day > 90
  group by user 
  order by sum(points) desc 
  limit 1;

update place p set major = 
  (select user from visits v where p.name = v.place 
  and day > 90
  group by user 
  order by sum(points) desc 
  limit 1);

select * from place where major = 'Joe';
select * from place where name = 'LA';

I would keep the 'current major' in the place table, and update that from time to time. Example (I have no idea if the data model is correct):

drop table place;
create table place(name varchar(20) primary key, major varchar(20));
insert into place values('NY', null), ('LA', null);
create index idx_p_m on place(major);

drop table visits;
create table visits(user varchar(20), place varchar(20), points int, day int);
create index idx_v_p on visits(place, day desc);
insert into visits values
  ('Ben', 'NY', 1, 100), 
  ('Ben', 'LA', 3, 102), 
  ('Joe', 'NY', 2, 103), 
  ('Joe', 'LA', 1, 104);

-- just to prove this is efficient  
explain  select user from visits v where v.place = 'NY' 
  and day > 90
  group by user 
  order by sum(points) desc 
  limit 1;

update place p set major = 
  (select user from visits v where p.name = v.place 
  and day > 90
  group by user 
  order by sum(points) desc 
  limit 1);

select * from place where major = 'Joe';
select * from place where name = 'LA';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文