views:

286

answers:

1

Ok, I'm trying to make an indexed view that is against a simple table that stores the results of what people think is good/bad for a post. This is the results of a thumbs up / thumbs down, voting on posts.

So here's my pseduo fake table :-

HelpfulPostId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
PostId INT NOT NULL,
IsHelpful BIT NOT NULL,
UserId INT NOT NULL

So a user can only have one vote per post. It's either a 1 (helpful) or 0 (unhelpful) <-- not sure of a better way to handle that, if there is a better way.

Ok. What i'm trying to do is get a view that looks like the following.

HelpfulPostId INT IDENTITY(1,1) NOT NULL PRIMARY KEY,

PostId INT NOT NULL,
IsHelpfulCount COUNT_BIG (WHERE IsHelpful = 1)
IsNotHelpfulCount COUNT_BIG (WHERE IsHelpful = 0)

And finally, i'll need to make it schemabindable so i can add an index on the PK and then an index on the PostId.

I have no idea about the sql to make the view. Any suggestions?

Cheers :)

+1  A: 

Thoughts:

  • You can't use COUNT(*) in an indexed view
  • You can't aggregate bit fields

There are other limitations of indexed views

CREATE VIEW dbo.Example
WITH SCHEMABINDING
AS
SELECT
    PostId,
    SUM(CAST(IsHelpful AS bigint)) AS IsHelpfulCount,
    SUM(CAST(1-IsHelpful AS bigint)) AS IsNotHelpfulCount,
    COUNT_BIG(*) AS Dummy   --Used to satisfy requirement
FROM
    dbo.bob
GROUP BY
    PostId
GO
CREATE UNIQUE CLUSTERED INDEX IXC_Test ON dbo.Example (PostId)
GO

Edit: Removed the Identity field, which was accidently added to the original question/post.

Edit 2 (gbn): I forgot that any aggregate in an indexed view also needs a COUNT_BIG(*). So, simply add one as a dummy column. I've tested this.

If the view definition uses an aggregate function, the SELECT list must also include COUNT_BIG (*).

gbn
Um .. that's not going to work :( How does it know what the count of IsHelpfulCount is?? that's what i need to define, in this view :( Can that be a subquery?
Pure.Krome
IsHelpfulCount implies aggregate, no? And have you tried it
gbn
Pure.Krome
:( when I try to index this view, it doesn't work. Saying i need a COUNT_BIG(*) ???
Pure.Krome
.. but it now works (I can add a unique clustered index) when i add the following, to the select statement.. => COUNT_BIG(*) AS TotalCount
Pure.Krome
@Pure.Krome: Ok, I edited as you added comment.
gbn
cheers. Confirmed also. Works great :) cheers mate for following through with this.
Pure.Krome