1 -- Need to add immutable to the hot_rank function in order to index by it
2 -- Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
3 CREATE OR REPLACE FUNCTION hot_rank (score numeric, published timestamp without time zone)
7 -- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
8 RETURN floor(10000 * log(greatest (1, score + 3)) / power(((EXTRACT(EPOCH FROM (timezone('utc', now()) - published)) / 3600) + 2), 1.8))::integer;
15 CREATE INDEX idx_post_aggregates_stickied_hot ON post_aggregates (stickied DESC, hot_rank (score, published) DESC, published DESC);
17 CREATE INDEX idx_post_aggregates_hot ON post_aggregates (hot_rank (score, published) DESC, published DESC);
19 CREATE INDEX idx_post_aggregates_stickied_active ON post_aggregates (stickied DESC, hot_rank (score, newest_comment_time) DESC, newest_comment_time DESC);
21 CREATE INDEX idx_post_aggregates_active ON post_aggregates (hot_rank (score, newest_comment_time) DESC, newest_comment_time DESC);
23 CREATE INDEX idx_post_aggregates_stickied_score ON post_aggregates (stickied DESC, score DESC);
25 CREATE INDEX idx_post_aggregates_score ON post_aggregates (score DESC);
27 CREATE INDEX idx_post_aggregates_stickied_published ON post_aggregates (stickied DESC, published DESC);
29 CREATE INDEX idx_post_aggregates_published ON post_aggregates (published DESC);
32 CREATE INDEX idx_comment_published ON comment (published DESC);
35 CREATE INDEX idx_comment_aggregates_hot ON comment_aggregates (hot_rank (score, published) DESC, published DESC);
37 CREATE INDEX idx_comment_aggregates_score ON comment_aggregates (score DESC);
40 CREATE INDEX idx_user_published ON user_ (published DESC);
43 CREATE INDEX idx_user_aggregates_comment_score ON user_aggregates (comment_score DESC);
46 CREATE INDEX idx_community_published ON community (published DESC);
48 -- Community_aggregates
49 CREATE INDEX idx_community_aggregates_hot ON community_aggregates (hot_rank (subscribers, published) DESC, published DESC);
51 CREATE INDEX idx_community_aggregates_subscribers ON community_aggregates (subscribers DESC);