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 ««12

Indexed Views - why don't they improve performance?!!!! Expand / Collapse
Author
Message
Posted Wednesday, January 23, 2013 6:38 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:43 AM
Points: 6,890, Visits: 14,254
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
Post #1410534
Posted Wednesday, January 23, 2013 6:51 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:57 AM
Points: 2,112, Visits: 3,712
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?
Post #1410541
Posted Wednesday, January 23, 2013 7:03 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:17 AM
Points: 14,202, Visits: 28,531
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
Post #1410551
Posted Wednesday, January 23, 2013 8:43 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Wednesday, December 3, 2014 8:01 AM
Points: 711, Visits: 2,210
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
Post #1410635
Posted Thursday, January 24, 2013 9:09 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 4:19 PM
Points: 2,328, Visits: 3,505

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)

Carl Sagan said: "There is no such thing as a dumb question." Sagan obviously never watched a congressional hearing!
Post #1411198
Posted Thursday, January 24, 2013 9:38 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, November 3, 2014 4:30 PM
Points: 4,574, Visits: 8,366
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

Post #1411457
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse