NoSQL vs SQL. When and what type of database to choose?

Piotr Rzeźnik | Databases | 16.02.2022

SQL VS No SQL Databases

The aspect of data storage and processing large amounts of data is one of the inherent elements and challenges of any IT project. There are many types of databases and choosing between several flexible data models is not always easy – the main challenge here is to accurately and logically represent business processes of a given domain. So when and which database to choose? What databases are available? And finally – what are the capabilities of non-relational NoSQL databases?

SQL and NoSQL database types

The most popular types of databases include:

  • relational databases (e.g. MySQL
  • and non-relational databases (e.g. MongoDB, Oracle NoSQL database).

What is SQL?

SQL is nothing but a language, but its purpose is quite different from that of Java or C # languages. SQL is used for specific activities such as data access and modification. To be more specific, SQL means Structured Query Language. It is a query language that allows you to retrieve data from a database – for this purpose it was created: to access, store and edit data in relational databases.

What is a relational database?

A relational database is a type of database that is usually made up of tables. This allows you to access data in a relationship that is part of other data (a table) in the same database. In other words, it stores data in multiple tables that are structured into columns and rows. This allows you to query data from different tables at the same time.

A relational database is based on a relational model, and RDBMS (Relational Database Management System). In order for Relational Database Management Systems to work with many kinds of databases, SQL is used for managing and creating queries, which is in this case the most popular language.

MongoDB

NoSQL with MongoDB

 

Find out more about distributed databases capabilities.

Read the article

How do SQL relational databases work

Relational database is based on the relational model in which data is mapped to one or more tables (or “relationships”), columns and rows, which maps data to one or more tables (or “relationships”), columns and rows. Each row in the table has a unique identifier by which it is associated. In turn, each database table represents some type of entity (an example of an entity could be “customer”).

The rows of the table show a specific instance of this entity (e.g. customer – John Doe), and the columns, also known as attributes, show the details of a given object (e.g. name, address). The relationships themselves are nothing more than matching data in different tables on the basis of primary and foreign keys.

Relationship types

The main types of relationships are:

1-to-1

A one-to-one relationship between two tables. It occurs when each record from the first table has exactly one record
from the second table assigned to it, and vice versa. To define a one-to-one relationship, include the primary key value from the first table in the second table.

1-to-many

A one-to-many relationship also exists between two tables. Occurs when a single record from the first table has one or more records from the second table associated with it. However, the second table only has one record from the
first table associated with it.

Many-to-many

Many-to-many relationship – This is also the relationship between two tables. A single record from the first table has one or more records from the second table associated with it, and vice versa. In a many-to-many relationship, a third table is often created.

NoSQL database – how does it work?

Non-relational databases are also called NoSQL databases. The name comes from a different approach to storing and retrieving data than in SQL-based relational databases. Note that some non-relational databases support SQL.
NoSQL databases are characterized by the fact that they are able to handle a large amount of unstructured data. NoSQL solutions are nothing new, but only for several years they have been rapidly gaining popularity due to the possibility of handling many data, e.g. from mobile devices, IoT or Big Data.

Relational vs non-relational database

The structure

  • SQL databases store data in tables with a fixed number of rows and columns.
  • NoSQL databases store data using the below data models:
    • document databases (JSON documents)
    • Key-value data model (key – value pairs)
    • Graph databases

Scheme / Diagram

  • SQL databases require a fixed, predefined schema. All data must have the same or similar structure. As a result, it is often necessary to have gathered the initial requirements for the system before starting work. In addition, the flexibility of the base may be compromised given that modifications (migrations) of the structure can be complicated and complex.
  • NoSQL databases have a dynamic schema for unstructured data. A fixed schema definition is not required, which makes it easier to make changes to the structure.

Scalability

  • SQL databases are vertically scalable (it is so-called scale-up). This means that if you want to increase the amount of data stored on a single server, you need to increase RAM, CPU performance or SSD capacity. Scaling relational databases is rather more difficult. In order to maintain data integrity in transactions in a multi-server SQL database, a backend is needed that allows to synchronize all write operations and transactions in order to avoid the deadlock phenomenon (i.e. deadlock, mutual blocking of actions).
  • NoSQL databases are characterized by horizontal scalability (scale-out). This means that scaling is done by increasing the number of servers. JOIN operations allow you to combine and bind pieces of data. NoSQL databases typically are not designed to handle JOIN operations efficiently, but they do. Data can reside on different servers in NoSQL databases, where joining tables from multiple servers can be troublesome. NoSQL enables easy scaling by data sharding. Having a routing layer allows you to redirect the query to the appropriate shard, making NoSQL databases highly scalable and allowing for quick query handling.

Queries

  • The SQL language has been around for over 30 years, which is why it is widely used, popular and has a good reputation. It is extremely efficient when it comes to querying, operating, and retrieving data from relational databases. In addition, it is also distinguished by declarativeness (that is, it allows you to describe what is to be done with its help). The advantage of SQL is that it is quite easy to learn. This means that business analysts or other employees not related to programming can use it without major problems.
  • When it comes to NoSQL queries, it may not be as simple to use SQL in relational databases as it usually requires additional data processing and there is no single declarative query language. Therefore, tasks using NoSQL are usually performed by programmers.

In summary, how to run queries in NoSQL databases largely depends on the database. For example, in MongoDB, to request data from a JSON document database, specify documents with properties that the results should match and use the following function: db.collection.find()

Other popular solutions may include creating query functionality directly at the application layer (rather than at the database layer) or implementing MapReduce, a platform that facilitates the processing of big data.

When to choose NoSQL and when to choose SQL

Now that we know the main differences between SQL and NoSQL, let’s try to answer the question: When to use relational databases and when to use non-relational databases? As is often the case in IT – the decision depends
on many components. In this case, the main points to consider are:

  1. Data types
  2. Database management method
  3. Amount of data
NoSQL vs SQL. When and what type of database to choose?NoSQL vs SQL. When and what type of database to choose?

When to choose SQL?

Referring to the first component, the type of data – in this case, relational databases will perform better than NoSQL databases, if data consistency and integrity are key.

It is a common belief that relational databases are not a good choice for handling large amounts of data. This statement is not entirely true. Many databases like MySQL or PostgreSQL can handle large amounts of data very well. Relational databases have a fixed, fixed schema and require data that is structured. Maintaining such a structure, consistency and efficiency may turn out to be very difficult if, with the help of the relational base, we are going to handle the Big Data business.

At first glance, it might seem that the fixed data structures may be limiting, but there is no rule here. Having a fixed, predefined structure makes SQL databases a better option for handling payment systems or reservation systems. An interesting fact is that most financial institutions rely on relational databases. Relational databases ensure transactional nature, i.e. data integrity and correctness. SQL can limit some functionality at times, but
on the other hand, it is a very mature and proven technology.

When to choose NoSQL?

NoSQL databases can store different kinds of data and they don’t need to be structured in any way. Therefore, non-relational databases provide greater flexibility and are a good choice for handling large amounts of data without a common structure.

Typically, the more extensive your data set is, the more likely a NoSQL database is to be the better choice. Non-relational databases are predisposed to scalability and availability, making them an ideal solution for real-time applications (e.g. online gambling, instant messaging).

How about using multiple databases?

You first need to understand the domain. What effect are you trying to achieve? Nowadays, often the choice between SQL and NoSQL is not a question of which database to use, but of when and where to use each of these databases within the same application or system.

Personally, I am working on an application in which the use of NoSQL was – without going into details – the most sensible, but the same application also required reports. To avoid excessive problems and analyzes, I decided to use both types of databases. I used NoSQL for the web and desktop application and SQL for the reports themselves. The information is stored in the NoSQL database and only the data required for the reports is transferred to the SQL database.

NoSQL databases vs SQL databases – comparison

NoSQL vs SQL. When and what type of database to choose?NoSQL vs SQL. When and what type of database to choose?

To sum up: relational database or non-relational data model?

Choosing the right database is not easy, even for experts, and deciding whether to choose relational or non-relational databases can depend on many factors. You should also consider how many options are available in the market for SQL and NoSQL databases. For example, if you have a large amount of unstructured data, databases may be a good solution CouchDB or MongoDB. However, when high availability is your priority, they may be a better choice Redis and Cassandra.

On the other hand, SQL databases offer many advantages in terms of data transactions and their overall integrity. Moreover, the relationships within them are easily identified and defined, which makes it easier to draw conclusions from critical insights.

The author of the post is:

.NET Developer

A flesh and blood programmer who likes various challenges related to R&D and Proof of Concept, with maintaining the DDD and SOLID approaches.
Fascinated by microservice architecture and decomposition. Privately, a fan of motorization, racing and Track Days.

Add comment: