Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Indexed View Sql Server 2005 Expand / Collapse
Author
Message
Posted Sunday, April 06, 2008 10:53 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, March 07, 2011 3:15 AM
Points: 375, Visits: 1,255
Hi All,

I have a table and I created one view on it and I created Clustered Index on this view.

When i am making a Select Statement to view the data,
this view is doing table scan instead of index scan,

In this point I am really confused, then what is the used of a Indexed View??

Please Clear me on this concepts...

Code Present Below


IF OBJECT_ID('v') IS NOT NULL DROP VIEW v
IF OBJECT_ID('t') IS NOT NULL DROP TABLE t
go
CREATE TABLE t
(id int NOT NULL,
a int NOT NULL,
b float(53) NOT NULL)
GO
INSERT t VALUES(1, 1,1.0e1)
INSERT t VALUES(2, 1,1.0e2)
INSERT t VALUES(3, 2, 1.0e0)
INSERT t VALUES(4, 2, 5.0e-17)
INSERT t VALUES(5, 2, 5.0e-17)
INSERT t VALUES(6, 2, 5.0e-17)
GO

CREATE VIEW v WITH SCHEMABINDING AS
SELECT a, SUM(b) AS sum_b, COUNT_BIG(*) AS c
FROM dbo.t
GROUP BY a
GO
CREATE UNIQUE CLUSTERED INDEX idx ON v(a)
GO
Select * from v


Please help me ...

Cheers!

Sandy.


--
Post #480506
Posted Sunday, April 06, 2008 12:06 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, March 14, 2011 1:30 AM
Points: 83, Visits: 149
We cannot use aggregate functions when using schemabinding.

--------------------------------------------------------------------------------------
Save our world, its all we have! A must watch video Pale Blue Dot
Post #480507
Posted Sunday, April 06, 2008 3:13 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, March 14, 2011 1:14 PM
Points: 436, Visits: 874
Sandy: not sure why your view isn't using the clustered index but the group by is not the issue. using your sample data script, here's my sqlcmd results:

2> CREATE VIEW v WITH SCHEMABINDING AS
3> SELECT a, SUM(b) AS sum_b, COUNT_BIG(*) AS c
4> FROM dbo.t
5> GROUP BY a
6> GO
1> CREATE UNIQUE CLUSTERED INDEX idx ON v(a)
2> GO
1> set showplan_text on
2> go
1> Select * from v where a = 2
2> go
StmtText
------------------------------
select * from v where a = 2

(1 rows affected)
StmtText


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-----------
|--Stream Aggregate(DEFINE: ([Expr1004]=SUM([staging].[dbo].[v].[sum_b]), [Expr
1005]=SUM([staging].[dbo].[v].[c]), [staging].[dbo].[t].[a]=ANY([staging].[dbo].
[v].[a])))
|--Clustered Index Seek(OBJECT: ([staging].[dbo].[v].[idx]), SEEK: ([stagin
g].[dbo].[v].[a]=(2)) ORDERED FORWARD)


(2 rows affected)

Post #480517
Posted Sunday, April 06, 2008 10:48 PM


One Orange Chip

One Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange Chip

Group: General Forum Members
Last Login: Today @ 11:50 AM
Points: 28,379, Visits: 22,169
You will never get an INDEX SEEK using SELECT *... only index scans.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #480582
Posted Monday, April 07, 2008 2:58 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, March 07, 2011 3:15 AM
Points: 375, Visits: 1,255
hi antonio.collins,

I have used your query too, but still same result, I mean table scan here...


|--Stream Aggregate(DEFINE[master].[dbo].[t].[a]=ANY([master].[dbo].[t].[a])))
|--Table Scan(OBJECT[master].[dbo].[t]), WHERE[master].[dbo].[t].[a]=(2)))


Can u please help me on this?

hey Jeff Moden,

Can you please clear me, I am not clear about your statement and
can u give me small idea about Indexed view??


You will never get an INDEX SEEK using SELECT *... only index scans.


Can you clear me about Indexed view concepts??

Cheers!

Sandy.



--
Post #480645
Posted Monday, April 07, 2008 6:36 AM


One Orange Chip

One Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange ChipOne Orange Chip

Group: General Forum Members
Last Login: Today @ 11:50 AM
Points: 28,379, Visits: 22,169
You will never get an INDEX SEEK using SELECT *... only index scans.

My most sincere apologies... the comment above does NOT apply to INDEXED VIEWs.

What matters is what you have in the WHERE clause of whatever query is using the INDEXED VIEW. For example, the following WILL cause an INDEX SCAN because it needs to return ALL rows from the view and it's more efficient to do a scan...

 SELECT * 
FROM v

... but if you have something in the WHERE clause that is capable of using the clustered or other index, you'll get a nice fast INDEX SEEK. For example, the following will use an INDEX SEEK...

 SELECT * 
FROM v
WHERE A = 2

This is important when a join comes into play on the indexed column. But, depending on the query, you could still get an INDEX SCAN just like on any other derived table (which is sometimes called an "Inline View").

The real key to the speed of an INDEXED VIEW is that the aggregates are precalculated and materialized as if you had populated a temp table with the same query. That's why they don't recommend using INDEXED VIEWs on tables that are made to suffer a great many changes... everytime you change something, the INDEXED VIEW must reaggregate the answers for the affected rows.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #480731
Posted Monday, April 07, 2008 10:37 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, March 07, 2011 3:15 AM
Points: 375, Visits: 1,255
Thanks Jeff,


Cheers!

Sandy.


--
Post #481291
Posted Tuesday, December 15, 2009 1:47 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, January 18, 2012 4:50 AM
Points: 3, Visits: 90
It's a very late response, but I would put my 2 cents anyway.

I totally agree to what Jeff has explained, but I guess the problem what Sandy is referring to is a little different. If the WITH (NOEXPAND) hint is used in the query then an INDEX scan will happen instead of TABLE scan.

Select * from V with (NOEXPAND)

In Sandy's case, the query processor just takes the Indexed View as a regular view and expands the view when the query is run, instead of using the Clustered Index on the view.

Deepak
Post #834744
Posted Tuesday, December 15, 2009 2:22 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:40 PM
Points: 14,819, Visits: 19,094
Not sure if this is relavent, but an INDEX scan on a clustered index is a table scan.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #834756
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse