简体   繁体   English

PHP mongoDB 从百万条记录中过滤计数工作非常缓慢

[英]PHP mongoDB get filtered count from million records is working very slow

I am using below PHP code to get count of total entries in MongoDB我正在使用以下 PHP 代码来获取 MongoDB 中的总条目数

$command = new \MongoDB\Driver\Command(['count' => 'log_master', 'query' => $filter]);
    try {
        $cursor = $m->executeCommand('DB_name', $command);
    } catch (\MongoDB\Driver\Exception\Exception $e) {
        $error_message = $e->getMessage();
    }

$count = $cursor->toArray()[0]->n;

it works fine but takes too much time if filters are on two or three fields.它工作正常,但如果过滤器在两个或三个字段上需要太多时间。 Records in table are around 50 millions.表中的记录在5000万左右。 How I can speed up count query我如何加快计数查询

Most probably you need to start indexing your data.很可能您需要开始索引数据。 Here are official docs and a tutorial .这里是官方文档教程 To see how your query is being executed you may employ explain() method in shell .要查看您的查询是如何执行的,您可以在 shell 中使用explain()方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM