tags:

views:

497

answers:

6

After developing software for about 5 years now, I have spent probably atleast 20% and perhaps up to 40% of that time simply making a RDBMS able to save and retrieve complex object graphs. Many times this resulted in less than optimal coding solutions in order to make something easier to do from the database side. This eventually ended after a very significant amount of time spent in learning NHibernate and the session management patterns that are part of it. With NHibernate I was able to finally eschew the large majority of 100% wasted time of writing CRUD for the 1000th time and use forward generation of my database from my domain model.

Yet all of this work still results in a flawed model where my database is merely the best attempt by SQL to imitate my actual object. With document databases this is no longer the case as the object becomes the document itself instead of merely emulating the object through tables and columns.

At this point I'm really starting the to question why would I ever need SQL again?

What can really be done substantially better with SQL than a document database?

I know this is somewhat of leading into a apples to oranges comparison especially when you factor in the various types of NoSQL databases having widely different feature-sets but for the sake of this argument base it on the notion of NoSQL databases can inherently query objects correctly and not on the limitations of a key value store. Also leave out the reporting aspect as that should generally be handled in a OLAP database unless your answer includes a specific reason you would not use a OLAP database for it.

+24  A: 

Relational data modeling is a formal, mathematical solution for representing complex data without redundancy and without allowing anomalies. You can design an optimal database design from the data relationships themselves. This is the process of relational database normalization.

Non-relational data modeling has no formal way to define the best database structure from the data. You can design a database based on your anticipated usage; that is, your queries determine the best data organization, not the data itself.

In non-relational databases, you can never be sure that data conforms a certain document structure. You could have documents left over in the database from an earlier revision. So your application code had better be able to "discover" the structure of each document, perform conversions if necessary, and hope that references between data collections are satisfied.

In relational databases, you can depend on data integrity being an integral part of the model. If you design for normalization and you set up constraints properly, you know you'll never have orphans or data anomalies.

Non-relational databases give you one type of efficiency, as you're designing the database. Relational databases give you another type of efficiency, as you're using the database.

That said, the specific type of problem you've been working with -- object graphs -- is tricky to accomplish efficiently with plain SQL. But I think you'll find it's not much easier with NoSQL databases.


Re your comment: Granted, consistency is not a priority for every app. That doesn't make the value of consistency "insubstantial" for the apps where it is important.

You asked about why you would use relational databases -- you'd use them when the benefits of relational databases fit the priorities of your project.

Don't drive a nail with a screwdriver, and don't turn a screw with a hammer. There's an appropriate tool to solve each type of problem.

Bill Karwin
I find some this argument's primary points to be somewhat unsubstantial, the notion of orphaned data can be handled just as correctly through your application as the database itself. Besides orphaned data being allowed/not allowed is more of a business decision to start with. The argument of versioning documents just as clearly correlates to the notion of versioning database schemas. I don't see how these 2 factors are in anyway that different between each other.
Chris Marisic
There are document stores that meet the ACID definition.
Chris Marisic
There's plenty of data patterns and consistency criteria not expressible in relational model. Like anything involving transitive closures (every node can be reached from root node) - quite ironic for something that calls itself "relational". Things that can be cleanly modeled relationally are rare outside textbooks.
taw
I'd really like to know why people are upvoting this answer.
Chris Marisic
@Chris Marisic: because it describes the differences between RDBMS and NoSQL. Each is a tool with pros and cons: neither is the end in itself. If you didn't want a balanced answers or someone to defend RDBMS then you should have stated you only want answers that agree with you. Or not asked.
gbn
@gbn IMO this post seems to be made with inaccurate assumptions about NoSQL databases, most specifically that it is inherently not capable of supporting ACID fully.
Chris Marisic
@Chris Marisic: I'm interested if you have an example of a non-relational database that supports consistency. Specifically, constraints for referential integrity. I'm not denying there may be one such implementation, but it is not typical among databases using the NoSQL brand.
Bill Karwin
RavenDB is full ACID.
Chris Marisic
@Chris Marisic: You keep mentioning ACID, but I'm not talking about ACID. I'm talking about consistency. Perhaps I should have used the word *integrity*. I'm talking about data modeling, not transactions. I don't believe any schemaless database can enforce data integrity.
Bill Karwin
Yes you can achieve referential integrity and store references to other documents inside a document. However I assume you could probably physically delete documents that would cause data to be disjoint, but this goes back to my earlier statement this is a business decision on whether this is wrong or not. If it's wrong you make your application not allow it and don't do it as an admin.
Chris Marisic
@Chris Marisic: Ah, the "do it in the app" argument. My point is that when you can define RI constraints in a relational database, you don't have to worry about whether your documents contain broken references. Performing that enforcement in the app is insufficient, because apps can have bugs. Maybe another developer is writing his own app to access your same data, and he doesn't know which constraints to implement. So you can never be sure your data has integrity unless the database is in charge of enforcement. And that also means you need to define a schema.
Bill Karwin
"So you can never be sure your data has integrity unless the database is in charge of enforcement." Is bullshit. Not to mention as a developer I could easily execute sql to drop the constraints and do what I wish anyway. Intentionally altering data is not an error, it's a business action whether a correct or incorrect one. Not to mention when the database dictates its "constraints" you end up with inserts that fail you have 100% data loss. I'd much rather have a partial document instead of 100% data loss.
Chris Marisic
@Chris Marisic: Straw man argument.
Bill Karwin
I would find your argument to be the straw man argument. You're stating an application isn't capable of enforcing referential integrity, what else is a database other than an application?
Chris Marisic
@Chris Marisic: I did not say an application isn't capable of enforcing RI. Of course your apps are bug-free and flawless. But what about the schmuck in the next cubicle? If he can't program *his* app to do the correct RI enforcement, you have a problem. That's when it's advantageous to have a database schema that blocks invalid changes. Remember, you asked for a case where an RDBMS has an advantage over NoSQL/schemaless.
Bill Karwin
A: 

When I've investigated noSQL-style databases, I found that they did not provide ACID, nor did they provide relational features(not being relational databases). Since I like data consistency, and I have usually wanted some sort of relational feature, I've not selected noSQL databases.

However, I don't use the ORM tools out there, I tend to write SQL itself.

Paul Nathan
What sort of "relational feature"? That you can do joins on the data?
Chris Marisic
ACID and relational are orthogonal. Both [non-relational ACID](http://en.wikipedia.org/wiki/Berkeley_DB) and [non-ACID SQL](http://en.wikipedia.org/wiki/MyISAM) are in widespread use.
taw
@taw you should make your comment into an actual answer on this thread.
Chris Marisic
+18  A: 

At Amazon I worked with a lot of code. Most of the code I worked one was code nobody really understood anymore. It was riddled with special case handling that wasn't well understood because it was an accretion of quick patches over a long period of time. If you wanted to fully understand the effect of a change you were making you were out-of-luck. In essence, you were forced to add to the accretion.

I also worked with a lot of data. The structure of the tables in SQL made excellent long-term documentation for the data. The database was relatively easy to work with directly, and the structure of the data made sense. There were people who's job it was to manage the structure and integrity of the data.

I fear that a NoSQL database, with its lack of well-documented structure, would slowly acquire all the evil qualities of the code I worked on. It would end up filled with data from old structures that nobody really understood anymore, and become a vast patchwork of mostly useless garbage.

I see the main benefits of SQL databases as the forced documentation that maintaining the database structure and consistency rules requires. Those benefits do not have an easy short-term measure like speed of a query or transactional consistency. They are long-term benefits that affect the usefulness of your data over an extended period of time.

As a second, related point, I find it more useful, when using ORMs and the like, to map out my data and then decide how that will translate into objects in the application I'm writing. The data and its relationships represent a long-term archival structure that may be used for a variety of purposes.

The structure of the object relationships in the application are there for the purposes of that application. A given set of data represented in SQL tables and relationship constraints will have many possible object models that represent it in an application, and each of those object models will reflect the goals of that particular application. But the data and its structure exist independently of any given ephemeral use that might be made of them.

I see the arguments people make about 'reporting' as being arguments that different applications can usefully view the same set of data in very different ways.

Personally, I think SQL is a good model to use directly for archival data, infrequently modified data, or data with extremely high consistency requirements. And I think that I will continue to use relational algebra to define the overall structure of my data even if I'm storing it in a NoSQL database. And I will not change the structure of the data in the NoSQL database without first modifying the relational structure describing it. This will allow me to map my NoSQL databases back to SQL so I can still use SQL for long-term storage and warehousing and force me to maintain the data structures in a well documented form.

Doing things this way will also assist me when I have to pull data out of the NoSQL database for use in applications that were not envisioned when the database was created.

Of course, there is some data who's structure naturally fits NoSQL and where generating a relational schema for it would be pointless. For example, storage of actual documents, storage of pictures or other media, or other large blobs of data that has no structure that might be useful to represent. This distinction is very tricky though. Pictures and movies do have structure to them, just not generally structure you need to store in a database. A blog post may have structure as well if you have a system designed to try to read and understand it, and that may well be structure you want to maintain a record of.

Omnifarious
"And I will not change the structure of the data in the NoSQL database without first modifying the relational structure describing it. This will allow me to map my NoSQL databases back to SQL so I can still use SQL for long-term storage and warehousing." This seems like a nearly inordinate amount of work, wouldn't this effort better spent just building and maintaining a proper import process for the datastore -> datawarehouse instead?
Chris Marisic
@Chris Marisic: And what happens when the importer program becomes something that someone needs to spend a few weeks to understand? No, IMHO, it's vitally important that you always have a really good handle on exactly what data you have in your database, what it means, and how it relates to the other data. Keeping an SQL schema (or any schema really) of it outside the database is a means for achieving that.
Omnifarious
I awarded you the bounty because using a database schema as a rigid model of your domain while might not be something I'd do definitely offers a case where Sql is substantially better suited for.
Chris Marisic
+3  A: 

it depends on what you are trying to do. when you need to do searching on different fields of your objects then SQL is good. if you don't need to do searching and you have very complex polymorphic tree like structures then SQL is horrible.

i've worked on app that allowed users to build web pages by joining little fragments together and the original serialization used key/value SQL tables. all the fragments had properties which were stored (fragment, property, value). so schemaless but still a lot of heavy lifting. probably the worst of both worlds because you don't really get much data validation from the database, it is very difficult to look at the tables and understand what is going on and there is still a lot of work to write it to the db and read it back.

we've also done a similar app but we learnt our lesson and we just take plain java classes and encode them using JSON. the user just edits their page in the front in a rich ui. clicks save and the whole page is sent back to the server as a json object. the server then does validation on the object to make sure all the constraints are correct which should always be true unless a user has been tampering or there is a bug in the code. then the object is written to a row by encoding to back to json.

this works well for us because we never want to deal with part of the object. we always deal with the whole of the object so JSON is not only easier but it is faster than doing the 40+ queries on each read we would have to do if it was properly normalized.

drscroogemcduck
A: 

Tooling is much better for SQL. NoSql has a buggy reputation. But even assuming those two differences even out...

I have the opposite experience from you in modeling complex objects in SQL. To say that tables and columns are at best an 'emulation' of your objects, that's a bit semantic. Any serialization of your objects would also be an emulation: While a document database or xml or whatever may feel like a better emulation than tables/columns, it tends to be less powerful technology. ORMs have helped immensely to bridge the gap from RBDMS to object oriented languages.

Since relational theory was formalized, SQL has been king. Hierarchical dbs (which document databases are) lost, relational dbs won. I would ask yourself, given that history, is your problem all that different from the majority of problems over the last 30 years that you need to revert to hierarchical form?

NoSql dbs are hip now for problems that require horizontal scaling (which SQL doesn't do well now). Does your problem require that?

Shlomo
I would disagree that serialization is an emulation of objects as it is in truest form a physical representation of the objects. Whereas using tables/columns is an emulation of storing the data in a way similar to serialization but is not serialization. In regards to the question about the last 30 years about SQL being king is in no way a validation of it being objectively better than other databases. In 30 years programming has become fundamentally object orientated which even in its early days was the reason hierarchical databases were created.
Chris Marisic
Prior to current times non relational databases just generally weren't successful. This could be from any number of a reasons whether the lack of need for horizontal scaling obscured the major flaws in SQL databases, poor marketing of the need of other tools, lack of adoption from business, etc any and all of these and more contributed to previously relational databases being accepted as the only real solution but times change and tools evolve and from everything I've seen this year nonrelational databases will have significant growth in usage over SQL because SQL sucks for developers.
Chris Marisic
A: 

My key question was where would a SQL database really outshine a document database and from all the responses there really doesn't seem to be much.

Given that NoSQL databases come in just as many variations of types of databases as relational that both match all or some parts of ACID depending on which database you use that at this point they are basically the equitable for solving problems.

After this the key differences would be tooling and maturity which SQL databases have a much larger grasp in for being the established player but this is how it is for all new technology.

Chris Marisic
Don't forget you took away two important arguments: Query flexibility/Reporting and the lack of power of key/value store. Basically, you can boil your question down to, is Sql without all that's good about it better than NoSql without all of its problems.Also from a business perspective (leaving aside technology), an important factor is community adoption, and any SQL implementation is still light years ahead of NoSql in that regard.
Shlomo
A key-value store is a very special tool used for specific purposes and has never been meant to replace a SQL database. I did not state any where about query flexibility, I stated about reporting which should in theory be done in an OLAP database, not a relational database.
Chris Marisic
http://en.wikipedia.org/wiki/ROLAP
coolgeek
@coolgeek that article ever further resounds my point of not using a normal database for reporting, FTA "While ROLAP uses a relational database source, generally the database must be carefully designed for ROLAP use. A database which was designed for OLTP will not function well as a ROLAP database. Therefore, ROLAP still involves creating an additional copy of the data. However, since it is a database, a variety of technologies can be used to populate the database." So while it might leverage a direct relational database for the storage it still requires you to maintain an OLAP database.
Chris Marisic