CodeDevelopr
CodeDevelopr

Reputation: 1265

How is a Document based DB so fast?

I just want to understand better, in what I have learned for years is a document based solution is slow and requires a lot of I/O. FOr example in a PHP project, it is generally said it is much much better to use a memory cache like Redis ,Memecache, or APC because they are memory based instead of caching data to an actual FILE.

Now all these NoSQL DB's have arrived and I read about how they are so much faster then MySQl and others and they are Document Based. Can someone help me understand this theory? If each record is a Document (FILE), then how is it so good on performance? I recently read about a guy who was using Redis in a project and said he switched to MongoDB and is having better results then he did with Redis (I realize I am comparing a Cache to a DB, but that is not the real question, I want to know how a Document based solution is faster then non-document based solutions?)

Upvotes: 9

Views: 3614

Answers (6)

Sudip Bhandari
Sudip Bhandari

Reputation: 2275

One of the factors which make Document based db faster than the relational ones is Locality.

Documents are independent units:: which makes performance better (related data is read contiguously off disk) and makes it easier to distribute data across multiple servers while preserving its locality.

Document DataBase

Upvotes: 1

Addys
Addys

Reputation: 2501

The magic ingredient isn't necessarily a "faster" database, it is a database which enables the design and implementation of "faster" systems. That is why NoSQL databases are considered a game-changer.

For several decades relational databases were the only game in town. Many SQL-based systems pay a double performance tax: once for the full ACID featureset (which they probably don't need anyway), and then again to shoehorn their domain data into a relational table model.

Also, one common trait of most NoSQL databases is that they are simpler due to their being more specialized than the "general case" approach of a SQL database. That means less logic/code which needs to run on each operation, simpler data structures (which may require less IO) and in general - less overhead, better performance.

Upvotes: 3

milan
milan

Reputation: 12412

To get an idea, consider this:

  • with MongoDB you'd design your schema in a way that a single document holds everything you need to render a page.
  • with MySQL (or any other RDBMS) you'd normalize the data and split it across many tables. To render the same page, you'd have to make many SQL queries.

Although that one mongo query might be slower than one mysql query, comparing 1 mongo query to 100 mysql queries is going to be much faster.

Upvotes: 0

Eugen Rieck
Eugen Rieck

Reputation: 65274

The NoSQL speak can be prone to misunderstandings, as some of the concepts will use names, that have a different meaning to the traditional one:

  • File based doesn't (necessarily) mean, that the Datastore will write each record to a file - it is meant to say that records in the datastore will not have to conform to a predefines schema of fields if a certain data type. Think of "file" as something like XML, JSON or friends.
  • The performance wins of (most) NoSQL datastores comes at a price: Typically well-understood ACID promises are traded against a looser consistency model.
  • The power of relational SQL databases comes to a big part from the fact, that as good as every query can be written against an existing schema. This is not allways true with NoSQL datastores: In the most extreme version access to a record is possible only via a record-ID.
  • Most NoSQL datastores will scale much better than a typical relational Database - they are the answer to the question "What do we have to sacrifice from a well-understood relational DB" to overcome the scaling limits"

Upvotes: 3

bardiir
bardiir

Reputation: 14782

Document Based doesn't necessarily mean they are stored entirely on file system. Some parts can still be held in memory like an index.

Document based only means the database stores data in packages (like sheets of paper where every sheet is a dataset and you can write freely on it) instead of a very specific structure like a table.

http://en.wikipedia.org/wiki/Document-oriented_database

Ah and why they can be faster than redis:
Let's say you need to store some non-linear information in a set (i.e. not every dataset looks the same and you got different datatypes in one set. On Redis you can only store key-value pairs so you will need so link them back together to a set in your own code/implementation. On a NoSQL Database this is handled for you by the database in a (probably) much more optimized way :)

Upvotes: 5

thedrs
thedrs

Reputation: 1464

First thing is - you can't compare the NoSQL DBs to in-memory DBs. NoSQL DBs are ment for data that won't fit in the memory.

Now, regarding the NoSQL DBs, they are not just simple files, they have indexes that provide fast access to offsets in the files and that is where the speed really is.

Upvotes: 0

Related Questions