Mysql 优化有说服力的关系检索
我有一个界面,显示平台上的社区列表。社区有成员,反过来,成员/个人资料可以彼此成为朋友。在列表页面上,每个社区卡都需要显示这些成员的成员数(社区中)和朋友数(登录档案中的朋友) 这是一张社区卡的图片 我首先要让社区成员了解:Mysql 优化有说服力的关系检索,mysql,laravel,eloquent,relationship,Mysql,Laravel,Eloquent,Relationship,我有一个界面,显示平台上的社区列表。社区有成员,反过来,成员/个人资料可以彼此成为朋友。在列表页面上,每个社区卡都需要显示这些成员的成员数(社区中)和朋友数(登录档案中的朋友) 这是一张社区卡的图片 我首先要让社区成员了解: $communities = $loggedInProfile->communities->load('members')->take(15); 然后遍历社区和成员,找出哪些社区是登录用户的朋友 foreach ($communities as
$communities = $loggedInProfile->communities->load('members')->take(15);
然后遍历社区和成员,找出哪些社区是登录用户的朋友
foreach ($communities as $key => $community) {
$friends = [];
foreach ($community->members as $member) {
if ($loggedInProfile->isFriendWith($member)) {
array_push($friends, $member);
}
}
$community->members_who_are_friends = $friends;
}
我的问题是,当关联变大时,就查询数量而言,这是非常费力的。有没有更好的方法来检索这些关系而不必使用嵌套for循环?我还使用Elasticsearch为所有数据编制索引。使用Elasticsearch进行这种检索会更好吗?这也是hasThrough
的一个很好的用例吗
更新
成员
关系:
public function members()
{
return $this->belongsToMany('App\Profile', 'community_members', 'community_id', 'profile_id')->withTimestamps();
}
isFriendWith
关系:
public function isFriendWith(Model $recipient)
{
return $this->findFriendship($recipient)->where('status', Status::ACCEPTED)->exists();
}
检查是在名为友谊
的表上完成的。检查状态
列(可以是0或1)以查看是否有朋友
findFriendship
检查:
private function findFriendship(Model $recipient)
{
return Friendship::betweenModels($this, $recipient);
}
数据库结构:
-配置文件迁移
Schema::create('profiles', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
Schema::create('community_members', function (Blueprint $table) {
$table->primary(['profile_id', 'community_id']);
$table->unsignedInteger('profile_id');
$table->foreign('profile_id')->references('id')->on('profiles');
$table->unsignedInteger('community_id');
$table->foreign('community_id')->references('id')->on('communities');
$table->timestamps();
});
Schema::create('friendships'), function (Blueprint $table) {
$table->increments('id');
$table->morphs('sender');
$table->morphs('recipient');
$table->tinyInteger('status')->default(0);
$table->timestamps();
});
-社区迁移(外键是社区的所有者
)
-社区成员移徙
Schema::create('profiles', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
Schema::create('community_members', function (Blueprint $table) {
$table->primary(['profile_id', 'community_id']);
$table->unsignedInteger('profile_id');
$table->foreign('profile_id')->references('id')->on('profiles');
$table->unsignedInteger('community_id');
$table->foreign('community_id')->references('id')->on('communities');
$table->timestamps();
});
Schema::create('friendships'), function (Blueprint $table) {
$table->increments('id');
$table->morphs('sender');
$table->morphs('recipient');
$table->tinyInteger('status')->default(0);
$table->timestamps();
});
-友谊移民
Schema::create('profiles', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
Schema::create('community_members', function (Blueprint $table) {
$table->primary(['profile_id', 'community_id']);
$table->unsignedInteger('profile_id');
$table->foreign('profile_id')->references('id')->on('profiles');
$table->unsignedInteger('community_id');
$table->foreign('community_id')->references('id')->on('communities');
$table->timestamps();
});
Schema::create('friendships'), function (Blueprint $table) {
$table->increments('id');
$table->morphs('sender');
$table->morphs('recipient');
$table->tinyInteger('status')->default(0);
$table->timestamps();
});
在你的队伍中:
$communities = $loggedInProfile->communities->load('members')->take(15);
load()
用于执行延迟加载,即在检索社区后加载成员,从而为每个社区生成不同的查询。您可以使用with()
通过单个查询提取整个数据。另外,take(15)
对结果集合而不是查询执行。试试这个:
$communities = $loggedInProfile->communities()->with('members')->take(15)->get();
在你的队伍中:
$communities = $loggedInProfile->communities->load('members')->take(15);
load()
用于执行延迟加载,即在检索社区后加载成员,从而为每个社区生成不同的查询。您可以使用with()
通过单个查询提取整个数据。另外,take(15)
对结果集合而不是查询执行。试试这个:
$communities = $loggedInProfile->communities()->with('members')->take(15)->get();
你能给我看看getMutualFriends函数吗?您在那里使用的$member字段是什么?很抱歉,您使用了错误的友谊支票。更新了问题。成员是我正在迭代的社区的成员。我正在遍历社区中的members()关系。请向我显示
函数isFriendWith($member)
,或者告诉我$member的w/c字段,您是否使用它来知道它是否相互更新了问题。member是用户权限的实例?和loggedUser一样..你能给我看看函数getMutualFriends吗?您在那里使用的$member字段是什么?很抱歉,您使用了错误的友谊支票。更新了问题。成员是我正在迭代的社区的成员。我正在遍历社区中的members()关系。请向我显示函数isFriendWith($member)
,或者告诉我$member的w/c字段,您是否使用它来知道它是否相互更新了问题。member是用户权限的实例?与loggedUser相同。我尝试了两种方法,但它似乎不会影响正在运行的查询数:/。我尝试了两种方法,但它似乎不会影响正在运行的查询数:/。