sabina199 2016-10-20 08:32 采纳率: 0%
浏览 2155

【求教】mongoDB插入速度怎么比MySQL还慢

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

各位大神这是怎么回事?
  • 写回答

1条回答

  • oyljerry 2016-10-20 08:51
    关注

    mongodb引擎是否用的wt。最好是linux上来测试。
    同时,还要再做一下profile,看具体时间花费在哪一步

    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题