Use a materialized view, also called (indexed view with schema binding and creating an index on it). When you do this, the SQL server will essentially create and use this extra table suggested by your colleague (the data will be stored in the index) and will preserve the integrity of the data for you. Here's how to do it:
Create a view that returns the individual [month] [year] values, and then the index [year] [month] in the view. SQL Server will use a tiny index in the view instead of scanning a large table. Since the SQL server will not allow you to index the view with the DISTINCT keyword, instead of GROUP BY [year], [month] and use BIG_COUNT (*) in SELECT. It will look something like this:
CREATE VIEW dbo.vwMonthYear WITH SCHEMABINDING AS SELECT [year], [month], COUNT_BIG(*) [MonthCount] FROM [dbo].[YourBigTable] GROUP BY [year],[month] GO CREATE UNIQUE CLUSTERED INDEX ICU_vwMonthYear_Year_Month ON [dbo].[vwMonthYear](Year,Month)
Now when you select DISTINCT [Year], [Month] on a large table, the query optimizer scans a tiny index in the view instead of scanning millions of records in a large table.
SELECT DISTINCT [year], [month] FROM YourBigTable
This method took me from 5 million reads with an I / O rating of 10.9 to 36 reads with an I / O rating of 0.003. The overhead for this will be associated with maintaining an additional index, so every time a large table is updated, the update index will also be updated.
If you find that this indicator significantly slows down loading time. Drop the index, load the data, and then recreate it.
Full working example:
CREATE TABLE YourBigTable( YourBigTableID INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_YourBigTable_YourBigTableID PRIMARY KEY, [Year] INT, [Month] INT) GO CREATE VIEW dbo.vwMonthYear WITH SCHEMABINDING AS SELECT [year], [month], COUNT_BIG(*) [MonthCount] FROM [dbo].[YourBigTable] GROUP BY [year],[month] GO CREATE UNIQUE CLUSTERED INDEX ICU_vwMonthYear_Year_Month ON [dbo].[vwMonthYear](Year,Month) SELECT DISTINCT [year], [month] FROM YourBigTable -- Actual execution plan shows SQL server scaning ICU_vwMonthYear_Year_Month