Do databases rot the mind? Charles Petzold’s recent post about Visual Studio triggered this thought in the back of my mind.
All too often when confronted with a “business” problem I, like many other people I know, wind up reaching for my trusty database. Or, in a more common scenario, someone hands you an existing schema and says “solve my problem” using it. The reality, however, is that relational databases are a lousy solution to many problems that we commonly run across. In a recent e-commerce platform that I created there was a 7-way join that had to be executed just to retrieve a list of products to display to a customer. We did LOTS of performance tuning of that database and our queries to make it go about as fast as it could reasonably be expected to go under SQL Server.
Just this week, I decided to do an experiment. I reimplemented the core of our e-commerce platform by loading the entire database into a custom in-memory data structure. It took me about 1 hour to write the code to load the core tables into my custom data structure, and about an additional 15 hours to tune the data structure and refactor the code to my liking. Oh yeah, I wrote it using Ruby.
My app was 201 lines of Ruby code (including liberal use of blank lines to enhance readability). For sake of comparison, just one of the dynamically generated SQL queries that it replaced was in the order of 150+ lines of SQL. And that, of course, doesn’t count all of the lines of C# code required to generate, execute, and parse the results of the SQL query.
One more thing: it also ran 100x faster.
A bit of history: the original application that I inherited was on the order of 20,000 lines of SQL + C#. This was largely due to an enormous amount of duplication of code due to the (ab)use of stored procedures to handle queries. My rewrite of that application trimmed it down to 4000 lines of SQL + C#. My Ruby application, while not at feature parity with the existing application, solved essentially all of the “hard” problems. I would estimate that it duplicates >80% of the functionality of the real application.
The first version of the code was about 5x faster than the original application. I then spent some time looking for optimizations. When you only have about 100 lines of code to look through, optimizations become pretty obvious. I was able to cache the results of a (relatively) expensive O(M+N) algorithm that sat on the rate-determining-step of the computation. This netted me a 20x speedup. This optimization is simply not possible using SQL Server, but is dead-simple when you get to implement your own query engine.
I love the symmetry of my results: 1/100th the code and 100x the performance. It’s also much easier to maintain 200 lines of code than 20,000 lines of code. Testing was also much more straightforward.
Now the code that I rewrote was tailor-made for this kind of rewrite. It was a read-only database that was relatively (< 1GB) small in size. If you have such an application lying around – try rewriting it using in-memory data structures. If you’re a bit more adventurous, try using a dynamically typed language to do this (might I suggest Ruby?). You just might be surprised by the outcome and come away with a new technique in your toolbox by doing so.
8 archived reader comments
Matt (2005-10-31)
I remember from a few years ago, that a upcoming COM+ release had a new feature called -> In Memory Database -> IMDB. It was in the beta but never made it into the final release. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncomser/html/whatimdb.asp
The link above does give some insight as to why it did not make it in, but really in the end I think it was: Why duplicate the functionality of Sql Server? Unfortunately in many applications, bad database design is present (also possibly bad T-SQL, if using Sql server), and it is difficult or impossible to create a more normalized or efficient relational structure to replace the old one. While pulling the data into some custom code can do the trick, I have found that the following often increases the performance of Sql Queries: a) Use of temp tables to act as driver tables with less rows, and to cut down your large query into a series of queries with less than five tables in the joins b) Using narrower primary keys c) Changing isolation levels d) etc.
I am sure that all the above was tried, but also from a deployment standpoint, it is much easiar to deploy a stored procedure, than a piece of code. I am not a big advocate of placing business logic in Sql , but to just do the CRUD operations in a fast and efficient manner, my choice is always T-SQL.
John Lam (2005-11-01)
IMDB didn’t work because of 2 issues: a) SQL Server was faster and b) it did not support cache coherency.
Believe me, we tuned the hell out of that schema and query set. Even Bob Beauchemin and Dan Sullivan gave me some sage advice. But that wasn’t anywhere near enough.
And I assert that my deploying a Ruby script is a lot easier than your deploying a sproc :)
Udi Dahan - The Software Simplist (2005-11-05)
While I agree that databases are too quickly pulled out, I think that your example of “read-only” access isn’t representative of many apps. However, I agree that we shouldn’t necessarily continue solving todays problems with yesterdays solutions, especially without re-examining those solutions.
John Lam (2005-11-07)
Udi, bingo! Use the right tool for the job. The real point of this blog was to demonstrate a case where a “slow” language like Ruby can really help you think better. And when you think better, you can choose better algorithms - hence you can get the benefits of a much smaller code base and much higher performance.
J. Merrill (2005-11-18)
Have you looked at what they have at kx.com? (Caveat: I haven’t used it, but they seem to have been working on the “updateable in-memory database” for quite a while, with clients that had both hard requirements and success.)
Pościel Wełniana (2006-10-22)
buffering data into memory is a well-know trick, but what not always it is possible - especially when operating on very big databases. In such cases the key to success are maximally optimized base queries.
John Lam (2005/11/06)
Udi, bingo! Use the right tool for the job. The real point of this blog was to demonstrate a case where a "slow" language like Ruby can really help you think better. And when you think better, you can choose better algorithms - hence you can get the benefits of a much smaller code base and much higher performance.
J. Merrill (2005/11/18)
Have you looked at what they have at kx.com? (Caveat: I haven’t used it, but they seem to have been working on the "updateable in-memory database" for quite a while, with clients that had both hard requirements and success.)