验证MongoDB中$set、$inc、$push几种修改器操作的速度
验证MongoDB中$set、$inc、$push几种修改器操作的速度
在MongoDB中,$set、$inc、$push都能够对数据库进行操作,那么到底哪种修改器操作速度更快呢?我们今天来验证一下。
先描述一下本人的实验环境:
Win8操作系统64位,内装虚拟机32位系统,CentOS5.5
实验第一步,安装并启动MongoDB数据库服务器:
[plain] [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,文件内容如下:
[plain] #!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次
[plain] [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修改器。