Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
lebedev
Posting Yak Master
126 Posts |
Posted - 2013-03-08 : 14:06:09
|
Hello,We have a table in SQL Server 2012 which contains information about files, such as CreatedDate and Size. We created a report which returns the number of files and their cumulative size by year. For example, it would have a row 2012, 1000000, 100GB, which means that in 2012 there were 1000000 files created with total size of 100GB.This reports takes many hours to run. Creating an index on CreatedDate does not help because all rows end up being aggregated anyway.What are our optimization options here besides creating a star schema? Create Indexed View? What else?Thanks,Alec |
|
UnemployedInOz
Yak Posting Veteran
54 Posts |
Posted - 2013-03-08 : 23:42:00
|
-- this may help if there is no time on the CreatDate and uses the index on the CreateDate -- with a possible include of sizeSelect Year(CreateDate),sum(CNT), sum(size2) as Sizefrom (select Createdate,count(*) as CNT, sum(size) as size2 From YourTable Group by Createdate) zGroup by Year(CreateDate) |
|
|
UnemployedInOz
Yak Posting Veteran
54 Posts |
Posted - 2013-03-09 : 00:47:27
|
Create view dbo.Testvw WITH SCHEMABINDINGASSelect size, year(CreateDate) as cd,[ID] from dbo.YourTable;go--Set the options to support indexed views.SET NUMERIC_ROUNDABORT OFF;SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON;GOCREATE UNIQUE CLUSTERED INDEX IDX_V1 ON Testvw(cd,size,ID);GOselect count(*),sum(size),cdfrom Testvwgroup by tdgo-- or (as they both use the index created)select count(*),sum(size),year(CreateDate) as tdfrom YourTablegroup by year(CreateDate) go |
|
|
|
|
|
|
|