17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

About Schema Binding<br />

Schema binding essentially takes the things that your view is dependent upon (tables or other views),<br />

and “binds” them to that view. The significance of this is that no one can make alterations to those<br />

objects (CREATE, ALTER) unless they drop the schema-bound view first.<br />

Why would you want to do this? Well, there are a few reasons why this can come in handy:<br />

❑ It prevents your view from becoming “orphaned” by alterations in underlying objects. Imagine,<br />

for a moment, that someone performs a DROP or makes some other change (even deleting a column<br />

could cause your view grief), but doesn’t pay attention to your view. Oops. If the view is<br />

schema bound, then this is prevented from happening.<br />

❑ To allow indexed views: If you want an index on your view, you must create it using the<br />

SCHEMABINDING option. (We’ll look at indexed views just a few paragraphs from now.)<br />

❑ If you are going to create a schema-bound user-defined function (and there are instances where<br />

your function must be schema bound) that references your view, then your view must also be<br />

schema bound.<br />

Keep these in mind as you are building your views.<br />

Making Your View Look Lik e a Table with<br />

VIEW_METADATA<br />

This option has the effect of making your view look very much like an actual table to DB-LIB, ODBC,<br />

and OLE-DB clients. Without this option, the metadata passed back to the client API is that of the base<br />

table(s) that your view relies on.<br />

Providing this metadata information is required to allow for any client-side cursors (cursors your client<br />

application manages) to be updatable. Note that, if you want to support such cursors, you’re also going<br />

to need to use an INSTEAD OF trigger.<br />

Indexed (Materialized) V iews<br />

Chapter 10: Views<br />

When a view is referred to, the logic in the query that makes up the view is essentially incorporated into<br />

the calling query. Unfortunately, this means that the calling query just gets that much more complex. The<br />

extra overhead of figuring out the impact of the view (and what data it represents) on the fly can actually<br />

get very high. What’s more, you’re often including additional joins into your query in the form of<br />

the tables that are joined in the view. Indexed views give us a way of taking care of some of this impact<br />

before the query is ever run.<br />

An indexed view is essentially a view that has had a set of unique values “materialized” into the form of<br />

a clustered index. The advantage of this is that it provides a very quick lookup in terms of pulling the<br />

information behind a view together. After the first index (which must be a clustered index against a<br />

unique set of values), <strong>SQL</strong> <strong>Server</strong> can also build additional indexes on the view using the cluster key<br />

319

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!