MongoDB is not as fast as MySQL?

I discovered mongodb a few months ago, and after reading this post , I thought mongodb was really faster than mysql, so I decided to build my own bench, the problem is that I do not have the same result as the above author messages, especially for querying a database: mongodb seems slower than MyISAM tables. Could you take a look at my python code, maybe something is wrong with it:

from datetime import datetime import random import MySQLdb import pymongo mysql_db=MySQLdb.connect(user="me",passwd="mypasswd",db="test_kv") c=mysql_db.cursor() connection = pymongo.Connection() mongo_db = connection.test kvtab = mongo_db.kvtab nb=1000000 thelist=[] for i in xrange(nb): thelist.append((str(random.random()),str(random.random()))) t1=datetime.now() for k,v in thelist: c.execute("INSERT INTO key_val_tab (k,v) VALUES ('" + k + "','" + v + "')") dt=datetime.now() - t1 print 'MySQL insert elapse :',dt t1=datetime.now() for i in xrange(nb): c.execute("select * FROM key_val_tab WHERE k='" + random.choice(thelist)[0] + "'") result=c.fetchone() dt=datetime.now() - t1 print 'MySQL select elapse :',dt t1=datetime.now() for k,v in thelist: kvtab.insert({"key":k,"value":v}) dt=datetime.now() - t1 print 'Mongodb insert elapse :',dt kvtab.ensure_index('key') t1=datetime.now() for i in xrange(nb): result=kvtab.find_one({"key":random.choice(thelist)[0]}) dt=datetime.now() - t1 print 'Mongodb select elapse :',dt 

Notes:

  • both MySQL and mongodb are on locahost.
  • Both MySQL and mongodb have an indexed key column

MySQL table:

 CREATE TABLE IF NOT EXISTS `key_val_tab` ( `k` varchar(24) NOT NULL, `v` varchar(24) NOT NULL, KEY `kindex` (`k`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; 

Versions:

  • MySQL: 5.1.41
  • mongodb: 1.8.3
  • python: 2.6.5
  • pymongo: 2.0.1
  • Linux: Ubuntu 2.6.32 32Bits with PAE
  • Equipment: desktop core i7 2.93 Ghz

Results (for 1 million inserts / selects):

 MySQL insert elapse : 0:02:52.143803 MySQL select elapse : 0:04:43.675914 Mongodb insert elapse : 0:00:49.038416 -> mongodb much faster for insert Mongodb select elapse : 0:05:10.409025 -> ...but slower for quering (thought was the opposite) 
+7
source share
3 answers

Sigh. These kinds of tests, and I use this term in this case, usually break from the very beginning. MySQL is not a "slower" database than MongoDB. One is a relational database, the other is a NoSQL document repository. They will / should be faster in the functional areas that they were intended to cover. In the case of MySQL (or any DBMS) and MongoDB, this overlap is not as great as many people think it is. This is the same broken apple and comparison of oranges that you get with Radish and MongoDB discussions.

There are so many variables (application functional requirements, hardware resources, concurrency, configuration, scalability, etc.) to consider that any benchmark or article that ends with “MongoDB faster than MySQL” or vice versa summarizes the results to the point of futility.

If you want to do a test, first define a strict set of functional requirements and business rules, and then implement them as efficiently as possible for both solution and preservation. The result will be that one is faster than the other, and in almost all cases a faster approach has some significant drawbacks that can make a slower solution more viable depending on requirements.

All this ignores the fact that the above test does not simulate any real-world scenario. There will not be many applications occupying the maximum throughput without any streams / concurrency (which significantly affects the performance of most storage solutions).

Finally, comparing inserts like this is a bit broken. MongoDB can achieve stunning insert throughput with fire and forget volume insertions, or it can be several orders of magnitude slower with synchronized replicated recording. The fact is that MongoDB offers you a choice where MySQL is not (or less). Thus, the comparison here only makes sense of business requirements that allow you to record fire and forget recordings (which boils down to “I hope this works, but not great if it’s not”)

TL; DR stops making simple bandwidth benchmarks. They are almost always useless.

+27
source
 MySQL insert elapse : 0:02:52.143803 Mongodb insert elapse : 0:00:49.038416 -> mongodb much faster for insert 

Mongodb inserts much faster due to the fact that mongodb inserts all the data into RAM and then periodically cleans up the data on disk.

 MySQL select elapse : 0:04:43.675914 Mongodb select elapse : 0:05:10.409025 -> ...but slower for quering (thought was 

You can achieve the best performance with mongodb when you implement / denormalize your data. In many situations, mongodb allows us to avoid unification due to implementation / denormalization.

And when you just paste the data into one collection / table and think that the mongodb index should not be faster, the read speed should be the same when compared with sql database.

BTW: In mongodb 2.0, indexes are 25% faster, so I think 2.0 will work faster than mysql.

+6
source

It is wrong to watch python runtime and evaluate database quality. Each request consists of at least 3 parts:

  • request preparation (client side),
  • query execution (server),
  • response preparation (client side)

In my experience, data conversion for MongoDB => python takes much longer than for MySQL => python.

You should also use indexes in both databases. MongoDB only works well if you have the field indexes that you use for queries. Speaking about MySQL, I think that it is better to test performance on innoDB, MyISAM does not support transactions, foreign keys, triggers, but for me it is a bit outdated.

+2
source

All Articles