在PHP mongodb上使用不同的操作速度较慢
我正在使用mongoDB来存储用户的日志。在我的实时报告中,我需要统计特定类型中表的不同用户。开始时,它运行得很快,但当桌子变大时,速度会变慢 以下是我使用的代码:在PHP mongodb上使用不同的操作速度较慢,php,performance,mongodb,Php,Performance,Mongodb,我正在使用mongoDB来存储用户的日志。在我的实时报告中,我需要统计特定类型中表的不同用户。开始时,它运行得很快,但当桌子变大时,速度会变慢 以下是我使用的代码: $connection = new MongoClient(); $result = $collection->distinct('user', array('type' => $type, 'ctime' => array('$gte' => $start))); $total = count($resul
$connection = new MongoClient();
$result = $collection->distinct('user', array('type' => $type, 'ctime' => array('$gte' => $start)));
$total = count($result);
$total
是唯一用户的总数
有人能建议我如何改进查询以获得更好的性能吗?
非常感谢。使用
$collection->ensureIndex(数组('user'=>1))
在用户字段上创建索引。您的文档结构在哪里?另外,在shell版本中键入查询,这样人们可以更快地测试它。文档结构是:array('user','type','ctime')这不是一个文档结构,我们的意思是你的行看起来像什么,还有你区分了多少行对不起,我用PHP创建了行,行看起来像{“allen”,“type1”,1378375980}{“allen”,“type1”,1378375980}{“john”,“type2”,1378895340}在用户上有索引吗?你区分了多少行?