MongoDB 子文档数组上的多个索引与单个索引?
想知道哪种技术对我需要跟踪的文档的各种时间戳进行索引是更有效的技术,请记住我的应用程序在写入方面相当繁重,但在阅读方面又足够繁重,如果没有索引,查询就会太慢。
是为每个时间戳设置一个字段并为每个字段建立索引,还是将时间戳及其关联类型存储在数组字段中,并为该数组的每个字段建立索引?
第一个选项,单独的字段,每个字段都有一个索引:
{
"_id" : "...",
"Field1.Timestamp" : '2011-01-01 01:00.000',
"Field2.Timestamp" : '2011-01-01 01:00.000',
"Field3.Timestamp" : '2011-01-01 01:00.000',
"Field4.Timestamp" : '2011-01-01 01:00.000',
"Field5.Timestamp" : '2011-01-01 01:00.000',
"Field6.Timestamp" : '2011-01-01 01:00.000',
"Field7.Timestamp" : '2011-01-01 01:00.000',
"Field8.Timestamp" : '2011-01-01 01:00.000',
"Field9.Timestamp" : '2011-01-01 01:00.000',
}
db.mycollection.ensureIndex({ "Field1.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field2.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field3.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field4.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field5.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field6.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field7.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field8.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field9.Timestamp" : 1 });
然后有一个时间戳及其状态的数组,只有一个索引
{
"_id" : "...",
"Timestamps" : [
{ "Type" : "Field1", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field2", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field3", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field4", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field5", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field6", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field7", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field8", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field9", "Timestamp" : '2011-01-01 01:00.000' },
]
}
db.mycollection.ensureIndex({ "Timestamps.Type" : 1, "Timestamps.Timestamp" : 1 });
我在这里离题了吗?或者哪一个是更好的方法
Wondering which would be the more efficient technique for indexing my document's various timestamps that I need to keep track of, keeping in mind my application is fairly heavy on writing, but heavy enough on reading that without the indexes, the queries are too slow.
Is it better to have a field for each timestamp, and index each field, or store the timestamps and their associated type in an array field, and index each field of that array?
First option, separate fields, and an index for each:
{
"_id" : "...",
"Field1.Timestamp" : '2011-01-01 01:00.000',
"Field2.Timestamp" : '2011-01-01 01:00.000',
"Field3.Timestamp" : '2011-01-01 01:00.000',
"Field4.Timestamp" : '2011-01-01 01:00.000',
"Field5.Timestamp" : '2011-01-01 01:00.000',
"Field6.Timestamp" : '2011-01-01 01:00.000',
"Field7.Timestamp" : '2011-01-01 01:00.000',
"Field8.Timestamp" : '2011-01-01 01:00.000',
"Field9.Timestamp" : '2011-01-01 01:00.000',
}
db.mycollection.ensureIndex({ "Field1.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field2.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field3.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field4.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field5.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field6.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field7.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field8.Timestamp" : 1 });
db.mycollection.ensureIndex({ "Field9.Timestamp" : 1 });
Then there's an array of the timestamps and their status, with only a single index
{
"_id" : "...",
"Timestamps" : [
{ "Type" : "Field1", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field2", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field3", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field4", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field5", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field6", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field7", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field8", "Timestamp" : '2011-01-01 01:00.000' },
{ "Type" : "Field9", "Timestamp" : '2011-01-01 01:00.000' },
]
}
db.mycollection.ensureIndex({ "Timestamps.Type" : 1, "Timestamps.Timestamp" : 1 });
Am I way off the mark here? or which would be the better way
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这基本上可以归结为,10 个大小为 N 的索引是否比一个大小为 N * 10 的索引更有效。如果您纯粹考虑读取,那么单独的索引应该总是更快。相关的 b 树遍历将检查较小的键集等。
不过,有几点需要考虑:
This basically boils down to if 10 index of size N are more efficient than one index of size N * 10. If you purely look at reads then the seperate indexes should always be faster. The associated b-tree walks will examine a smaller keyset etc.
There are a couple of points to consider though :