Home All Groups Group Topic Archive Search About
Author
7 Sep 2006 1:23 AM
SQL Ken
Can anyone explain to me how does a vew work internally?

If a view is a replicate  of a table, will it be faster to query from a
table then a view?

Thanks

Author
7 Sep 2006 1:38 AM
--CELKO--
>> Can anyone explain to me how does a VIEW work internally? <<

The SQL Standard requires that it act as if it is materailized when
invoke.  The actual SQL products can do whatever they want to get that
effect.

The three most common approaches are:

1) Copy the SELECT statement body into the invoking query like a
derived table.

2) Materialize the SELECT statement body in a working table for that
session.

3) Materialize the SELECT statement body in a working table for all
sessions using the VIEW and share it.

If there is  a WITH CHECK OPTION, things change.  No, before you ask, I
am not going to tell you.  You actually will have to make an effort to
learn that on your own.
Author
7 Sep 2006 1:41 AM
Tom Moreau
A view doesn't normally contain data.  It is just a query.  However, an
indexed view does contain data and *may* improve your query performance.

--
    Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON   Canada

"SQL Ken" <SQL***@gmail.com> wrote in message
news:1157592184.497564.25560@b28g2000cwb.googlegroups.com...
Can anyone explain to me how does a vew work internally?

If a view is a replicate  of a table, will it be faster to query from a
table then a view?

Thanks
Author
7 Sep 2006 5:41 PM
SQL Ken
Tom,
If a view and a table are indexed properly,
shouldn't  querying  from a table be faster because it go directly
against the table where the querying from a view require an additional
loop?
For example
query from a table >>

SELECT  *
FROM    table1

where as
Query from a view would be equivalent to >>

  SELECT *
  FROM
          (SELECT *
           FROM table1
          ) MyView


Thanks

Ken


Tom Moreau wrote:
Show quote
> A view doesn't normally contain data.  It is just a query.  However, an
> indexed view does contain data and *may* improve your query performance.
>
> --
>     Tom
>
> ----------------------------------------------------
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
> SQL Server MVP
> Toronto, ON   Canada
>
> "SQL Ken" <SQL***@gmail.com> wrote in message
> news:1157592184.497564.25560@b28g2000cwb.googlegroups.com...
> Can anyone explain to me how does a vew work internally?
>
> If a view is a replicate  of a table, will it be faster to query from a
> table then a view?
>
> Thanks
Author
7 Sep 2006 6:04 PM
--CELKO--
>> If a view and a table are indexed properly, shouldn't  querying  from a table be faster because it go directly against the table where the querying from a view require an additional loop? <<

No, not always.  Consider this skeleton:

CREATE TABLE Personnel
(emp_id, emp_loc, ...);

CREATE VIEW NYC_Personnel ( ..)
AS
SELECT emp_id,  ...
  FROM Personnel
WHERE emp_loc = 'New York City';

The query

SELECT N.emp_id, F.stuff, ..
  FROM NYC_Personnel AS N, Foobar AS F
WHERE F.emp_id = N.emp_id;

Will be converted to this by every optimizer I know.  :

SELECT N.emp_id, F.stuff, ..
  FROM  Personnel AS N, Foobar AS F
WHERE F.emp_id = N.emp_id
  AND N.emp_loc = 'New York City';

The NYC_Personnel will not be materialized, but you get the same
effect.
Author
7 Sep 2006 6:45 PM
Kalen Delaney
No, there is no additional 'loop' involved. If the view was a simple SELECT,
selecting from the view or the table would be the exact same work. SQL
Server would translate the select from the view into a select from the
underlying table before execution. So selecting from a view should never be
any WORSE performance than doing the direct query against the base table(s).

The benefit comes in when the view is based on an aggregation of table data,
e.g. select the sum of sales for each of the 50 states in the US). Building
an index on the view actually permanently stores the aggregated data. So for
the example given, instead of millions of rows for your individual sales,
you'll only have 50 rows, one for each state. Without the index on the view,
every time you selected the aggregated data, SQL would have to RECOMPUTE the
sums for each state. With the indexed view, the sums are physically stored
in a mere 50 rows, and it's very fast to get the data you need.

--
HTH
Kalen Delaney, SQL Server MVP


Show quote
"SQL Ken" <SQL***@gmail.com> wrote in message
news:1157650871.227855.289080@m79g2000cwm.googlegroups.com...
> Tom,
> If a view and a table are indexed properly,
> shouldn't  querying  from a table be faster because it go directly
> against the table where the querying from a view require an additional
> loop?
> For example
> query from a table >>
>
> SELECT  *
> FROM    table1
>
> where as
> Query from a view would be equivalent to >>
>
>  SELECT *
>  FROM
>          (SELECT *
>           FROM table1
>          ) MyView
>
>
> Thanks
>
> Ken
>
>
> Tom Moreau wrote:
>> A view doesn't normally contain data.  It is just a query.  However, an
>> indexed view does contain data and *may* improve your query performance.
>>
>> --
>>     Tom
>>
>> ----------------------------------------------------
>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
>> SQL Server MVP
>> Toronto, ON   Canada
>>
>> "SQL Ken" <SQL***@gmail.com> wrote in message
>> news:1157592184.497564.25560@b28g2000cwb.googlegroups.com...
>> Can anyone explain to me how does a vew work internally?
>>
>> If a view is a replicate  of a table, will it be faster to query from a
>> table then a view?
>>
>> Thanks
>
Author
7 Sep 2006 9:11 PM
Alexander Kuznetsov
Kalen,

I would like to comment that in OLTP situation an indexed view like
this might introduce a lot of competition on locks and might cause
deadlocks. Even though it's only 50 rows, under read committed
isolation level your select query might spend quite a time in lock
waiting state...
Author
7 Sep 2006 11:43 PM
Kalen Delaney
Yes of course, there's a lot of things to consider before building indexed
views.
Anyone expecting to use them should do careful planning, understand the
prerequisites, and be aware of the costs as well as the benefits.

--
HTH
Kalen Delaney, SQL Server MVP


Show quote
"Alexander Kuznetsov" <AK_TIREDOFSPAM@hotmail.COM> wrote in message
news:1157663477.320776.20800@m79g2000cwm.googlegroups.com...
> Kalen,
>
> I would like to comment that in OLTP situation an indexed view like
> this might introduce a lot of competition on locks and might cause
> deadlocks. Even though it's only 50 rows, under read committed
> isolation level your select query might spend quite a time in lock
> waiting state...
>
Author
7 Sep 2006 6:54 PM
Tracy McKibben
SQL Ken wrote:
Show quote
> Tom,
> If a view and a table are indexed properly,
> shouldn't  querying  from a table be faster because it go directly
> against the table where the querying from a view require an additional
> loop?
> For example
> query from a table >>
>
>  SELECT  *
>  FROM    table1
>
> where as
> Query from a view would be equivalent to >>
>
>   SELECT *
>   FROM
>           (SELECT *
>            FROM table1
>           ) MyView
>
>
> Thanks
>
> Ken
>

Ken, compare the execution plan of these two queries, you'll see that
internally they do the exact same thing...


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com

AddThis Social Bookmark Button