【发布时间】:2017-07-09 22:12:06
【问题描述】:
问题:
我的数据库中有两个集合:
// UnifiedPost
public class UnifiedPost
{
public UnifiedPost() { Id = ObjectId.GenerateNewId(); }
[BsonId]
public ObjectId Id { get; set; }
public int OriginalId { get; set; }
public string Thumbnail { get; set; }
public string Preview { get; set; }
[AlsoNotifyFor("IsVideo")]
public string FullSized { get; set; }
public char Rating { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public string Dimensions { get { return string.Format("{0}x{1}", Width, Height); } }
public int Score { get; set; }
[MongoDB.Bson.Serialization.Attributes.BsonIgnore]
public string Tags { get; set; }
public ICollection<ObjectId> TagIds { get; set; }
public char Site { get; set; }
public bool IsVideo { get { return FullSized.Contains(".mp4") || FullSized.Contains(".webm"); } }
public string UniversalId { get; set; } // UNIQUE index (Site + Original id; fe. S3001)
}
// Tag
public class Tag
{
public Tag() { }
public ObjectId Id { get; set; }
public string Name { get; set; }
}
截至目前,Posts 集合中约有 400 万个条目。 db.Posts.find()、db.Posts.find( { "_id": ObjectId(xyz) } ) 和 db.Posts.find( { "UniversalId": "S3001" } ) 等查询会在毫秒内返回结果。
另一方面,像 .find( { "Site": "S" } ) or.find( { "OriginalId": {$lt: "25" } })` 这样的查询可能需要 30 秒才能完成。
我已经拥有的:
我正在使用扩展方法向UniversalId 属性添加/更新唯一键:
if (!createIndex)
{
await postsColl.Indexes.AddOrUpdateAsync(new CreateIndexOptions() { Unique = true }, new IndexKeysDefinitionBuilder<UnifiedPost>().Ascending(o => o.UniversalId));
createIndex = true;
}
我不确定的:
- 我不知道我是否应该为任何其他字段使用索引,以及我应该使用什么类型的索引。
-
Site拥有一个char值,到目前为止,该集合中有大约 6 种可能性。它可能会或可能不会进一步增加。 *OriginalId只是一个int,代表原始网站上的帖子。此 ID 不是唯一的,因为集合中存在不同的站点,1s、2s、3s(等等)的数量大致相同。
优化数据库的最佳方法是什么,以便我可以在合理的操作时间内根据这些属性查询帖子?
【问题讨论】:
标签: c# .net mongodb optimization database