SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


View index not being used


View index not being used

Author
Message
andybellenie
andybellenie
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 46
Hi there,

I have the following indexed view:

CREATE VIEW [dbo].[campaignContacts] WITH SCHEMABINDING
AS

SELECT cs.campaignId, sc.contactId, COUNT_BIG(*) AS total
FROM dbo.campaignSources cs
INNER JOIN dbo.sources s ON s.id = cs.sourceId
INNER JOIN dbo.sourceContacts sc ON sc.sourceId = s.id
GROUP BY cs.campaignId, sc.contactId

GO

CREATE UNIQUE CLUSTERED INDEX [IX_campaignContacts] ON [dbo].[campaignContacts]
(
campaignId ASC,
contactId ASC
)
GO

Now, when I do a simple SELECT contactId FROM campaignContacts it still takes a few seconds to compute the query before delivering results (these are big tables), and in the execution plan the index is not used.

I think I'm probably misunderstanding something critical about indexed views here, but surely if there is an index that already has the data I'm looking for, it should be used?
Lowell
Lowell
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28103 Visits: 39938
andybellenie (8/1/2013)

CREATE UNIQUE CLUSTERED INDEX [IX_campaignContacts] ON [dbo].[campaignContacts]
(
campaignId ASC,
contactId ASC
)


The index you created above would be used if you had a statement like this:

SELECT contactId
FROM campaignContacts
WHERE campaignId = @p1
AND contactId = @p2




so the index specifically supports WHERE statements..
if you don't have a WHERE statement, the optimizer would ignore that index, and use something different.

does that help?

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

happycat59
happycat59
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4735 Visits: 3215
What edition of SQL 2005 are you using ? From memory, Standard Edition will not use an index on a view unless you specify the NOEXPAND hint



andybellenie
andybellenie
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 46
Thanks for the replies, very helpful.
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16174 Visits: 19543
-- force usage of the view
SELECT Column1, Column2, ... FROM Table1, View1
WITH (NOEXPAND) WHERE ...

-- force usage of the base tables
SELECT Column1, Column2, ... FROM Table1, View1
WHERE ...
OPTION (EXPAND VIEWS)



Source: http://technet.microsoft.com/en-us/library/cc917715.aspx

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search