I have a table
CREATE TABLE [dbo].[UserMoviesActions](
[Actionid] [int] IDENTITY(1,1) NOT NULL,
[MovieId] [int] NOT NULL,
[UserId] [int] NOT NULL,
[CreatedDateUtc] [datetime2](7) NOT NULL,
[MovieActionType] [int] NOT NULL
This table will have a lot of activities for movies, such as watched, liked, loved, ignored, added to watch list etc (MovieActionType field)
I would like to create a high performing query/index view that will get top 10 movies with the most actions for the last 30 days. Below is my query
SELECT TOP 10
m.Title,
m.posterpath,
m.movieid,
COUNT(*) AS ActionCount
FROM
[dbo].[UserMoviesActions] ma
INNER JOIN Movies m on m.MovieID = ma.MovieId
WHERE CreatedDateUtc > GETUTCDATE()-30
GROUP BY
m.MovieId, title, m.posterpath
ORDER BY ActionCount DESC
Would this be the best approach and what indexes should I have on the tables.
Would columnstore index apply here? if so how (not a dba, apologies for noob questions)
EDIT: Filled the table with 2M records
This is the plan without any suggested index (either from JD or SQL Server engine)
https://www.brentozar.com/pastetheplan/?id=HJDqGyf_a
Plan with the non-cluster index on UserMoviesAction suggested by JD
CREATE NONCLUSTERED INDEX IX_UserMovieActions_MovieId_CreatedDateUtc ON dbo.UserMoviesActions (MovieId, CreatedDateUtc);
https://www.brentozar.com/pastetheplan/?id=rkkUmyGu6
Plan with both indexes suggested by JD
--columnstore
CREATE NONCLUSTERED COLUMNSTORE INDEX IX_Movies_MovieId_Title_PosterPath ON dbo.Movies (MovieId, Title, PosterPath);
--non-cluster SO
CREATE NONCLUSTERED INDEX IX_UserMovieActions_MovieId_CreatedDateUtc ON dbo.UserMoviesActions (MovieId, CreatedDateUtc);
https://www.brentozar.com/pastetheplan/?id=BkT3Q1zuT
Plan with index suggested by sql server engine after running the raw query (same as JD but it has movieId in the INCLUDE column vs suggested by JD, not sure what the difference is between the two
--non-cluster sql server suggested
CREATE NONCLUSTERED INDEX [IX_UserMovieActions_CreatedDateUtc_Include_MovieId]
ON [dbo].[UserMoviesActions] ([CreatedDateUtc])
INCLUDE ([MovieId])