News for hackers. Stuff that matters.
Re:Why use a database instead of just saving your data to disk? by tptacek on Sunday May 19, 2013 @03:14PM (#5734054)
Worth noting that Hacker News is a "store it all in files" design.
Re:Why use a database instead of just saving your data to disk? by stalcottsmith on Sunday May 19, 2013 @05:07PM (#5734391)
Well structured relational databases are an awesome tool. I have built and used hundreds. But they are a bit of cargo cult architecture. Deciding to use one means adding lots of code. It adds a lot of complexity around testing. All of it multiplies. It's another big Artifact to maintain and you may have to decide how you will scale it, replicate it, make it highly-available, etc. NOSQL was the first crack in the seemingly unassailable assumption that you need to use a DB. Now I think a few of us are questioning whether to use a datastore at all. After all, once you've dropped SQL for some functions, why stuff them into yet another opaque datastore? You might end up having to use a relational database anyway at some point and then you're stuck with another piece of junk and all its conceptual overhead and extra code. Start with files and introduce a relational database only when necessary.

Formats are straight forward these days. Just write out JSON, XML, HTML5 w/ microformats or even Yaml. The art is in the "schema" or how you store the files. Choose wisely with some knowledge of your requirements and it seems like it can work well.

Files on disk mean you can use all your great UNIX tooling to do all sorts of complex operations that would take you many many hours of skilled development to do with a database. Then there are all the possible things you could do with Git or (imagine!) ZFS! Versioning everything for free? Keep an activity log within a hierarchy. What kind of cool stuff can you do with that? I aim to find out.

Re:Why use a database instead of just saving your data to disk? by jacques_chester on Sunday May 19, 2013 @08:55PM (#5735197)
Your argument makes perfect sense.

If all you need is storage and retrieval.

Take storing project management data as JSON files.

Suddenly the boss bursts in: he needs to know, right now, how many hours Jeremy has spent working on these three projects.

Given that your JSON hierarchy runs project->person and not person->project, you will now have to traverse your entire database to give an answer.

The deep magic of relational algebra or relational calculus is that they privilege no one view of the data over the other.

This is, incidentally, one of the causes of OO/relational mismatch. A class hierarchy locks in a particular view of the problem domain. Relational has many potential views of the problem domain.

The cost and inconvenience of traversing trees for ad hoc queries is one of the big reasons that RDBMSes swept away network and hierarchical databases.

Re:Why use a database instead of just saving your data to disk? by ucee054 on Sunday May 19, 2013 @08:27PM (#5735115)
Formats are straight forward these days. Just write out JSON, XML, HTML5 w/ microformats or even Yaml.

And when my data grows to multiple terabytes in size?

Re:Why use a database instead of just saving your data to disk? by guard-of-terra on Sunday May 19, 2013 @03:51PM (#5734155)
pg is a big proponent of plain files, as explained in his stories about viaweb.

There are many places where you want ad-hoc databases. You better bet on it being read-only or append-only, but in practice it is often so anyway.

Re:Why use a database instead of just saving your data to disk? by ExpiredLink on Monday May 20, 2013 @06:40AM (#5736874)
> You better bet on it being read-only

Yep, CRD instead of CRUD. 'U' would be a recipe for disaster.

Re:Why use a database instead of just saving your data to disk? by ucee054 on Sunday May 19, 2013 @05:26PM (#5734452)
I don't think pg understands the issue at a gut-feel level.

There is a lot of benefit to processing your data using a declarative language, which is what SQL is. It's basically prolog, with less recursion and more separation of code and data.

You get all of that power for all of the data manipulation code that you run inside the database. If you have the misfortune to be writing in VB or Java, that's a huge power-up that you gain by using an RDBMS.

But pg is already using a declarative language, because he programs in Lisp. So the advantages are fewer in his case.

So I doubt pg feels significantly empowered by an RDBMS.

Re:Why use a database instead of just saving your data to disk? by wildgift on Sunday May 19, 2013 @06:36PM (#5734695)
I never thought of SQL as declarative or like Prolog. I think it's more like a filter-writing language, with set functions like join.

Agreed that the distance between Lisp and SQL is a lot shorter so an RDMBS doesn't gain you very much. In functional languages, it's normal to write filters and apply functions to lists. You also end up performing your joins manually, and end up "optimizing the query" as you code.

In SQL, filters and apply (update) are easier to write, but it's also easy to perform a join that is slow.

Re:Why use a database instead of just saving your data to disk? by ksherlock on Sunday May 19, 2013 @10:40PM (#5735536)
Try reading up on relational calculus (and relational algebra). You may find it enlightening
Re:Why use a database instead of just saving your data to disk? by ucee054 on Sunday May 19, 2013 @08:10PM (#5735045)
I never thought of SQL as declarative or like Prolog.

Look at the different forms of relational calculus (SQL is intended to be basically syntactic sugar on top) or look at some research papers by Fariba Sadri - hell, look at the earliest papers on relational databases by EF Codd. RDBMSs were meant to be logic systems right from the start.

it's also easy to perform a join that is slow

Have you been using MySQL? Use a real database with a working query optimizer eg I know from experience that MS SQL will do its best even when you give it a dog of a query. For example it will re-arrange your "filter" and build a temporary index to make your join go at the fastest possible speed.

If you have a specific use case nailed down you can always make it go faster outside of an RDBMS much the same way that you can always make something go faster if you hand-code it in machine code instead of using a compiler.

But if you can't put that much effort into hand-optimizing things, say because your use case might change so you'd have to change the code, the query optimizer/compiler will do a better job.

Re:Why use a database instead of just saving your data to disk? by guard-of-terra on Sunday May 19, 2013 @06:29PM (#5734667)
Declarative often means opaque and hard to debug. SQL is just that over huge datasets. With properly partitioned text files, at least you know you're doing a full scan of a small partition that is practically real-time.
Re:Why use a database instead of just saving your data to disk? by ucee054 on Sunday May 19, 2013 @08:19PM (#5735079)
I don't think this is true at all ... you use the same query irrespective of the size of the data in the tables. So the size of the datasets is irrelevant except for surfacing performance problems.

In the example you gave you shouldn't have problems if you put the right index on the table.

What really causes the opacity is either (a) the complexity of the query or (b) the guy writing the clearly doesn't know how to write declarative code, so he tries to write eg procedural code in SQL, with cursors and triggers and other horrors.

If you are in scenario (a) and you really do need to do something complex, I'd pick SQL over VB to do it in any day - the non-declarative style leads to huge code with side effects are more opportunities for bugs to creep in.

If you are in scenario (b), the guy writing the queries really needs to read this: http://www.amazon.com/Database-Management-Systems-Raghu-Rama...

Re:Why use a database instead of just saving your data to disk? by wglb on Sunday May 19, 2013 @11:45PM (#5735764)
I am at a loss to understand I don't think pg understands the issue at a gut-feel level.

And Lisp as a declarative language?

SQL being basically Prolog?

Re:Why use a database instead of just saving your data to disk? by enqk on Tuesday May 21, 2013 @05:01PM (#5746799)
It has been very helpful to me to consider SQL being an approximation of the prolog model. It made the understanding and creation of schemas and queries much easier.
Re:Why use a database instead of just saving your data to disk? by mootothemax on Sunday May 19, 2013 @03:33PM (#5734107)
Do you know if that's why user comment averages (as on our profile pages) aren't updated in real time?

Seems like that's the kind of thing which is very easy with a database and much less so with flat files.

Re:Why use a database instead of just saving your data to disk? by joosters on Sunday May 19, 2013 @04:27PM (#5734270)
It might be very easy to code with a database, but the DB still has to do a lot of behind-the-scenes work, so you might not want them updated in real time anyway. (n.b. this is all theoretical, I have no idea about the workings of HN)

You could argue that this is another plus for flat files; you have a better appreciation for what operations are hard work, whereas a 'simple' one line select/join can hide a huge performance blocker...

Re:Why use a database instead of just saving your data to disk? by Detrus on Sunday May 19, 2013 @03:45PM (#5734141)
I think everything is in RAM and backed up to disk, Redis style. Probably only keeps recent things in RAM.
Re:Why use a database instead of just saving your data to disk? by qu4z-2 on Sunday May 19, 2013 @10:12PM (#5735454)
I don't really see why that would be significantly harder with flat files.
Re:Why use a database instead of just saving your data to disk? by mbesto on Sunday May 19, 2013 @04:21PM (#5734250)
I suspect this is largely due to the nature of the most of the data here, i.e. documents as opposed to data.

In it's simplest, this is why NoSQL was created, right?

Re:Why use a database instead of just saving your data to disk? by tptacek on Sunday May 19, 2013 @04:32PM (#5734284)
I'm not a fan of this particular design choice; I think the site would be better if it was backed by SQL.
Re:Why use a database instead of just saving your data to disk? by mbesto on Tuesday May 21, 2013 @07:24AM (#5742943)
Does the site satisfy all of your needs as a user? If the answer is yes, then it's non issue.