title: pymongo insert vs pymysql insert
body: <p>I am testing MongoDb and MySQL using their python connector pymongo and pymysql, especially the insert function. pymongo version is 3.4, pymysql is 0.7.9, python is 3.5</p>
  
  <p>My code looks like for mongo :</p>
  
  <pre><code>    client = MongoClient('localhost', 27017)
      db = client['local']
      collection = db['cqt']            
  
      for i in range (0,10):
          datas = [ {'a' : 1, 'b':2" }, {'a' : 3, 'b':4" }, ...] # 2000 dicts
          data =  [ bson.son.SON( d ) for d in datas]
          deltaT = time.clock()
          collection.insert_many( data )
          deltaT = time.clock() - deltaT 
  </code></pre>
  
  <p>And for mysql :</p>
  
  <pre><code>    connection = pymysql.connect(host='127.0.0.1',
                       user='admin',
                       password='toto',
                       db='cqt',
                       charset='utf8mb4',
                       cursorclass=pymysql.cursors.DictCursor)
  
      cursor = connection.cursor()
      sqlRequest = """INSERT INTO `cqt`.`myTable` (`a`,`b`) VALUES """
  
  
      for data in datas : sql += data.getSQL() + ","  # 2000 rows
      deltaT = time.clock()
      cursor.execute( sql )
      deltaT = time.clock() - deltaT 
  </code></pre>
  
  <p>Data are simple dictionnaries, 6 columns with a string key and a int value.</p>
  
  <p>When plotting the deltaT variable (the required time to insert 2000 entries), I'm surprised to find that mongo is much slower than mysql, by a factor 10. 
  <a href="https://i.stack.imgur.com/efZuV.png" rel="nofollow noreferrer">insert duration results</a></p>
  
  <p>I don't want to do a benchmark here but just to have estimation of database performance in the usecase I will need. However, from other tests made in java or looking at benchmark on the web between MySQL and MongoDB, my results with python are not what I was expecting at all. I should have quite similar performances for insert with MySQL and Mongo, and even better performance for Mongo.</p>
  
  <p>So, is the pymongo connector slow ? 
  Do you know if there are some parameters I should modify in my code or in MongoDB to improve the perf ? 
  Any suggestion to have better performance with MongoDB ?</p>