Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Indexed Views - why don't they improve performance?!!!!


Indexed Views - why don't they improve performance?!!!!

Author
Message
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8949 Visits: 19009
Have you tried figuring out the filters and applying to the inner query? Something like this:

DECLARE @YEAR INT, @MONTH INT
SELECT @YEAR = 2005, @MONTH = 6

DECLARE @StartDate DATETIME, @EndDate DATETIME
SET @StartDate = CONVERT(DATETIME,CAST(@YEAR AS CHAR(4))+RIGHT('0'+CAST(@MONTH AS VARCHAR(2)),2)+'01',112)
SET @EndDate = DATEADD(mm,13,@StartDate)
SELECT @StartDate, @EndDate -- eyeball

SELECT
   c.Reference,
   mc.MPAN--,
   --ElementID,
   --mm = MONTH(hhdatetime),
   --yy = YEAR(hhdatetime)
FROM CONTRACTS.MPAN_Contract mc
join CONTRACTS.ContractElementUnitInstance ui
   on mc.ContractID = ui.ContractID
join CONTRACTS.Contract c
   on ui.ContractID = c.id
   and ui.ElementID in (2,3,4)
join CONTRACTS.CalendarAllHH cal
   on cal.HHDatetime between ui.Startdate and ui.Enddate
   and cal.HHDatetime between mc.startdate and mc.enddate
   
WHERE cal.HHDatetime >= @StartDate AND cal.HHDatetime < @EndDate

GROUP BY c.Reference, mc.MPAN, ElementID, MONTH(hhdatetime), YEAR(hhdatetime)



“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
Gazareth
Gazareth
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2855 Visits: 5304
ChrisM@Work (1/23/2013)
Have you tried figuring out the filters and applying to the inner query? Something like this:

DECLARE @YEAR INT, @MONTH INT
SELECT @YEAR = 2005, @MONTH = 6

DECLARE @StartDate DATETIME, @EndDate DATETIME
SET @StartDate = CONVERT(DATETIME,CAST(@YEAR AS CHAR(4))+RIGHT('0'+CAST(@MONTH AS VARCHAR(2)),2)+'01',112)
SET @EndDate = DATEADD(mm,13,@StartDate)
SELECT @StartDate, @EndDate -- eyeball

SELECT
   c.Reference,
   mc.MPAN--,
   --ElementID,
   --mm = MONTH(hhdatetime),
   --yy = YEAR(hhdatetime)
FROM CONTRACTS.MPAN_Contract mc
join CONTRACTS.ContractElementUnitInstance ui
   on mc.ContractID = ui.ContractID
join CONTRACTS.Contract c
   on ui.ContractID = c.id
   and ui.ElementID in (2,3,4)
join CONTRACTS.CalendarAllHH cal
   on cal.HHDatetime between ui.Startdate and ui.Enddate
   and cal.HHDatetime between mc.startdate and mc.enddate
   
WHERE cal.HHDatetime >= @StartDate AND cal.HHDatetime < @EndDate

GROUP BY c.Reference, mc.MPAN, ElementID, MONTH(hhdatetime), YEAR(hhdatetime)



I did wonder if the view was created to allow direct use of the @Month & @Year parameters.
I also assume the group by clause is there to remove duplicates - any ideas on if that or distinct performs better?
Grant Fritchey
Grant Fritchey
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17529 Visits: 32252
Can't see the plan. I'm getting encoding errors. If you save the plan as a .sqlplan file, you can attach it. Then it can be downloaded.

Just on what I can see though, it looks like it's expanding the view. For giggles, try the noexpand hint when you create the indexed view. Since the query is clearly going to lead to scans (functions on the GROUP BY are a problem), you want to avoid that. This is a place where a hint might work well.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
s_osborne2
s_osborne2
Right there with Babe
Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)

Group: General Forum Members
Points: 745 Visits: 2292
I was going to suggest the NOEXPAND hint! I couldn't see anywhere in the thread to see what edition of SQL Server the OP is using. Unless it's enterprise edition the optimiser won't consider the index on the view when it's costing the query. Unless i've misunderstood the way the optimiser works with indexed views.

Thanks,


Simon



MCSE: Data Platform
MCSE: Business Intelligence
Follow me on Twitter: @WazzTheBadger
LinkedIn Profile: Simon Osborne
ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3937 Visits: 6663
First, I assume you've all met all the oodles of restrictions for an indexed view; you might want to verify that using Books Online "CREATE INDEX", "Remarks", "Indexed Views" as a guied.

Also, GROUP BY in the base query in exactly the same order as the clus index will be:


create view dbo.withClusteredIdx
with schemabinding
as
select c.Reference, mc.MPAN,ElementID,MONTH(hhdatetime) mm,YEAR(hhdatetime) yy
...
group by YEAR(hhdatetime),MONTH(hhdatetime),c.Reference, mc.MPAN,ElementID


CREATE UNIQUE CLUSTERED INDEX [idxTest] ON [dbo].[withClusteredIdx]
(
[yy] ASC,
[mm] ASC,
[Reference] ASC,
[MPAN] ASC,
[ElementID] ASC
)
go


SELECT ...
FROM dbo.withClusteredIdx
WHERE
yy >= @YEAR ... AND
mm ...



SQL DBA,SQL Server MVP('07, '08, '09)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
Sergiy
Sergiy
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5816 Visits: 11393
Gazareth (1/23/2013)

If you change your indexed view definition to:

CREATE UNIQUE CLUSTERED INDEX [idxTest] ON [dbo].[withClusteredIdx]
(
[yy] ASC,
[mm] ASC,
[Reference] ASC,
[MPAN] ASC,
[ElementID] ASC
   
) go

I think you'll see this view being used in place of the table.

Cheers


You should take this advice.

Or, better, merge YEAR and MONTH columns together:

DATEADD(mm, DATEDIFF(mm, 0, hhdatetime), 0) MonthOfYear



And make this column the first one in the clustered index.
Then you may just select
WHERE MonthOfYear between @StartOfThePeriod AND @EndOfThePeriod

Than you may just use
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