Why NoSQL?
Today NoSQL databases are one of the most interesting topics. Sometimes people think that those databases are the best solution ever and could fix all their problems. When you need consistency or data isn’t very huge then you need a relational database. But when you need scalability, distributing and availability then you need NoSQL — this isn’t entirely the true. Let’s find out the difference between NoSQL and SQL databases.
We’ll take into consideration one example of the NoSQL database — document-oriented like MongoDB.
Relational vs non-relational
By term “SQL Database” we usually mean RDBMS(Relational DataBase Management System). The main word here is relational. This is tightly connected with the term “Normal Form”.
In SQL databases your data spread among different tables, to receive information, you must join tables to receive the result. Let’s have a look at two tables below:
Table Client
+----+------+--------+
| Id | Name | phone |
+----+------+--------+
| 1 | Nick | 800081 |
| 2 | Bob | 700071 |
+----+------+--------+
Table Order
+----+-----------+------------+-------+
| Id | Client_Id | Name | Price |
+----+-----------+------------+-------+
| 1 | 1 | "HDD" | 100 |
| 2 | 1 | "SSD" | 200 |
| 3 | 2 | "Computer" | 500 |
+----+-----------+------------+-------+
to find all the orders with the client name you need to join these two tables. Join operation is very expensive.
But if you have a look at one example of NoSQL, document-oriented. Data could be stored in JSON format:
[
{
id: 1
value: {
name: "Nick",
phone: "800081"
order: [{name: "HDD", price: 100}, {name: "SSD", price: 200}]
}
},
{
id: 1
value: {
name: "Nick",
phone: "800081"
order:[ {name: "Computer", price: 500}]
}
}
]
As you can see all the information is stored within one object. Therefore it’s cheaper to get this data for the database because it doesn’t require join operation.
Changing a schema
Another difference is changing a schema. In the relational database you need to apply DDL(Data definition language), e.g. to add a new column. It could look like this:
alter table Client add column email varchar(500);
In the current NoSQL you don’t need to do anything, just insert a new object with a new property.
Overall it isn’t true, you can add schema validation on NoSQL. And if the value isn’t empty you should apply an additional operation on SQL and NoSQL.
Scaling
NoSQL databases mostly have built-in horizontal partitioning. It’s good for availability, but you’ll have problems with consistency.
For SQL databases there is no absolute scaling because it means you could break relations or consistency. And scaled SQL databases mostly are very expensive solutions.
Consistency
If ACID is a requirement for the SQL databases NoSQL hasn’t consistency like in SQL because it’s distributed. Depending on your configuration you could wait for the operation to be consistent on all the nodes. Or you could wait until data inserted in one node and then database will make eventual consistency. You should take this into account when you architect your system.
Conclusion
There are many differences between NoSQL and SQL databases. Depending on the needs you should choose one, but they could complement each other. So, whenever you decide which database you should use, check all the information for a particular database and decide what you need in your system. Use right tool for the the right job!