|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: 2 days ago @ 9:27 AM
Points: 5,618,
Visits: 10,990
|
|
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
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 1:53 AM
Points: 1,474,
Visits: 2,342
|
|
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?
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 3:41 AM
Points: 13,383,
Visits: 25,189
|
|
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 2012 Query Performance Tuning SQL Server 2008 Query Performance Tuning Distilled and SQL Server Execution Plans
Product Evangelist for Red Gate Software
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Yesterday @ 9:03 AM
Points: 619,
Visits: 1,882
|
|
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
MCSA: SQL Server 2012 Follow me on Twitter: @WazzTheBadger LinkedIn Profile: Simon Osborne
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 3:56 PM
Points: 1,324,
Visits: 1,778
|
|
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) One man with courage makes a majority. Andrew Jackson
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Yesterday @ 6:59 PM
Points: 4,557,
Visits: 8,215
|
|
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
|
|
|
|