gpt4 book ai didi

php - MongoDB $lookup 的结果非常慢

转载 作者:行者123 更新时间:2023-12-03 16:00:08 28 4
gpt4 key购买 nike

我正在使用 PHP 与 MongoDB 交互,最近我在 5 个集合上使用了 $lookup ,我的查询开始需要大约 20-25 秒才能获得结果。我所有的收藏都有测试数据,不超过30-40条。我还使用了分析,但收到消息:“查询未记录(太大)”

我正在分享我的 PHP 文件中的代码。我还尝试在字段上创建索引,该索引在集合之间创建联接,但得到了相同的结果。请告诉我创建集合结构和从 MongoDB 检索数据的最佳方法。

提前致谢。

    $collection = new MongoCollection($db, 'col_main');
$condition = array();
$condition[] = array(
'$lookup' => array(
"from" => "col_a",
"localField" => "a",
"foreignField" => "_id",
"as" => "col_a"
)
);
$condition[] = array(
'$lookup' => array(
"from" => "col_b",
"localField" => "b",
"foreignField" => "_id",
"as" => "col_b"
)
);

$condition[] = array(
'$lookup' => array(
"from" => "col_c",
"localField" => "c",
"foreignField" => "_id",
"as" => "col_c"
)
);

$condition[] = array(
'$lookup' => array(
"from" => "col_e",
"localField" => "e",
"foreignField" => "_id",
"as" => "col_e"
)
);

$condition[] = array(
'$lookup' => array(
"from" => "col_f",
"localField" => "f",
"foreignField" => "_id",
"as" => "col_f"
)
);

$condition[] = array(
'$project' => $projection
);

$condition[] = array (
'$unwind' => '$col_a'

);

$condition[] = array (
'$unwind' => '$col_a.translation'

);

$condition[] = array (
'$unwind' => '$col_b'

);

$condition[] = array (
'$unwind' => '$col_b.translation'

);

$condition[] = array (
'$unwind' => '$col_c'

);

$condition[] = array (
'$unwind' => '$col_c.translation'

);

$condition[] = array (
'$unwind' => '$col_e'

);

$condition[] = array (
'$unwind' => '$col_e.staff_name'

);

$condition[] = array (
'$unwind' => '$col_e.staff_surname'

);

$condition[] = array (
'$unwind' => '$col_f'

);

$condition[] = array (
'$unwind' => '$col_f.translation'

);


//Some More conditions

$condition[] = array(
'$match' => array( 'col_b.translation.language' => $val )
);
$condition[] = array(
'$match' => array( 'col_a.translation.language' => $val )
);
$condition[] = array(
'$match' => array( 'col_c.translation.language' => $val )
);

$condition[] = array(
'$match' => array( 'col_f.translation.language' => $val )
);


return $collection->aggregate($condition);

最佳答案

当您使用许多展开操作来乘以对象时,这可能会产生大量数据,每个 $unwind muliplayes documnet 集,因此可能会产生巨大的内存消耗,甚至交换。

如果我们有 3 个数组,每个数组有 3 个元素,那么我们将对 a、b、c 执行展开我们将得到:

  • 第一步后有 3 个文档,
  • 第 2 步后有 9 个文档
  • 最后一步后有 27 个文档..

关于php - MongoDB $lookup 的结果非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37485432/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com