2

我有以下数据模型:

class StadiumOccupant {
  String stadiumname;
  String username;
}

class Friend {
  String username;
  String friendname;
}

我想在体育场找到所有也是我朋友的用户。我可以这样做:

List<String> friendsAtStadium;
String stadiumId = 'xyz';

List<Friend> friends = select from Friend where username = 'me';
for (Friend friend : friends) {
  List<StadiumOccupant> friendAtStadium = 
    select from StadiumOccupant where 
    stadiumname = stadiumId and
    username = friend.friendname;
  friendsAtStadium.addAll(friendAtStadium);
}

这可以处理非常少量的对象。一旦用户拥有多个朋友,这将不起作用。应用引擎上有解决方案吗?我想不出一个表现好的。

谢谢

-------- 一些样本数据--------

Friend { john, tim }
Friend { john, mary }
Friend { mary, frank }

StadiumOccupant { abc, tim }
StadiumOccupant { abc, frank }
StadiumOccupant { abc, kim }

所以如果我是用户 'john',我的朋友是:{ tim, mary }。如果我使用 Stadiumname='abc' 运行体育场查询,我应该返回 { tim }。

4

1 回答 1

2

对于少量数据,你可以按照你的建议去做。对于大量数据,您无论如何都不会进行连接,即使您使用 SQL 数据库作为后端。

您的问题几乎是典型的社交网络数据问题。最有可能的是,您将希望对数据进行非规范化,并在用户更新其状态时将信息“推送”到所有用户的朋友列表,或者可能像您在上面所做的那样将其拉出。最佳解决方案取决于数据的形状以及要优化的查询类型 - 检索或更新。

有关更多信息,请参阅我的答案,包括 Facebook 工程师的帖子。

于 2010-08-11T19:54:44.730 回答