NoSQL
Intro to Document Stores
What is NoSQL?
NoSQL refers to the storage and retrieval of persistent data that are not stored in related tables.
DB Management Systems that fall under this term are
Key-Value, Graph, or Document Stores
What is a
Document Store?
a.k.a. Document-oriented Database
Document Store
Every database management system that utilizes the idea of storing data as "documents", store these documents in the following formats
- JSON
- BSON
- XML
- YAML
A Sample Document
{
"_id" : ObjectId("55affc8487df4e4899702589"),
"username" : "qui-gon-jinn-and-juice",
"firstName" : "Anakin",
"lastName" : "SkyTroller",
"password" : "padawonton"
}
Tables, Records, Collections, Documents,
what's the difference?
Tables and Collections
RDBMS stores records as rows in related tables
Document-Oriented DBs store Documents in unrelated Collections
Tables define columns, their data type, have keys and constraints.
Collections are schema-less at the db layer.
Tables and Collections, cont.
Tables can be indexed
Collections can be indexed
Indexes increase the performance of queries.
db.jedis.createIndex({ name : "text"});
db.weapons.createIndex({ weight : 1 });
CREATE INDEX jedi_name_idx ON jedis (name);
Records and Documents
Tables have a predefined schema that all contained rows must abide by. Each column is defined with strict data types
Documents store unstructured data in fields that do not have to conform to a predefined schema.
Rows in tables must follow the predefined schema, if the table schema changes, every row in the table adopts these changes.
Individual Documents can be updated at any time to remove existing or add new fields.
db.jedis.insert({ username: "mace808", firstName:"Mace"});
db.jedis.insert({ name: { first: "Obi Wan", last: "Kenobi"}, force : 1200 });
CREATE TABLE jedis (
id serial,
username varchar(50) NOT NULL,
firstName varchar(50) NOT NULL,
lastName varchar(50) NOT NULL,
password varchar(32) NOT NULL,
weapon_id integer,
PRIMARY KEY id
);
Records and Documents, cont.
Tables can set foreign key constraints to track relationships
Documents do not have foreign keys to manage relations
Table schemas can be normalized to reduce redundancy and disk usage.
Documents can be denormalized to increase data duplication and disk usage, and can increase data read/retrieval performance.
CREATE TABLE orders (
id integer PRIMARY KEY,
customer_id integer REFERENCES products (id),
price integer
);
Records and Documents, cont.
Tables can join other tables using foreign keys, to include related data in the resulting rows.
Documents can embed other documents!
Table rows cannot embed other rows.
{
"_id" : ObjectId("55affc8487df4e4899702589"),
"username" : "qui-gon-jinn-and-juice",
"firstName" : "Anakin",
"lastName" : "SkyTroller",
"password" : "padawonton"
"weapon" : {
"_id" : ObjectId("21zffc8487df4e4899702589"),
"name" : "Light Saber",
"weight" : 59.634
},
"friends" : [
{ "_id" : ObjectId("44bffc8487df4e4899702589"), ... },
{ "_id" : ObjectId("33cffc8487df4e4899702589"), ... },
]
}
SQL, NoSQL,
What's the difference?
SQL refers to any of the database systems where data is stored in tables that support relations.
NoSQL refers to any of the databases that manages data in non-relational Document, Key-Value, Graph, or Wide-Column stores.
Most features of RDBMS can be used synonymously with the term: SQL.
Most features of Document, Key-Value, Graph, and Wide-Column data stores can be used synonymously with term: NoSQL.
SQL databases
use the declarative Structured Query Language for defining and manipulating the data.
NoSQL databases
use an unstructured query language that varies from system to system, and can be imperative.
var usersCur = db.users.find();
var firstUser = usersCur.next();
var firstUserName = firstUser.username;
SELECT username FROM users LIMIT 1;
SQL databases
store data in tables with a fixed schema with strict types
NoSQL databases
store data in collections where each entry (document, value, node, or row) can have dynamic schemas, they can differ from entries belonging to the same collection
SQL databases
most rdbms are ACID compliant
Atomicity, Consistency, Isolation, Durability,
and support transactions.
NoSQL databases
most do are not ACID compliant, in favor of performance and scalability
SQL databases
typically use vertical scaling strategies.
the more data you have to store,
the bigger your server gets.
NoSQL databases
are designed to scale horizontally,
across multiple servers.
SQL products
- MySql
- Postgres
- MariaDB
- MS-SQL
- Oracle
- Sqlite
- more...
NoSQL products
- MongoDB
- Redis
- Cassandra
- Riak
- CouchDB
- BigTable
- Hbase
- more...
SQL
- Do you need the consistencies and reliability provided by being ACID compliant, having constraints, and transactions?
- Will you be analyzing aggregate data?
- Is a static typed schema ok?
- Do you need/want to write SQL?
NoSQL
- Do you need to scale easily?
- Is "eventual consistency" ok?
- Do you need non-mission critical data at served at higher performance?
- Will your schema change?
- Is it ok to not write SQL?
Which to Choose?
Intro to NoSQL
By Jon Borgonia
Intro to NoSQL
Intro
- 2,145