MongoDB vs MySQL using golang
I’m the guy who like to test. Benchmark is my… middle name. You can find dozens articles about speed one or other solution. I mean MongoDB and MySQL . I tried MongoDB couple times, and each time I wasn’t moved by it speed. Got project coming in, when I have to deal with large amount of concurrent connection. It’s not very big, but bigger then casual MySQL load.
So… how did I test – very dump way. Got 2 tables, one around 11k records, second one close to 200k record. Not much. Fields were indexed in both MySQL and MongoDB. And then i run benchmarks. First one had string fields, second one numeric.
Testing procedure
It’s not very sophisticated. With MySQL we have query like that SELECT something FROM table WHERE key=?
(not primary) and with MongoDB pretty mych the same: Find({key:string_value})
. Again – it’s not primary – it’s indexed field.
Both of them running in the same machine – my Mac Book Pro with SSD drives. Code is lunched from golang.
Measuring code is pretty ugly:
t0 := time.Now()
for i := 0; i < loop; i++ {
getmongo()
}
t1 := time.Now()
fmt.Printf("The mongo 1 call took %v to run.\n\n", t1.Sub(t0))
and for MongoDB
t0 = time.Now()
for i := 0; i < loop; i++ {
getmysql()
}
t1 = time.Now()
fmt.Printf("The mysql 1 call took %v to run.\n", t1.Sub(t0))
Let’s see results:
Running queries
The mongo 1 call took 2.230508ms to run.
The mysql 1 call took 900.2us to run.
------------------
The mongo 2 call took 1.229932ms to run.
The mysql 2 call took 718.89us to run.
Surprise surprise – MySQL is much more faster then MongoDB. Let’s run the same query 100 times.
The mongo 1 call took 85.661532ms to run.
The mysql 1 call took 56.858658ms to run.
------------------
The mongo 2 call took 89.683713ms to run.
The mysql 2 call took 54.091382ms to run.
Again – MySQL wins. So… where is the catch ? Let’s run the same query 500 times
The mongo 1 call took 396.514457ms to run.
The mysql 1 call took 254.117612ms to run.
------------------
The mongo 2 call took 409.387653ms to run.
The mysql 2 call took 269.543978ms to run.
It’s even worse. MongoDB still far behind. Let’s go crazy, and run the same query in parallel.
Concurrency
We are going to modify out code to run them as go routines :
t0 := time.Now()
for i := 0; i < loop; i++ {
go getmongo()
}
t1 := time.Now()
fmt.Printf("The mongo 1 call took %v to run.\n\n", t1.Sub(t0))
Let’s start with 100 in the same time:
The mongo 1 call took 250.5us to run.
The mysql 1 call took 216.609us to run.
------------------
The mongo 2 call took 217.295us to run.
The mysql 2 call took 243.412us to run.
And 500
The mongo 1 call took 1.153202ms to run.
The mysql 1 call took 1.201624ms to run.
------------------
The mongo 2 call took 1.19199ms to run.
The mysql 2 call took 1.202942ms to run.
So… MongoDB is start leading, when we run the same query as a concurrent queries. But still… difference is bit small to be excited about.
The Winner Is…
Unfortunately there is no winner here. If you are looking for an answer “what is better” the answer, will be “it depends”. MySQL is good enough for most of purposes. MongoDB is good when you don’t have fixed structure os just looking for easy way just to chunk data into storage.