]> Untitled Git - lemmy.git/blob - migrations/2021-01-05-200932_add_hot_rank_indexes/up.sql
Fixing GetPosts active sort index. Fixes #2683 (#2684)
[lemmy.git] / migrations / 2021-01-05-200932_add_hot_rank_indexes / up.sql
1 -- Need to add immutable to the hot_rank function in order to index by it
2
3 -- Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
4 create or replace function hot_rank(
5   score numeric,
6   published timestamp without time zone)
7 returns integer as $$
8 begin
9   -- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
10   return floor(10000*log(greatest(1,score+3)) / power(((EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600) + 2), 1.8))::integer;
11 end; $$
12 LANGUAGE plpgsql
13 IMMUTABLE;
14
15 -- Post_aggregates
16 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);
18
19 create index idx_post_aggregates_stickied_active on post_aggregates (stickied desc, hot_rank(score, newest_comment_time) desc, newest_comment_time desc);
20 create index idx_post_aggregates_active on post_aggregates (hot_rank(score, newest_comment_time) desc, newest_comment_time desc);
21
22 create index idx_post_aggregates_stickied_score on post_aggregates (stickied desc, score desc);
23 create index idx_post_aggregates_score on post_aggregates (score desc);
24
25 create index idx_post_aggregates_stickied_published on post_aggregates (stickied desc, published desc);
26 create index idx_post_aggregates_published on post_aggregates (published desc);
27
28 -- Comment
29 create index idx_comment_published on comment (published desc);
30
31 -- Comment_aggregates
32 create index idx_comment_aggregates_hot on comment_aggregates (hot_rank(score, published) desc, published desc);
33 create index idx_comment_aggregates_score on comment_aggregates (score desc);
34
35 -- User
36 create index idx_user_published on user_ (published desc);
37
38 -- User_aggregates
39 create index idx_user_aggregates_comment_score on user_aggregates (comment_score desc);
40
41 -- Community
42 create index idx_community_published on community (published desc);
43
44 -- Community_aggregates
45 create index idx_community_aggregates_hot on community_aggregates (hot_rank(subscribers, published) desc, published desc);
46 create index idx_community_aggregates_subscribers on community_aggregates (subscribers desc);
47
48
49