Python 验证MongoDB中$set、$inc、$push几种修改器操作的速度
在MongoDB中,$set、$inc、$push都能够对数据库进行操作,那么到底哪种修改器操作速度更快呢?我们今天来验证一下。
先描述一下本人的实验环境:
Win8操作系统64位,内装虚拟机32位系统,CentOS5.5
实验第一步,安装并启动MongoDB数据库服务器:
在CentOS下如何安装MongoDB数据库如果有疑问的请猛击如下链接http://blog.csdn.net/yima1006/article/details/9840239
[root@h3 home]# mongod -f /etc/mongod.conf
Thu Jul 25 00:19:15.950
Thu Jul 25 00:19:15.950 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Jul 25 00:19:15.950
about to fork child process, waiting until server is ready for connections.
forked process: 14519
all output going to: /var/log/mongo/mongod.log
child process started successfully, parent exiting
[root@h3 home]#
实验第二步,编辑测试文件,这里我们保存为test.py,文件内容如下:
#!usr/bin/python
#coding=utf-8
from pymongo import Connection
import time
db = Connection().performance_test
db.drop_collection("updates")
collection = db.updates
collection.insert({"x": 1})
collection.find_one()
# in inc test
start_inc = time.time()
for i in range(100000):
collection.update({}, {"$inc" : {"x" : 1}})
collection.find_one()
print '$inc:', time.time() - start_inc
# in set test
start_set = time.time()
for i in range(100000):
collection.update({}, {"$set" : {"x" : 1}})
collection.find_one()
print '$set:', time.time() - start_set
# in push test
start_push = time.time()
for i in range(100000):
collection.update({}, {"$push" : {"x" : 1}})
collection.find_one()
print '$push:', time.time() - start_push
实验第三步,我们运行测试test.py,这里我们运行5次
[root@h3 home]# python test.py
$inc: 6.83732295036
$set: 6.61989808083
$push: 10.5028648376
[root@h3 home]# python test.py
$inc: 6.56225991249
$set: 6.12179207802
$push: 10.264081955
[root@h3 home]# python test.py
$inc: 6.18113183975
$set: 6.4468960762
$push: 10.077311039
[root@h3 home]# python test.py
$inc: 6.19089698792
$set: 6.61814904213
$push: 9.5483288765
[root@h3 home]# python test.py
$inc: 6.93615078926
$set: 6.58752202988
$push: 9.91513586044
[root@h3 home]#
结论:在MongoDB中,$inc、$set 修改器的操作速度相差不大,但明显快于$push修改器。
分享到:
相关推荐
【Python高级开发课程 高级教程】课程列表 01 Python语言开发要点详解....07 Python操作MongoDB数据库.pptx 08 Python多线程 多进程开发.pptx 09 Python爬虫框架Scrapy实战.pptx 10 Python Web开发框架Django实战.pptx
本教材的内容结合了开发中的经验,并参考了《Python数据库编程》《Python和MongoDB开发指南》《Python数据库编程指南》等书,力求给读者一个清晰简单的示范案例,主要包括MongoDB和传统型数据库对比,MongoDB的常规...
python操作mongodb的密码强度验证.pdf
Python MongoDB 应用开发,构建高效稳定数据库应用系统
通过python语言mongodb数据库惊醒操作,包括连接、上传、下载等操作
python使用mongoDB
MongoDB的$type操作符.pdf 学习资料 复习资料 教学资源
通过python启动mongodb,动态灵活设置mongodb.conf文件,并且实现储存数据和log在程序文件夹下,
对于文档的更新除替换外,针对某个或多个文档只需要部分更新可使用原子的更新修改器,能够高效的进行文档更新。更新修改器是中特殊的键
python实现连接mongodb并执行相关操作,添加数据,并查询数据
Python + MongoDB开发的百度云爬虫.zip Python + MongoDB开发的百度云爬虫.zipPython + MongoDB开发的百度云爬虫.zipPython + MongoDB开发的百度云爬虫.zipPython + MongoDB开发的百度云爬虫.zipPython + MongoDB...
本文实例讲述了Python操作mongodb数据库进行模糊查询操作。分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- import pymongo import re from pymongo import MongoClient #创建连接 #10.20.66.106 client...
MongoDB基本操作之Python篇 连接数据库 MongoClient VS Connection
mongodb 数据库基本操作 1、mongoDB的介绍 mongoDB是一个基于分布式文件存储的数据库,由 C++ 语言编写,旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。它介于关系数据库和非关系数据库之间,被认为是非...