Thursday, March 29, 2012

Can I index a table in View ?

I create a view based on table. The table (say tblA) has an index on column
A.
When I do a sql query like "select .. from vMyView as MyView,tblB
where tblB.colA = MyView.A", will it use the index on tblA column A , or do
I
need to (and can I) create an index on the view ?
CREATE view vMyView()
select a,b,c...from tblA --> tblA.a is indexed
Thank you very much.Look up "Indexed views" in Books online.
Note - This feature is only supported in Enterprise/Developer editions.
Immy
"Paul fpvt2" <Paulfpvt2@.discussions.microsoft.com> wrote in message
news:16F4C4B5-1006-4A73-9BDF-BF7C2CC36B80@.microsoft.com...
>I create a view based on table. The table (say tblA) has an index on column
>A.
> When I do a sql query like "select .. from vMyView as MyView,tblB
> where tblB.colA = MyView.A", will it use the index on tblA column A , or
> do I
> need to (and can I) create an index on the view ?
> CREATE view vMyView()
> select a,b,c...from tblA --> tblA.a is indexed
> Thank you very much.|||The base table index will be used, unless you create an indexed view. See
Books Online for details on how to create indexed views.
Indexed views basically allow you to index only a subset of data.
What exactly are you trying to achieve?
ML
http://milambda.blogspot.com/|||Not entirely true. From Books Online:
"
Indexed views can be created in any edition of SQL Server 2000. In SQL
Server 2000 Enterprise Edition, the query optimizer will automatically
consider the indexed view. To use an indexed view in all other editions, the
NOEXPAND hint must be used.
"
ML
http://milambda.blogspot.com/|||It will use the base table index if it is useful to do so (based on many
factors, like the selectivity of the index and your search argument)
You can index the view, provided it meets the strict requirements for doing
so. You can do it in any version, but as noted only in Enterprise Edition
will it use the index transparently, other versions require you to use the
NOEXPAND keyword so he text of the view isn't brought into the plan creation
(so you manually force it to treat the view as a table, more or less)
So the main reason I piped up and said basically the same things that were
said before is this. Do you know how to tell if an index is being used
using Management Studio / Query Analyzer? When it comes to predicting what
index may or may not be used, it generally behooves you to try it out.
There are menu items on both for displaying estimated plans, and then actual
plans that were used to execute the query. So execute the query and look at
the plan, and fiddle about with it for a while and you will get the hang of
when an index will be used.
----
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP
"Arguments are to be avoided: they are always vulgar and often convincing."
(Oscar Wilde)
"Paul fpvt2" <Paulfpvt2@.discussions.microsoft.com> wrote in message
news:16F4C4B5-1006-4A73-9BDF-BF7C2CC36B80@.microsoft.com...
>I create a view based on table. The table (say tblA) has an index on column
>A.
> When I do a sql query like "select .. from vMyView as MyView,tblB
> where tblB.colA = MyView.A", will it use the index on tblA column A , or
> do I
> need to (and can I) create an index on the view ?
> CREATE view vMyView()
> select a,b,c...from tblA --> tblA.a is indexed
> Thank you very much.|||Thanks.
I guess I am worry that in a sql query like the following
"select .. from vMyView as MyView,tblB where tblB.colA = MyView.A", the
query will be slow if it does not use the base table index.
CREATE view vMyView()
select a,b,c...from tblA --> tblA.a is indexed
"ML" wrote:

> The base table index will be used, unless you create an indexed view. See
> Books Online for details on how to create indexed views.
> Indexed views basically allow you to index only a subset of data.
> What exactly are you trying to achieve?
>
> ML
> --
> http://milambda.blogspot.com/|||If you post more DDL (see here for details:
http://www.aspfaq.com/etiquette.asp?id=5006) , we can provide better help.
select .. from vMyView as MyView,tblB where tblB.colA = MyView.A
This may not be optimal. For one the old (deprecated) join syntax is used.
Compare the execution plan of your current query to the execution plan of
something like this:
select <columns>
from tblA
inner join tblB
on tblB.colA = tblA.colA
ML
http://milambda.blogspot.com/sql

No comments:

Post a Comment