Relational and document-oriented databases, which one should I use?
The databases are behind virtually any app or website. They allow you to save information and consult it when necessary to eventually show it to the user. In this way, the information is always kept dynamic. On the other hand, there are several types of relational and document-oriented databases, here we will analyze their characteristics.
Note: to reduce redundancy in the article, the term "database" is interchangeable with that of "database management system (DBMS)".
The "traditional" way of storing is by using Oracle, MySQL or SQL Server, since they offer great speed, security and integrity of the information, implementing the relational model. They are proven and efficient technologies, but the time may come when this is not enough to offer an agile service.
When the number of users and information begins to grow, the speed with which the data is consulted and modified suffers. Therefore, it is likely that some other way to store them is needed, so that our application is always fast.
Document-oriented databases are part of the so-called non-relational databases (NoSQL). Unlike relational databases, its main focus is the denormalization of the data to maintain high speed, as well as being very flexible with the structure of each of its elements.
It is important to compare the advantages and disadvantages of these technologies, to determine if we need either or both to maintain optimal system performance.
Comparative
Relational databases
Its main focus is to keep the information complete and consistent at all times, having several ways to validate that there are no errors.
Examples: Oracle, MySQL, MS SQL Server, PostgreSQL, SQLite.
Advantage
- Information integrity
- High level of configuration through database management systems
- The necessary information can be efficiently consulted
- Less data redundancy, which reduces the storage required by databases
- Existing for more than 40 years, easily getting quality support and documentation.
Disadvantages
- Modifying the database structure is time consuming
- More planning is required to adjust them to business needs
- As the information is normalized, doing the joins reduces the speed of the queries.
Document-oriented databases
Its main focus is the rapid consultation of information, in addition to being flexible and allowing faster systems development.
Examples: Azure Cosmos DB, ArangoDB, BaseX, Clusterpoint, CouchDB, DocumentDB, IBM Domino, MarkLogic, MongoDB, Firestore.
Advantage
- Greater speed when consulting information
- Its structure is not fixed, so it is easy to modify. Documents may vary from each other
- Quickly adapts to the needs of the company and market
- Useful for handling huge amounts of information (Big Data)
Disadvantages
- Because your approach is denormalization, there is data redundancy
- By not using joins, any similar operation has to be performed by the client
- Data consistency is lower
- It is a relatively recent technology, so there is less support and documentation
conclusion
Both have advantages and disadvantages that can be mitigated using different optimization strategies, but this in turn is time consuming and requires advanced knowledge. Due to this, it is important to know how to decide which of these technologies is preferable for our project (or if both can be implemented). Therefore:
Use a SQL database in case:
- Data consistency is paramount for the project
- The structure of the information does not change much over time
- Quick and efficient support is needed in case of errors
Use a NoSQL database in case:
- The information structure is constantly changing
- Rapid development is required, without the need for a complex structure
- A huge amount of information is handled
Use both yes: your system or service needs consistency and flexibility in different components.