{"action":"opened","issue":{"url":"https://api.github.com/repos/smallnewer/bugs/issues/21","labels_url":"https://api.github.com/repos/smallnewer/bugs/issues/21/labels{/name}","comments_url":"https://api.github.com/repos/smallnewer/bugs/issues/21/comments","events_url":"https://api.github.com/repos/smallnewer/bugs/issues/21/events","html_url":"https://github.com/smallnewer/bugs/issues/21","id":58227550,"number":21,"title":"[测试]索引大小","user":{"login":"smallnewer","id":1390940,"avatar_url":"https://avatars.githubusercontent.com/u/1390940?v=3","gravatar_id":"","url":"https://api.github.com/users/smallnewer","html_url":"https://github.com/smallnewer","followers_url":"https://api.github.com/users/smallnewer/followers","following_url":"https://api.github.com/users/smallnewer/following{/other_user}","gists_url":"https://api.github.com/users/smallnewer/gists{/gist_id}","starred_url":"https://api.github.com/users/smallnewer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smallnewer/subscriptions","organizations_url":"https://api.github.com/users/smallnewer/orgs","repos_url":"https://api.github.com/users/smallnewer/repos","events_url":"https://api.github.com/users/smallnewer/events{/privacy}","received_events_url":"https://api.github.com/users/smallnewer/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/smallnewer/bugs/labels/mongodb","name":"mongodb","color":"e11d21"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-02-19T16:03:01Z","updated_at":"2015-02-19T16:03:01Z","closed_at":null,"body":"## 测试1-用数字做索引\r\n\r\n代码：\r\n````javascript\r\nvar maxlen = 100 * 10000;\r\nvar arr = [];\r\nfor (var i = 0; i < maxlen; i++) {\r\n\tarr.push({\r\n\t\t_id: i,\r\n\t\tjjc: maxlen - i\r\n\t});\r\n};\r\ndb.a.insert(arr);\r\n````\r\n\r\n没插入完shell就崩了，没深究，插入了81万多点。_id的索引大小为`22868272`byte，约为21M，之后为jjc字段加了索引，大小为`20619872`byte。\r\n\r\n100万的文档数量，单键索引在30M以内的话，完全可以接受。之前网上有测试，100万数字做索引，索引要300M，有点难以接受。\r\n\r\n## 测试2-用字符串做索引\r\n\r\n````javascript\r\nvar maxlen = 100 * 10000;\r\nvar arr1 = [];\r\nfor (var i = 0; i < maxlen; i++) {\r\n\tarr1.push({\r\n\t\t_id: i + \"\",\r\n\t\tjjc: maxlen - i\r\n\t});\r\n};\r\ndb.b.insert(arr1);\r\n````\r\n\r\n这次仍然是没插入到100万，只有68万。_id索引大小为`31289552`byte，可以看出绝对比int大了。jjc仍然为`17177776`byte，和测试1是能对应上的。\r\n\r\nObjectId就不测试了，在项目中没有特别的用处，在客户端中还得包装成类，不如干脆用自增的int代替。节省热数据占用内存的大小。"}}