mongoDB的查詢測試

做的事情:

分析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é)果截圖:


可以看出來,有索引的情況確認(rèn)會比沒有縮影的情況在查詢時間上還一點,但是沒有那么突出的效果。

而我冗余的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ī)器上已經(jīng)有了mongoDB數(shù)據(jù)庫。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,837評論 18 139
  • 目錄 查詢操作 集合查詢方法 find() 查詢內(nèi)嵌文檔 查詢操作符(內(nèi)含 數(shù)組查詢) "$gt" 、"$gte"...
    彩虹之夢閱讀 1,042評論 0 1
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,925評論 6 342
  • 1. MongoDB命令幫助系統(tǒng) 2. 基本命令及實例 一基本命令 二基本DDL和DML 三啟動與終止 四安全管理...
    ZGKzm閱讀 401評論 0 2
  • NoSql數(shù)據(jù)庫優(yōu)缺點 在優(yōu)勢方面主要體現(xiàn)在下面幾點: 簡單的擴(kuò)展 快速的讀寫 低廉的成本 靈活的數(shù)據(jù)模型 在不足...
    dreamer_lk閱讀 2,765評論 0 6