Note that there are some explanatory texts on larger screens.

plurals
  1. POAre these the correct Indexes for an Indexed View for Leaderboards Grouped By Hour/Day/Month?
    primarykey
    data
    text
    <p>Here's and example of a script which creates an Indexed View Grouped By the Hour. I also have 2 others for Day and Month. The table UserPoints stores a record whenever points are awarded to the User with an exact DateTime timestamp called CreationDate.</p> <pre> CREATE VIEW [dbo].[HourlyPoints] WITH SCHEMABINDING AS SELECT [Count] = COUNT_BIG(*) --Required by SQL Server ,[UserId] ,[PointTypeId] ,[Points] = SUM(Points) ,[Hour] = DATEADD(hour, DATEDIFF(HOUR, 0, [CreationDate]), 0) FROM [dbo].[UserPoints] GROUP BY ProfileId ,PointTypeId ,DATEADD(hour, DATEDIFF(HOUR, 0, [CreationDate]), 0) GO CREATE UNIQUE CLUSTERED INDEX [IX_HourlyPoints_UserId_PointTypeId_Hour] ON [HourlyPoints] ([UserId],[PointTypeId],[Hour]) GO CREATE NONCLUSTERED INDEX [IX_HourlyPoints_PointTypeId_Points_Hour] ON [HourlyProfilePoints] ([PointTypeId],[Points],[Hour]) INCLUDE ([UserId]) GO </pre> <p>I'm trying to make sure I have the right indexes to query this Indexed View for a leaderboard which will rank Users based on the PointType &amp; Sum of Points.</p> <p>For example, my leaderboard query looks like this if I want a leaderboard based on the number points Users have earned with the last 3 hours. (or any other time window).</p> <pre><code>SELECT * FROM [HourlyPoints] WHERE [PointTypeId] = 1 --Gold Points AND [Hour] &gt;= '2013-06-13 01:00:00.000' AND [Hour] &lt; '2013-06-13 04:00:00.000' ORDER BY Points </code></pre> <p>What I'm worried about is that when I run that query I don't see the Execution Plan window showing that it is using the <strong>IX_HourlyPoints_PointTypeId_Points_Hour</strong> index. Shouldn't it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload