Would the following approach be reasonable?
The Goal: queries have to be relatively fast, but without the hassles incurred by writing to a de-normalized database.
The Context: A database for handling user profiles for some CMS. User profiles sometimes change, but are not updated too regularly; so for the most part the information for each user remains the same for some time.
The Database Design:
First, we put together a highly normalized database, call it "
N
". User profiles are stored inN
.Being highly normalized,
N
is secure and elegant to update, but slow to query. So we take all the data stored inN
and we create a single de-normalized table. We call this tableT
.T
is used for the sole purpose of retrieving user information. It is only read from, not written to, because it's prone to anomalies and would be a mess to update.
With one exception:
- To table
T
, we add a boolean column, "hasChanged
", default value "FALSE
".
The CMS's algorithm for handling queries:
Suppose we want to load user 47's profile.
If
hasChanged == FALSE
for a row withID == 47
, we can safely retrieve the user profile from this row. (Because we know nothing has changed.)If
hasChanged == TRUE
, then row 47 inT
cannot be trusted (something has changed), so the query is passed on to the reliable but slowN
.When a user updates his profile,
N
is updated. WhenN
is updated,hasChanged
is set toTRUE
on all rows inT
containing information that could be affected by this change.T
is completely rewritten by the system whenever it becomes too unreliable (this is determined by how frequently queries are passed on toN
).