top | item 887605

Databases Should be Dynamically Typed

14 points| sharksandwich | 16 years ago |blog.mongodb.org | reply

30 comments

order
[+] seldo|16 years ago|reply
(I also commented on the article, but nobody seems to be commenting over there, so reposting...)

The advantage of statically typed RDBMS that he's leaving out here are (a) storage optimization (b) query optimization (as jawngee has already noted).

You could get halfway to simulating a dynamically-typed RDBMS by declaring all your columns as, say, VARCHAR(5000). You could store strings, integers, floats, dates, etc. all in there pretty simply. However, they would use a lot more storage space as strings than they would as native data types (e.g. integer 1000 is one byte, string '1000' is 4). Over a large data set that would really add up.

Secondly, when doing queries, your comparison operators (e.g. WHERE date > 2009-07-06) would be way less efficient as string comparisons than native type comparisons.

I don't want to be dismissive and curmudgeonly about this, but over and over what I hear from people enthusiastic about NoSQL solutions is that they solve the current problems of RDBMS, while forgetting all the great features that we have spent the last 30 years building into database systems.

The current "limitations" of SQL-based systems are often in fact age-old trade-offs that we made, but people have forgotten the positive benefits of those trade-offs. The reason databases are statically typed is because that saves storage space and processing time. The reason there's a standardized, domain-specific language that you have to learn is because having to learn a completely new API and mental model every time you want to access a data store from a new vendor is inefficient. Oh, and the reason SQL is so complicated is because relational algebra is complicated.

Sure, SQL and RDBMS have their limitations. They're not the right tool for every job, and they are not even 100% perfect at the jobs where they are the right tool. But too often I hear people saying "fuck SQL!" simply because they don't want to learn it, and because they're too early on in their little pet project to realize the scalability problems a NoSQL system is going to run into that RDBMS solved 20+ years ago.

[+] evgen|16 years ago|reply
> The reason databases are statically typed is because that saves storage space and processing time

The problem with this argument is that when I can slap several TB into every node in my db cluster the storage cost issue is basically moot. When it is cheaper to buy ten small boxes than one beefy server the processing time and query optimization issue also starts to come into question. At some point soon, and we may have already passed that point, the cost of centralizing db queries through a small number of expensive, highly tuned servers necessary to maintain the pillars of the RDBMS model will be outpaced by the benefits of abandoning both optimizations in favor of being able to use a small, cheap fleet of boxes running a distributed database.

The tradeoffs that were made several decades ago when the RDBMS and SQL ascended to their current dominance may have made sense at the time, but a lot has changed since then. You may try to delude yourself into thinking that there are "scalability problems a NoSQL system is going to run into that RDBMS solved 20+ years ago" but given the fact that almost all of these systems were designed with an eye on under what conditions RDBMS failed and in light of decades of research into distributed systems and scalability that did not exist when SQL/RDBMS emerged I find it hard to believe that there are scalability problems that RDBMS solved that NoSQL systems will run into. Hell, most of them were designed to solve specific scalability failures in big data systems where RDBMS fell over and died.

[+] bayareaguy|16 years ago|reply
integer 1000 is one byte

What sort of byte? Mine only represent integers from 0 to 255.

More seriously, I don't buy your implied argument that plain text representations are less compact. Whether or not this is true depends more on the specifics of your domain and the system you use. For much of the data I work with the strongly typed database representation is often larger then the plain untyped (ASCII) source.

[+] jseifer|16 years ago|reply
I'll bite. Use the right tool for the job. I think that's what things have always boiled down to. It just so happened that sql was the right tool for the job a lot of the time in the web app space. It still is.

But now we're at a place where there are a lot of options. You can use CouchDB, Tokyo cabinet/tyrant, redis, mongodb, and a host of other alternative storage engines. You don't have to drop your sql database, you can supplement it and take some of the load off of your current db.

I think the whole "right tool for the job" thing takes on new meaning these days. The right tool for the job could now be a combination of sql, mongo, and tokyo cabinet. It depends on the job, the app, and the requirements. But I wouldn't dismiss any NoSQL option so quick.

[+] omouse|16 years ago|reply
I'm curious...what about having optionally statically typed databases? Like so you can turn on the type checking and optimizations on a per-table basis?
[+] jawngee|16 years ago|reply
<sarcasm> Yeah that would be totally awesome! Now our databases can't optimize our query plans and any aggregate functions will be doing super cheap type conversions between strings and number types!

And, really, who cares about data consistency? Sure that stupid little script is inserting strings into what should be a numeric column, but I'm sure that won't cause any problems at all when the monthly reports run, or the batch processes that charge our customers run, because the database will know how to handle such cases because the DB developers have written all that extra logic in it for such cases. </sarcasm>

<reality> Stop being lazy. </reality>

[+] reg4c|16 years ago|reply
Data consistency could be maintained through stronger runtime input verification which in turn nulls everything you said.

I don't think that it would be a bad idea to have dynamically typed databases although it would put a little more strain on developing the actual application instead of having to spend time on making the database.

[+] noblethrasher|16 years ago|reply
In fact, I've just come to realize to the benefits of user defined data types, especially for primary keys. You can treat tables as functions and dynamically discover the ways in which you can compose them (i.e. create queries).
[+] neilc|16 years ago|reply
even with a statically typed database, type matching errors storing data are only reported at runtime! (That is, our java compiler doesn’t check our MySQL schema.)

That isn't necessary the case: for example, PG'Ocaml allows Ocaml programs that access a PostgreSQL database to have their type consistency checked at compile-time.

http://developer.berlios.de/projects/pgocaml/

[+] ct4ul4u|16 years ago|reply
How does he get from the the text of the article, which approximately says "It would be useful in some cases for a database to be dynamically typed" to the sweeping statement "Databases Should be Dynamically Typed".

As others have noted, relational databases have a rigorous theoretical foundation that is not consistent with what he describes as dynamic typing. He should consider looking at a graph database. Many of these are billed as "RDF Stores" or "Triple Stores". Franz's AllegroGraph is an excellent example.

[+] parenthesis|16 years ago|reply
[+] wvenable|16 years ago|reply
Hey, is there anyone out there that uses SQLite and actually uses it's "duck typing" feature to some effect?

I'm using it, but I still declare my "column affinity" and store values with the proper types. I don't put strings in my number columns, for example. I can't even imagine a good reason for that.

[+] elblanco|16 years ago|reply
And it's a fantastic free (public domain licensing anyone?) embedded dB with readily available support and custom dev services at the end of the phone.
[+] bayareaguy|16 years ago|reply
One situation that calls for a typeless database is when the system must accept data from a source that's unwilling or unable to adhere to a well-defined static schema.
[+] timwiseman|16 years ago|reply
Perhaps, but then perhaps a database is not the proper store for that at all, and a relational database almost certainly isn't.

If you really must do it via a relational database, you could use something equivalent to varbinary(max) to accept whatever stream of bits is sent in and then parse them out into something meaningful later (or just store it for the applications retrieval).

[+] wvenable|16 years ago|reply
When in doubt, serialize to a string. The end result compared with a type-less database is nearly the same.

The age old computing phrase "garbage in, garbage out" applies here.

[+] ecq|16 years ago|reply
you can use SYS.ANYDATA and SYS.ANYDATASET if you use Oracle. I'm sure other databases support similar feature.