数据量 5W
方案1:每次都创建一个链接
for i in range(50000):
conn,cursor=newConnect()
conn.close()
cursor.close()
耗时:6分21秒
方案2: 共享一个链接
conn,cursor=newConnect()
for i in range(50000):
conn,cursor=newConnect()
conn.close()
cursor.close()
耗时: 1分45秒
方案3: 使用连接池poolDB
pool = PooledDB(MySQLdb, maxcached=4, # 连接池中最大空闲连接数 maxshared=8, # 允许的最大共享连接数 maxconnections=maxconnections, host=host, user=username, port=port, passwd=password, db=dbname, charset=charset, use_unicode=True, cursorclass=DictCursor)
for i in range(50000):
conn = currentPool.connection()
cursor = conn.cursor()
conn.close()
cursor.close()
currentPoll.close()
耗时:1分32秒