做的事情:
分析mongodb的查詢性能伟阔,引入冗余的字段來加快查詢的效率是否可行尔觉?同時對比了冗余字段和索引的查詢能力皮璧。
我們從下面幾個維度來分析
????????1. 存儲空間
????????2. 時間
怎么做:
????????1. 生成數(shù)據(jù)
????????2. 分兩類來考察
????????????????a. 冗余一定的數(shù)據(jù)來加快數(shù)據(jù)本身的子父級關(guān)系
????????????????b. 建立索引的方式
????????3. 統(tǒng)計數(shù)據(jù)庫的空間成本版述,查詢花費的時間成本
舉例:
1. 數(shù)據(jù)結(jié)構(gòu)如下:
????????{"id":1, "data":"adasdasd","parent":2}
????????{"id":2, "data":"adasdasd","parent":null}
2. 數(shù)據(jù)結(jié)構(gòu)如下:
? ? ? ? {"id":1 , "data":"asdasdasd","children":[2,3],"parent":null}
? ? ? ? {"id":2 , "data":"asdasdasd","children":[],"parent":2}
我們要查詢的數(shù)據(jù)是:將一條數(shù)據(jù)的子數(shù)據(jù)都查詢出來
第一種查詢方式
????????db.test1.find({"parent":2})
第二種查詢方式:
????????db.test2.find({"$in":{"_id":[2,3,4]}})
第三種查詢方式和第一種一樣几蜻,只是第三種多了一個parent_id 的索引
1. 首先我們先要創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)
import ?random
defget_id(db):
????????key = db.seq.find_and_modify(
????????????????query={'name':'mongo_test'},
????????????????update={'$inc': {'seq':1}}
????????)
????????if notkey:
????????????????db.seq.insert({"name":"mongo_test","seq":1})
????????????????return 1
????????returnkey['seq']
def init_data(client):
????????""" 構(gòu)造數(shù)據(jù) """
????????db1 = client["mongo_test1"]
????????db2 = client["mongo_test2"]
? ? ? ? for i in xrange(100000):
????????????????key1 = get_id(db1)
????????????????parent_id = None
????????????????if random.random() > 0.2:
????????????????????????parent_id = random.randint(1, i + 1)
? ? ? ? ? ? ? ? data = {"_id": key1, "data": "And loved your beauty with love false or true {0}".format(i),"parent_id": parent_id}
????????????????db1.mongo_test.insert(data)
????????????????key2 = get_id(db2)
? ? ? ? ? ? ? ? data2 = {"_id": key2, "data": "And loved your beauty with love false or true {0}".format(i), "children_id": [],"parent_id": parent_id}
????????????????db2.mongo_test.insert(data2)
????????????????if parent_id:
????????????????????????parent = db2.mongo_test.find_one({"_id": parent_id})
? ? ? ? ? ? ? ? ? ? ? ? if parent and parent.get("parent_id") is None:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? children = parent.get("children_id", [])
????????????????????????????????children.append(key2)
????????????????????????????????db2.mongo_test.save(parent)
????????????????if i % 10000 == 0:
????????????????????????print "{0}0 % ".format(i // 10000)
這個方法就是在創(chuàng)建數(shù)據(jù)铸本,數(shù)據(jù)量在100000條肮雨。
2.? 數(shù)據(jù)生成完了之后,我們需要到mongo客戶端copy一個數(shù)據(jù)出來箱玷,命名為mong_test3怨规,這個數(shù)據(jù)庫和mong_test1數(shù)據(jù)庫唯一的區(qū)別在于,我們會再mong_test3中添加一個parent_id 的索引
db.copyDatabase('mongo_test1', 'mongo_test3', '127.0.0.1');
use mongo_test3
db.mongo_test.ensureIndex( { 'patent_id' : 1 } )
3. 上兩步完成之后就開始查詢數(shù)據(jù)锡足,并且計算查找時間
def test_db(client):
"""測試花費的時間"""
????????db1 = client["mongo_test1"]
????????import time
????????start = time.time()
????????parent = db1.mongo_test.find({"parent": None})
????????for item in parent:
????????????????children = db1.mongo_test.find({"parent_id": item.get("_id")})
? ? ? ? end = time.time()
????????test_db1_time = end - start
????????db2 = client["mongo_test2"]
????????start = time.time()
????????parent = db2.mongo_test.find({"parent": None})
????????for item in parent:
????????????????if item.get("children_id"):
????????????????????????children = db2.mongo_test.find({"_id": {"$in": item.get("children_id",[])}})
????????end = time.time()
????????test_db2_time = end - start
????????db3 = client["mongo_test3"]
????????start = time.time()
????????parent = db3.mongo_test.find({"parent": None})
????????for item in parent:
????????????????children = db3.mongo_test.find({"parent_id": item.get("_id")})
????????end = time.time()
????????test_db3_time = end - start
????????print "第一種方式花費時間:{0} \n" \
????????????????"第二種方式花費時間:{1} \n" \
????????????????"第三種方式花費時間(parent建立索引):{2}".format(test_db1_time, test_db2_time, test_db3_time)
4. 最后展示一張我查找的結(jié)果截圖:
可以看出來波丰,有索引的情況確認會比沒有縮影的情況在查詢時間上還一點,但是沒有那么突出的效果舶得。
而我冗余的children_id 數(shù)組卻帶來了4倍的查詢速度的提升 呀舔,可以說是效果非常的明顯了。
5. 我們來查看一下他們空間大欣┑啤:
查詢后發(fā)現(xiàn)媚赖,他們的區(qū)別在這個10W的數(shù)據(jù)量下表現(xiàn)不出來。
6. 主函數(shù):
if __name__ == '__main__':
????????from pymongo import MongoClient
????????client = MongoClient(host='127.0.0.1', port=27017)
????????init_data(client)
????????# test_db(client)
注意:
在我們執(zhí)行添加數(shù)據(jù)操作的時候珠插,會報兩次錯誤惧磺,這是由于數(shù)據(jù)庫數(shù)據(jù)引起的。不用在意捻撑,點擊執(zhí)行就好了磨隘,大概要點擊三次缤底,在第三次的時候就不會再報錯了。
總結(jié):
這是一個典型的用空間換時間的例子番捂。
我們這里還沒有去考慮建立索引帶來了插入和刪除的性能損耗的問題个唧,只是單純的做了一次查找的性能對比。
至此设预,在這個10W的數(shù)據(jù)量下的查詢?nèi)哂嘧侄瓮陝倨胀ǚ椒ê退饕?/p>
完整的代碼就是將上面的代碼依次放入py文件中徙歼,就可以執(zhí)行了。前提是本地機器上已經(jīng)有了mongoDB數(shù)據(jù)庫鳖枕。