【发布时间】:2013-07-25 03:15:59
【问题描述】:
我有一个名为 Post 的集合。我有一个映射系统,它始终确保每个文档都有这些字段:
- id (int)
- 目标(字符串)
- 类型(字符串)
- user_id
- client_id
- 已更新(字符串,11 个整数时间戳)
- 已创建(字符串,11 个 int 时间戳)
- 启用(布尔)
访问此集合以在 API 架构中输出。
所以一些典型的请求可能是:
/post?type=image&user_id=2
/post?updated=35234423&order_by=client_id
/post?enabled=true&order_by=id
没有 100% 保证某些字段会进入查找或排序字段。
最近当表达到 8GB 数据时,我开始收到此错误:
"localhost:27017: too much data for sort() with no index. add an index or specify a smaller limit"
我查看了 Mongo 索引的文档,发现很难理解它是否以与 MySQL 索引相同的方式工作。
我在索引中发现的一些线程:MongoDB - too much data for sort() with no index error 似乎建议使用特定的排序字段来确保索引被命中。显然,当我的很多过滤和排序是可选的时,我不能这样做。
就我是否应该索引表中的所有字段而言,谁能提出一个可靠的解决方案?
感谢大家的反馈,我已经开始构建自动索引功能了:
public function get() {
$indices['Post'] = array(
'fields' =>
array(
'id' => array('unique' => true, 'dropDups' => true, 'background' => true),
'client_id' => array('dropDups' => true, 'background' => true),
'image_id' => array('dropDups' => true, 'background' => true),
'user_id' => array('dropDups' => true, 'background' => true),
'publish_target' => array('dropDups' => true, 'background' => true),
'type' => array('dropDups' => true, 'background' => true),
'status' => array('dropDups' => true, 'background' => true),
'text' => array('background' => true)
)
);
foreach ($indices as $key => $index) {
/* set the collection */
$collection = $this->mongoDB->{$key};
/* delete the indexes */
$collection->deleteIndexes();
/* loop the fields and add the index */
foreach ($index['fields'] as $subKey => $data) {
$collection->ensureIndex($subKey, array_merge($data, array('name' => $subKey)));
}
}
/* return the list */
return $indices;
}
【问题讨论】:
-
您能分享您的疑问吗?对分析有帮助吗?
-
@user10 如果您向下看大约一半,他会分享他的 quireies 示例;这就是jira.mongodb.org/browse/SERVER-3071 真正有用的地方