我们想要设计一个可扩展的数据库。如果我们有 N 个用户,有 10 亿用户响应,那么下面的 2 个选项中哪一个是好的设计?我们希望根据 userID 和 Reponse ID 进行查询。
- 有 2 个集合,一个用于用户信息,另一个用于存储响应以及用户 ID。每个响应都存储为文档,因此我们将拥有 10 亿个文档。
User Collection { "userid" : "userid1", "password" : "xyz", , "City" : "New York", }, { "userid" : "userid2", "password" : "abc", , "City" : "New York", } responses Collection { "userid": "userid1", "responseID": "responseID1", "response" : "xyz" }, { "userid": "userid1", "responseID": "responseID2", "response" : "abc" }, { "userid": "userid2", "responseID": "responseID3", "response" : "mno" }
- 有 1 个集合来存储以下信息。每个响应都由一个新 key (responseIDX) 表示。
{ "userid" : "userid1", "responseID1" : "xyz", "responseID2" : "abc", , "responseN"; "mno", "city" : "New York" }
最佳答案
如果您使用第一个选项,我会使用关系数据库(例如 MySQL)而不是 MongoDB。如果您衷心使用 MongoDB,请充分利用它。
{
"userId": n,
"city": "foo"
"responses": {
"responseId1": "response message 1",
"responseId2": "response message 2"
}
}
至于哪种性能更好,请运行一些基准测试。
关于MongoDB 的可扩展性设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19204952/