When bad things happen to good schemas

June 06, 2006 · 0 comments

The schema that a couple of my projects rely on were developed by a partner company, for use in their product.

Having worked with them for a couple years, and been frustrated by the refusal to address performance issues, which greatly inhibited scaling and UI design, I spent the afternoon de-normalizing the schema to improve performance.

Surprise, surprise. The performance increased by about 70% and the my code was reduced by an equal amount.

Being a pragmatic self-taught engineer has its upsides. You tend to view the common wisdom that permeates the technical world with skepticism.

The other side of the coin is, when you find something that make sense, your gut tells you to go with it long before it becomes common wisdom.

In this case the Ruby on Rails philosophy, name things what they are in your schema.

Too many schemas have convoluted naming formats. Usually requiring intimate knowledge of the code and functionality to fully get it.

In this case, the partner company named their tables in a manner that over time blinded them to the actual usage of the data.

They kept talking about implementing metadata tables to improve performance.

I explained that a metadata table already existed, they just stored too much in that table that existed elsewhere, and didn’t properly use relationship to make queries fast enough.

For example:

Content -> Messages -> Users

Users as sender belonged to Messages

Users as receivers has-and-belonged-to Messages through a join table

The point they missed was that Content was a Message, and Messages contained metadata about the Content.

The performance issue was solved by removing all non-relevant data from Messages. Moving the Sender and Receiver relationships directly to Messages, without any join tables.

It meant that every Receiver would have their own Message entry, which is logical. While still storing only one copy of the actual Content.

No more SQL gymnastics to determine which Receiver owns what, or when a certain Receiver viewed their version of the Messages, or performing searches based on Content, Messages and Receivers.

0 responses so far ↓

  • There are no comments yet... Kick things off by filling out the form below.

Leave a Comment