Professional Documents
Culture Documents
Key-Value Stores:
Graph-Based databases:
Relationships
Aggregates are useful in that they put together data that is commonly
accessed together. But there are still lots of cases where data that’s
related is accessed differently.
Consider the relationship between a customer and all of his orders.
Some applications will want to access the order history whenever
they access the customer;
This fits in well with combining the customer with his order history
into a single aggregate.
Other applications, however, want to process orders individually and
thus model orders as independent aggregates.
In this case, you’ll want separate order and customer aggregates but
with some kind of relationship between them so that any work on an
order can look up customer data.
The simplest way to provide such a link is to embed the ID of the
customer within the order’s aggregate data. That way, if you need
data from the customer record, you read the order, ferret out the
customer ID, and make another call to the database to read the
customer data.
This will work, and will be just fine in many scenarios—but the
database will be ignorant of the relationship in the data.
This can be important because there are times when it’s useful for
the database to know about these links.
As a result, many databases—even key-value stores—provide ways
to make these relationships visible to the database.
Document stores make the content of the aggregate available to the
database to form indexes and queries.
Riak, a key-value store, allows you to put link information in
metadata, supporting partial retrieval and link-walking capability.
An important aspect of relationships between aggregates is how they
handle updates.
Aggregate oriented databases treat the aggregate as the unit of data-
retrieval.
Consequently, atomicity is only supported within the contents of a
single aggregate. If you update multiple aggregates at once, you
have to deal yourself with a failure partway through.
Relational databases help you with this by allowing you to modify
multiple records in a single transaction, providing ACID guarantees
while altering many rows.
All of this means that aggregate- oriented databases become more
awkward as you need to operate across multiple aggregates.
There are various ways to deal with this, which we’ll explore later in
this chapter, but the fundamental awkwardness remains.
This may imply that if you have data based on lots of relationships,
you should prefer a relational database over a NoSQL store.
While that’s true for aggregate-oriented databases, it’s worth
remembering that relational databases aren’t all that stellar with
complex relationships either.
While you can express queries involving joins in SQL, things
quickly get very hairy—both with SQL writing and with the
resulting performance—as the number of joins mounts up.
This makes it a good moment to introduce another category of
databases that’s often lumped into the NoSQL pile.
Graph Databases
A graph database is a type of NoSQL database that is designed to
handle data with complex relationships and interconnections.
In a graph database, data is stored as nodes and edges, where nodes
represent entities and edges represent the relationships between
those entities.
1. Graph databases are particularly well-suited for applications that
require deep and complex queries, such as social networks,
recommendation engines, and fraud detection systems. They can also be
used for other types of applications, such as supply chain management,
network and infrastructure management, and bioinformatics.
2. One of the main advantages of graph databases is their ability to
handle and represent relationships between entities. This is because the
relationships between entities are as important as the entities themselves,
and often cannot be easily represented in a traditional relational
database.
3. Another advantage of graph databases is their flexibility. Graph
databases can handle data with changing structures and can be adapted
to new use cases without requiring significant changes to the database
schema. This makes them particularly useful for applications with rapidly
changing data structures or complex data requirements.
4. However, graph databases may not be suitable for all applications. For
example, they may not be the best choice for applications that require
simple queries or that deal primarily with data that can be easily
represented in a traditional relational database. Additionally, graph
databases may require more specialized knowledge and expertise to use
effectively.
Some popular graph databases include Neo4j, OrientDB, and ArangoDB.
These databases provide a range of features, including support for different
data models, scalability, and high availability, and can be used for a wide
variety of applications.
RELATIONAL DATABASE
MANAGEMENT SYSTEM Non-relational or distributed database system.
(RDBMS)
Schemaless Databases
Traditional relational databases are well-defined, using a schema to describe every
functional element, including tables, rows views, indexes, and relationships. By
exerting a high degree of control, the database administrator can improve
performance and prevent capture of low-quality, incomplete, or malformed data. In a
SQL database, the schema is enforced by the Relational Database Management
System (RDBMS) whenever data is written to disk.
But in order to work, data needs to be heavily formatted and shaped to fit into the
table structure. This means sacrificing any undefined details during the save, or
storing valuable information outside the database entirely.
A schemaless database, like MongoDB, does not have these up-front constraints,
mapping to a more ‘natural’ database. Even when sitting on top of a data lake, each
document is created with a partial schema to aid retrieval. Any formal schema is
applied in the code of your applications; this layer of abstraction protects the raw
data in the NoSQL database and allows for rapid transformation as your needs
change.
Any data, formatted or not, can be stored in a non-tabular NoSQL type of database.
At the same time, using the right tools in the form of a schemaless database can
unlock the value of all of your structured and unstructured data types.
name:”abc”,age:30,interest:”football”
name:”xyz”,age:25
As you can see, the data itself normally has a fairly consistent structure. With the
schemaless MongoDB database, there is some additional structure — the system
namespace contains an explicit list of collections and indexes. Collections may be
implicitly or explicitly created — indexes must be explicitly declared.
The lack of schema means that your NoSQL database can accept any data
type — including those that you do not yet use. This future-proofs your
database, allowing it to grow and change as your data-driven operations
change and mature.
No data truncation
With NoSQL, you can use whichever data model is best suited to the job.
Graph databases allow you to view relationships between data points, or you
can use traditional wide table views with an exceptionally large number of
columns. You can query, report, and model information however you choose.
And as your requirements grow, you can keep adding nodes to increase
capacity and power.
Materialized Views
Views:
A View is a virtual relation that acts as an actual relation. It is not a part of
logical relational model of the database system. Tuples of the view are not
stored in the database system and tuples of the view are generated every
time the view is accessed. Query expression of the view is stored in the
databases system.
Views can be used everywhere were we can use the actual relation. Views
can be used to create custom virtual relations according to the needs of a
specific user. We can create as many views as we want in a databases
system.
Materialized Views:
When the results of a view expression are stored in a database system, they
are called materialized views. SQL does not provides any standard way of
defining materialized view, however some database management system
provides custom extensions to use materialized views. The process of
keeping the materialized views updated is know as view maintenance.
Database system uses one of the three ways to keep the materialized view
updated:
Update the materialized view as soon as the relation on which it is
defined is updated.
Update the materialized view every time the view is accessed.
Update the materialized view periodically.
It does not have any storage cost It does have a storage cost associated with
associated with it. it.
It does not have any updation cost It does have updation cost associated with
associated with it. it.