Analogies to help explain views

A not-uncommon request I hear is to explain when the contents of a view are updated.
Sometimes this will be to explain the difference between a “view” and a “materialized view.”

I like analogies as a tool to help describe what I’m trying to say.

I wear glasses.
Querying through a view is like looking at your data through glasses.

When I look at the world without my glasses I get one image.
When I put my glasses on I get another way of looking at the same thing.
If somebody walks into the room where I am, I don’t need to wait for my glasses to update.
What I see reflects the contents of the room just as if I were looking without my glasses and there is no lag.
What I see with or without my glasses is “updated” instantaneously.

If I take a picture of the room with my camera, I get another view through that lens and the view is static.
These are materialized views. Not only is what I see determined by the lens of the camera, that image won’t ever change unless I force the view to refresh (take another picture.) Here the terminology is helpful. One image capture of a picture is called a snapshot, similarly one data capture with a materialized view is also called a snapshot.
In older versions of Oracle, the terms Materialized Views and Snapshots were used interchangeably.

So… if normal views and materialized views are updated instantaneously then why are materialized views often considered “faster” than normal views?
Well, that’s a trick question.

First, neither type of view is actually fast or slow.
A view holds no data, it’s simply a query filter. Like tinted glasses, a view doesn’t really change the data, it just applies an additional set of operations when you look at it.
In the case of glasses, the speed of light makes the analogy break down a little bit because it passes through the filters so fast we can’t perceive the operation.
If our glasses were hundreds of thousands of meters thick we’d be able to detect the difference between looking through the lens and not because the light would move slower through our glasses versus through the air.

In the case of a view, if we assume the view actually does something for us like join to another table, call a function, include a sub-query or a combination of these, then of course we’ll see a lag as opposed to simply looking at a single table’s data.

The view is able to instantly pickup changes in the underlying data; but when you query for that new data, your query still needs to process it through whatever joins, functions or sub-queries you use and it’s those operations that create the lag.

With a materialized view, you can hide some of the work by doing it ahead of time. I can drive to the Grand Canyon and look at it myself or I can look at a picture of it. The picture is faster because the data has already been recorded and delivered to me. However, the content is limited by the delay and scope of the captured image. If you could somehow deliver an image that was simultaneously as vast, detailed and up-to-date as looking at the real thing live, then your image would be completely equivalent to the original. However, that’s not really possible.

So that’s why I look through my own normal view (my glasses), rather than a materialized view.

If however, I merely need a minor update without the full detail, like an update on the current weather; I might we willing to use a small photo taken every hour from a web cam.
That snapshot isn’t completely accurate and doesn’t have all the information; but it captures what I need with adequate frequency to tell me what I need.

I hope these analogies help more than hinder and if I have confused, please feel free to ask and I’ll be happy to try help explain a different way.
If you have other analogies that you’ve used to help explain the topic to others, I’d like to hear about them.


Questions and Comments always welcome

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: