67

Recently I was asked:

Why is NoSQL faster than SQL?

I didn't agree with the premise of the question... it's just nonsense for me personally. I can't see any performance boost by using NoSQL instead of SQL. Maybe SQL over NoSQL, yes but not in that way.

Am I missing something about NoSQL?

3
  • 3
    If you can't see a performance boost, that's what you say. Fact is that most of the NoSQL solutions forgo one (or more) of the ACID properties of a relational database, so they do less.
    – Oded
    Commented Nov 12, 2012 at 9:58
  • 1
    There are some workflows (and data structures) that can't easily be mapped to a traditional ACID-enabled relational database. For those, you can see huge performance increases by using a NoSQL database. If, however, you simply take an existing (well-designed) SQL DB and put it into a NoSQL Database, then your performance will surely suffer. Commented Nov 12, 2012 at 9:59
  • 2
    The answer is: Has it been established as faster? And faster in what? Development time? Read time? Write time? Which type of write? What are we comparing it to? Multi-table queries? Joins?
    – Rolf
    Commented Oct 29, 2017 at 12:01

8 Answers 8

84

There are many NoSQL solutions around, each one with its own strengths and weaknesses, so the following must be taken with a grain of salt.

But essentially, what many NoSQL databases do is rely on denormalization and try to optimize for the denormalized case. For instance, say you are reading a blog post together with its comments in a document-oriented database. Often, the comments will be saved together with the post itself. This means that it will be faster to retrieve all of them together, as they are stored in the same place and you do not have to perform a join.

Of course, you can do the same in SQL, and denormalizing is a common practice when one needs performance. It is just that many NoSQL solutions are engineered from the start to be always used this way. You then get the usual tradeoffs: for instance, adding a comment in the above example will be slower because you have to save the whole document with it. And once you have denormalized, you have to take care of preserving data integrity in your application.

Moreover, in many NoSQL solutions, it is impossible to do arbitrary joins, hence arbitrary queries. Some databases, like CouchDB, require you to think ahead of the queries you will need and prepare them inside the DB.

All in all, it boils down to expecting a denormalized schema and optimizing reads for that situation, and this works well for data that is not highly relational and that requires much more reads than writes.

8
  • 4
    This, by the way can be realized with simple materialized view, or a cache layer, while still benefiting from all the SQL goodness. Anything properly modeled is relational, and logical data duplication is not a solution (the mat. view is a duplication but not a logical duplication because it is simply an image of something else).
    – Morg.
    Commented Oct 17, 2013 at 11:54
  • As I have said in the answer, one can do the same in SQL; it is just that when this becomes the rule instead of the exception, NoSQL databases are usually faster and more natural to use. In theory, SQL is the best model one can use, but when data grows over a certain size, it just cannot accomodate some models, and data duplication becomes faster and easier to reason about.
    – Andrea
    Commented Oct 17, 2013 at 13:55
  • 5
    That's bull. The relational model covers everything you can make in NoSQL and so much more. The only advantage of NoSQL is that a simple and inconsistent approach to scaling is built in and easy to use. It has nothing to do with SQL, and everything to do with not caring about ACID properties. You can have sync jobs between independent SQL nodes that will have exactly the same (very bad) scaling and consistency properties the NoSQL stores have. The difference is that SQL nodes can ALSO have consistency if you choose to.
    – Morg.
    Commented Oct 18, 2013 at 14:25
  • 1
    What if you have 5,000,000 million rows of data and you want to get the comment from all of them by some condition. Wouldn't it be faster if you had an index on the comment field of the table with SQL? Full-Text indexing would further improve this.
    – jwize
    Commented Dec 23, 2015 at 22:47
  • @morg - "The relational model covers everything you can make in NoSQL and so much more." Not really, no. There are plenty of examples of types of data that are such bad fits to the relational model that forcing the data into it results in massive inefficiency. Example: an online game has a facility for storing players inventory. The players have a finite set of numbered slots, each of which may store one or more items of a specific type. There are about 50 different kinds of item, each of which has 4-6 associated attributes, with some overlap, so there are about 80 possible attributes ...
    – Jules
    Commented Apr 30, 2017 at 18:24
32

The thing you are missing about NoSQL is that NoSQl cannot be compared to SQL in any way. NoSQL is name of all persistence technologies that are not SQL. Document DBs, Key-Value DBs, Event DBs are all NoSQL. They are all different in almost all aspects, be it structure of saved data, querying, performance and available tools.

So if someone asks you such question on interview, this should be the answer.

1
  • 5
    If there's one killer feature of NoSQL I'd say it IS the scalability. That's why the Facebooks and the Googles use it. Because of the gigantic volume of Data. NoSQL : when you have to deal with enormous amounts of Data.
    – Pieter B
    Commented Nov 12, 2012 at 10:35
16

'NoSQL' (or more precisely: non-relational) databases give up some features of the traditional databases for speed, but more importantly for horizontal scalability.

The missing features depend on the concrete product, in general full ACID properties or even join operations are not supported. That is the price for the increased performance.

1
  • 1
    Describing NoSQL as non-relational is not more precise. There are other old non-relational DBs that don't fall into category NoSQL. NoSQL means much more than just non-relational. Read this for further info: martinfowler.com/bliki/NosqlDefinition.html
    – eddyP23
    Commented Jul 19, 2017 at 9:04
9

You're right, it would be nonsense to state that in a blanket statement. Which is probably the whole point; instead of a single answer, the interviewer is probably expecting you to reply with questions to help you figure out what the context of the problem is (what kind of data, how much of it, in what operating environment etc), the particular NoSQL solution. They'll try to find out how you analyse problems and along the way get an idea how much you know about the different solutions that are out there.

1
  • Yes, it is a blanket statement, and if we accept it to be true, then the answer to the question is: it depends.
    – Rolf
    Commented Oct 30, 2017 at 13:22
5

NoSQL databases normally only make sense if you design your data around them.

If you intend to simply use them as a RDBMS replacement, then you might get less performance rather than more, especially if you don't have enough budget to pay for servers with high amounts of RAM.

Look at this article which compares MySQL disk space usage with that of MongoDB: http://blog.trackerbird.com/content/mysql-vs-mongodb-disk-space-usage

3

Which NoSQL database? Which SQL database? If someone tells you that NoSQL is faster than SQL then you should walk away. Or better yet watch this video:

http://www.youtube.com/watch?v=b2F-DItXtZs

I won't say half the things claimed about NoSQL are wrong, but I will say that there is a lot of NoSQL fanboyism out there from people who really don't understand it very well.

SQL has its limits (of course) but it also is a very mature technology, which is well understood, and has a large pool of developers who understand how to use it well. I can not say the same for all forms of NoSQL.

-2

NoSql supported by column oriented databases where RDBMS is row oriented database... And say for example we have a Employee table with Name, Age, Salery, Address, EmployeeId etc... we put same table in MySql (RDBMS support) and HBase (NoSQL support). If a customer/client writes a query to get the average Age or Salery details from 1Lakh employees records... what happens?

In RDBMS it will go around each row and collects the value and sum & divide for resulting. When it comes to Columnar database no need to worry about all one lakh row iterations. But deal with only one Row which is faster to compute. So this way sometimes NoSQL is faster than SQL. This case NoSQL don't care about ACID complaints are worth!

1
  • 2
    I've fixed the formating a bit, though I'm not sure what you are trying to get between the two. And ACID isn't always supported by RDBMS either.
    – user40980
    Commented Sep 4, 2013 at 4:14
-3

Forget theory around databases.... the point once you understand your querys, you can save data in nosql databases in an exact way in which they are actually used in your application....

For example take this example, u have a customer model with many orders and many items associated with each order, then they also have many saved items for later purchases... if you are a large ecommerce store with let's say 10 million customers and 50 million orders. And that customer logs into their dashboard which displays this exact data, how much work is a sql database going to need to do to find the customer, join the orders and each line item and saved items. In a sql database all this data will likely need to be joined in some way... or u can create a collection in ur database called usercache and save this data exactly how you use it in real life. So this can truely be a single query on a single field [id] to get all of this data back. On top of that the nosql database doesn't need to send the query to all 40 database servers to retrieve the information just the ones that actually have the data.

So can a sql db query a single Id field just as fast if not faster than nosql? Yes but can a sql database return all of the data you need by querying one table and one field? No, unless you do something like save the data in Json inside a large text field. But now that data is not query-able for potential future use.

Not the answer you're looking for? Browse other questions tagged or ask your own question.