I believe most popular databases(SQL or NoSQL) are capable of handling data of any type of application but are they efficient enough for the use case? It’s very important to find the appropriate database for the system because sometimes all the use cases are vague or unknown and you may find your system is paralyzing in future then migrating or adding another database on the top of it(Redis) is time consuming, costly and risky.
What is the use case?
Being a part of the SuperBot team, I can say it’s designed in such a way that every module can have its own technology stack if it’s necessary and It was the time to add another module, The Databank.
What is the Databank?
The databank is a collection of millions of records of contacts and the structure of contact is completely dynamic. A contact can have one or more information like phone number, name, location, etc.
On successful reposition of the data in the databank, the user can create one or more datasets on it by applying desired filters on contacts. A dataset is a part of the databank, later on, which can be used to start SuperBot campaign.
Requirements?
As it’s evident from the use case, we need a database system to store a fully dynamic databank and its datasets with good reading, writing and searching(with wildcards) speed.
We had to choose from two types of database systems, relational databases and document or NoSQL databases.
Redis is an in-memory database, so we are not going to use this database in this case.
MySQL | MongoDB | ElasticSearch |
---|---|---|
56.919 | 30.025 | 36.286 |
53.548 | 28.237 | 36.168 |
36.752 | 26.0296 | 38.547 |
b) Searching integer on second level of nested field
MySQL | MongoDB | ElasticSearch |
---|---|---|
1742.802 | 361.322 | 14.661 |
688.346 | 350.108 | 11.364 |
657.467 | 376.343 | 12.193 |
c) Searching word on second level of nested field
MySQL | MongoDB | ElasticSearch |
---|---|---|
38.576 | 29.442 | 34.646 |
41.664 | 31.106 | 41.039 |
47.129 | 29.967 | 47.637 |
As we can clearly see from the above tables, MySQL is taking much higher time than MongoDB and ElasticSearch. ElasticSearch is close to MongoDB in two cases and won in one case but on average it’s slower than MongoDB and also it doesn’t fit in our use case because ElasticSearch is a text search engine and not a general-purpose database system.
PostgreSQL VS MongoDB
All tests are done with 5 million rows and 2 levels on nested data and the results are in seconds.
We have used the GIN index in PostgreSQL and wild card indexing in
MongoDB.
a) Searching text on the second level of nested field
PostgreSQL | MongoDB |
---|---|
2.304 | 2.924 |
1.540 | 2.434 |
1.563 | 2.368 |
b) Searching integer on second level of nested field
PostgreSQL | MongoDB |
---|---|
0.044 | 0.079 |
0.043 | 0.065 |
0.047 | 0.067 |
c) Searching word on second level of nested field
PostgreSQL | MongoDB |
---|---|
1.520 | 1.704 |
1.581 | 1.695 |
1.622 | 1.755 |
Apparently, we can see PostgreSQL is slightly faster than MongoDB in querying over JSON data but we wanted to store every databank into its own collection. Creating collections dynamically in MongoDB is easier than creating tables in any SQL database. MongoDB is the combination of efficiency with easiness and speed. So for that reason, we decided to go with the MongoDB database.
Share: