MySQL版本:5.7.13
MongoDB版本:3.2
操作系统:Windows server 2008 R2
内存:8G
python 2.7.11
本人MongoDB萌新一枚,用python分别写了一个循环插入的测试脚本,数据量为30万
MongoDB:
from pymongo import MongoClient
import time
def get_db():
#建立连接
client = MongoClient("localhost", 27017)
#test,还有其他写法
db = client.test
print "建立MongoDB数据库连接"
return db
def get_collection(db):
#选择集合
collection = db['test']
print "连接数据库:test"
return collection
def insert(collection):
i=0
f = open("phonenumbers.txt")
f1=open("result_mongo.txt","w") # 返回一个文件对象
line = f.readline() # 调用文件的 readline()方法
#print line,
start=time.clock()
while line:
user = {"name":"%s"%(line.strip('\n'))}
collection.insert(user)
line = f.readline()
i=i+1
if i%30000==0:
end = time.clock()
print "%f: %f s" % (i,end - start)
f1.write("%f条记录用时:%f s \n"%(i,end - start))
print "%f: %f s" % (i,end - start)
print 'task over'
f.close()
f1.close()
db=get_db()
collection=get_collection(db)
insert(collection)
MySQL:
#Mysql
conn = MySQLdb.connect(host='localhost',port = 3306, user='root',passwd='root',db ='test',charset='utf8')
cursor = conn.cursor()
print 'connect Mysql success!'
i=0
f = open("phonenumbers.txt")
f1=open("result.txt","w") # 返回一个文件对象
line = f.readline() # 调用文件的 readline()方法
#print line,
start=time.clock()
while line:
#print line.strip('\n')
sql_content = """insert into t_phone(phone_number) values('%s')"""%(line.strip('\n'))
#print sql_content
cursor.execute(sql_content.decode('utf8').encode('gb18030'))
# print(line, end = '') # 在 Python 3中使用
i=i+1
if i%30000==0:
end = time.clock()
print "%f: %f s" % (i,end - start)
f1.write("%f条记录用时:%f s \n"%(i,end - start))
line = f.readline()
print 'task over'
f.close()
f1.close()
conn.commit()
cursor.close()
conn.close()
运行时间如下:
MySQL
30000.000000: 5.953894 s
60000.000000: 11.355339 s
90000.000000: 16.826594 s
120000.000000: 22.311345 s
150000.000000: 27.833271 s
180000.000000: 33.445231 s
210000.000000: 38.899494 s
240000.000000: 44.386738 s
270000.000000: 49.829280 s
300000.000000: 55.298867 s
MongoDB
30000.000000: 17.713415 s
60000.000000: 35.223699 s
90000.000000: 52.518638 s
120000.000000: 69.901784 s
150000.000000: 87.370721 s
180000.000000: 105.004178 s
210000.000000: 122.643773 s
240000.000000: 140.226097 s
270000.000000: 157.490818 s
300000.000000: 175.007099 s
各位大神这是怎么回事?