What are the advantages of using a database instead of a JSON file to write data?

Asked

Viewed 100 times

4

Most systems currently use the database feature to record the most diverse types of information, even static information. This leads to the conclusion that a database brings significant conveniences.

What are the advantages of using a database in contrast to a simpler way of writing data such as a JSON or XML file?

Note that I am not asking what is "best". I know that each way of doing things (mostly at least) has its advantages and disadvantages. That’s why I’m asking about the advantages of a database in the specified context. With that, I’m also not asking for opinions, since I believe that this kind of question can be answered with facts, such as here and here.

I have also checked some other questions here at Sopt, but I did not find the desired answer:

To decrease the chances of someone finding the question too open, let’s take a simple example.

Suppose I have a task web application. The information to be saved in a database or JSON file would be the user authentication information, as well as information regarding tasks, statistics and settings of each of them. It is an application that could serve a large number of users, performing various CRUD operations. I ask again the question of the title to a situation like this.

Related articles/questions are also welcome.

  • 1

    How so compare a SGDB with a file format? The responsibility of the SGDB is to manage the persistence, handling, consistency and organization of the data, as well as control the access to the data manage the data access competition. While responsibility of the JSON format is only to be a simple and fast data exchange standard between systems. One you use to save/access the data the other uses to communicate the data.

  • 2

    A party in what creates confusion, Nosql document-oriented databases use XML-like document formats , YAML , JSON however what is not widespread is that these documents are supported within the Nosql database as a metadata structure creating an object model for each stored document, thus giving the impression to the user to be consulting simple JSON documents, for example, when in fact what is being consulted is a much more complex data structure.

3 answers

4


A database was made to store data, so by itself should be enough advantage to think about adopting it.

You may think that JSON is also for storing data, but no, and this has been answered before, as you know. You can use JSON wherever you want, even within a database, but it’s not meant to store data.

But you can take then the right comparison that is between storing data in a database and storing data in a file, by chance in JSON format.

A database is a software tool where you ask to store, access and manipulate data. A file, whatever format it is, is just that, your program has to manipulate it. It’s your problem to take care of everything in all these operations.

The database gives you more guarantees, more ease, and efficiency, as we’ve already known. Of course each specific DB will give more or less, but in general most will give a lot.

The advantage of the JSON file is that it is very simple to manipulate by not requiring sophisticated software to be installed or going together. Manipulating the JSON file will probably require a library to handle it (you can do it totally by hand, there are few that will do it for one reason or another), but it will be something simple. Otherwise it can still take up a smaller space when the information is very small.

When you are talking about storing settings or data that are practically not manipulated can be more practical than a DB, but even this is a little questionable. See more.

It might even be a log, although the JSON format may not be the most appropriate. But everything stops there. So it is even complicated to talk about the advantages of DB, because they are all possible except the one mentioned in the previous paragraph.

Even simple things already begin to compensate using a simple and small DB like Sqlite or other simpler yet.

Note that I’m not even buying with just a relational DB and mainly that has SQL as the data access language, this can all be specific advantages of a type of database.

If you have access from an external source and/or competitor has every effort to make it happen properly within the DB and it is not you who needs to code, which would be torture and would go wrong, few people are able to do it well. In a large volume of access the person can only make it happen if they practically write a database software inside their application.

A database gives various ways to access the data efficiently, it is not only a place to store data, but mainly recover it, usually through indexes. Just because you don’t have to think about it is a huge advantage. If you are going to do something like this in this JSON file you have to at least think about how you handle all the manipulation. If it’s not just reading something small then it’s something really hard to do.

I haven’t even talked about security, because there are several levels that can have in each DB.

I didn’t mention consistency either because not all of the Databases offer this very well. Not by chance some of those who do not offer are those who have chosen to have the JSON format internally.

In fact, I can only imagine two reasons why a person chooses to use a JSON file as a database: they don’t want to learn how to use a database (which seems more complicated at first, but when she goes to manipulate a file in her hand she will see the mistake she made and how it was easier); or she wants to make fun.

0

I believe that the answer to this question goes through the classification of the data according to its relation in the system diagram:

           .---------.
Entrada -> | Sistema | -> Saída
   ^       '---------'      |
   '------------------------'
  1. doorway only, as constants, images, message translation strings, and execution settings;
  2. just exit, like execution logs, command line processing, and data that is expected to be consumed by another system;
  3. in and out, as dynamic user data, mutable business data, and others that can be written and read.

A file, in JSON format or not, has the main advantage of being readable and editable, not only by humans but also by line-to-line tools such as Git and the entire Unix tool. In general, they are also simple to generate from within a system. For this reason, files fit well with data that fit the classifications (1) and (2) above. Some input types are so static that we even add to the system code!

The problem with files, however, is their robustness. In a way, they are a low-level solution. The file system may be unreliable, as in a network-mounted system; there may be concurrent change problems by different processes; and the file model is just a row of sequential bytes, which is barely expressive for more complex data models.

(It’s interesting that Sqlite says it can replace the fopen(), because they have already overcome all these problems while writing a database engine. They say they can even write to a file faster than with fwrite()!)

To increase the robustness of your application, it is recommended to use a database in cases (3). It would be possible to bypass some file problems, such as failed writing, or perform incremental writing, or use a checkpoint file... but all this is already supported by databases.

Note that I did not deal here about the data being structured or in tables. In a way, structured data aligns more to schema-free files, while table data maps beautifully to every type of database. However, I believe the main point is about access pattern: tabular data can be written as multiple CSV files, and hierarchical data can be stored in Nosql databases, or stored as blobs in relational databases.

0

When manipulating the JSON file, it should be loaded into memory and only then data processing is performed.

Now, imagine a database of 100 gigabytes or more. How much memory your server would have to have?

Object-oriented or relational databases have indexes to organize data and make it possible to extract data even if they have terabytes of information.

JSON only serves to send some data from one location to another, it’s just a repackaged XML.

Now I ask, what requires more server performance? I tested, here is the result.

http://sqlui.sourceforge.net/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.