قالب وردپرس درنا توس
Home / Tips and Tricks / What is a NoSQL database and what is it good for? – CloudSavvy IT

What is a NoSQL database and what is it good for? – CloudSavvy IT



A NoSQL database is any type of database that breaks away from the traditional design of SQL. NoSQL databases such as the document-based MongoDB have become more popular in recent years. What is it about?

The limitation of SQL: scalability

SQL has been around forever – 45 years. It works surprisingly well and modern implementations of SQL are very fast. But as the web has grown, the need for powerful databases has increased to meet demand.

The easiest way to scale an SQL database is to run it on a more powerful computer. SQL databases can be replicated to reduce the regional load on a single instance, but split a table (often called sharpness) is much more difficult for SQL.

Document-based NoSQL databases solve the problem through design. Each document is independent of other documents in the collection, so the collections can be shared across multiple servers much more easily. Many document databases will contain built-in tools for sharing data on different servers.

But the scalability problem is not really a problem until you have one pulp of data. You can easily run a SQL database with hundreds of thousands of users and have no problems, provided your structure is sound and your queries are fast.

Both MySQL and MongoDB are likely to get the job done for your application, so the choice between the two comes down to which structure and syntax you prefer. Simple development is important, and you may find the document model and syntax of the much newer MongoDB easier to work with than SQL.

NoSQL versus SQL structure

Traditional SQL databases are often called relational databases because of how they are structured. In a SQL database you will have several tables, all of which contain several rows (called tasks), which themselves have several different columns, or attribute. Each separate table is linked to each other by a primary key, which forms a relationship.

For example, imagine that you have a table with each entry that represents a post from a user. The primary key here is the username, which can be used to link the post table to the user table. If you wanted to find the email of the person who made the post, you should search for “Jon1

996” in the user table and select the “Email” field.

But this data structure may not work for everyone. SQL databases have a rigidly defined schedule that can get in the way if you need to make changes or just prefer to have a different layout. With complex data sets, the relationships between everything can grow more complicated than the data itself.

The main type of NoSQL database is a JSON document database, like MongoDB. Instead of storing rows and columns, all information is stored in individual documents. These documents are stored in collections (eg a “user document” will be stored in the collection “all users”) and do not have to have the same structure as other documents in the collection.

For example, a “user document” might look like this:

{
  "username":"Jon1996",
  "email":"jon1996@gmail.com",
  "posts": [
   {"id":1},
   {"id":2},
   {"id":3},
  ]
}

Usernames and e-mail fields are just key value pairs, similar to columns in SQL, but the “posts” field contains an array, which is not something you find in SQL databases. Now say we had a mail collection with documents like:

{
  "id":1,
  "title":"First Post",
  "content":"Hello, World!",
  "madeby":"Jon1996"
}

Now when someone visits Jon’s page, your application can retrieve three posts with IDs 1, 2 and 3, which is usually a quick question. Compared to SQL, where you may need to retrieve all posts that match Jon’s user ID. Still pretty fast, but the MongoDB question is more direct and makes more sense.

What are NoSQL databases good for?

NoSQL is a broad category and contains many different types of databases built with different goals. Each database is a tool and your job may require a certain type of tool or even several different tools.

SQL databases such as MySQL, Oracle and PostgreSQL have existed since before the internet. They are very stable, have a lot of support and can generally do the job for most people. If your data is valuable to you and you want an established, consistent solution, stick to SQL.

JSON document databases, such as MongoDB and Couchbase, are popular for web applications with changing data models and for storing complex documents. For example, a website like Amazon may often need to change the data model to store products on the website, so a document-based database may work well for them.

Document databases are intended to be a generic replacement for SQL and are probably what you think of when you hear “NoSQL.” They are also more intuitive to learn than SQL, as you do not have to deal with table relationships or complex issues.

RethinkDB is a JSON document database built for real-time applications. In a database like MongoDB, you have to check for updates every few seconds, or implement an API in addition to it to track real-time updates, which quickly becomes cumbersome. RethinkDB solves this problem by automatically pushing updates over web connection streams that clients can connect to.

Redis is an extremely high-performance key-value database that stores small keys and strings entirely in RAM, which is much faster to read and write to than even the fastest SSDs. It is often used in conjunction with other databases as a memory for small data that is written to and read frequently. For example, a messaging app might want to use Redis to store user messages (and even send real-time updates using their Pub / Sub methods). Storing many small messages in this way can cause performance issues with other types of databases.

Graph databases are built to store connections between data. A common use case is social networks where users are connected to each other and interact with other data, such as posts they have created.

In this example, George is friends with two people, Jon and Jane. If another type of database wanted to understand George’s connection to Sarah, they would have to ask all of Jon’s friends and all of Jane’s friends. But graph databases understand this connection intuitively; for friends to friend questions, the popular graph database Neo4J is 60% faster than MySQL. For friends to friends of friends (3 levels deep) Neo4J is 180 times faster.

Wide column databases such as Cassandra and Hbase, are used to store large amounts of data. They are built for data sets that are so large that you need multiple computers to store everything, and they are much faster than SQL and other NoSQL databases when spread across multiple nodes.


Source link