Skip to content

Commit

Permalink
Merge pull request #61 from strideynet/strideynet/query-performance
Browse files Browse the repository at this point in the history
Optimize queries
  • Loading branch information
strideynet authored Jul 25, 2023
2 parents db7b97e + ee06958 commit 0724a54
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
30 changes: 17 additions & 13 deletions feed/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,25 @@ func PostsFromStorePosts(storePosts []gen.CandidatePost) []Post {
for _, p := range storePosts {
posts = append(posts, Post{
URI: p.URI,
Cursor: bluesky.FormatTime(p.CreatedAt.Time),
Cursor: bluesky.FormatTime(p.IndexedAt.Time),
})
}
return posts
}

func newGenerator() GenerateFunc {
return func(ctx context.Context, pgxStore *store.PGXStore, cursor string, limit int) ([]Post, error) {
params := store.ListPostsForNewFeedOpts{
Limit: limit,
}
cursorTime := time.Now().UTC()
if cursor != "" {
cursorTime, err := bluesky.ParseTime(cursor)
parsedTime, err := bluesky.ParseTime(cursor)
if err != nil {
return nil, fmt.Errorf("parsing cursor: %w", err)
}
params.CursorTime = &cursorTime
cursorTime = parsedTime
}
params := store.ListPostsForNewFeedOpts{
Limit: limit,
CursorTime: cursorTime,
}

posts, err := pgxStore.ListPostsForNewFeed(ctx, params)
Expand All @@ -117,16 +119,18 @@ func newGenerator() GenerateFunc {

func newWithTagGenerator(tag string) GenerateFunc {
return func(ctx context.Context, pgxStore *store.PGXStore, cursor string, limit int) ([]Post, error) {
params := store.ListPostsForNewFeedOpts{
Limit: limit,
FilterTag: tag,
}
cursorTime := time.Now().UTC()
if cursor != "" {
cursorTime, err := bluesky.ParseTime(cursor)
parsedTime, err := bluesky.ParseTime(cursor)
if err != nil {
return nil, fmt.Errorf("parsing cursor: %w", err)
}
params.CursorTime = &cursorTime
cursorTime = parsedTime
}
params := store.ListPostsForNewFeedOpts{
Limit: limit,
FilterTag: tag,
CursorTime: cursorTime,
}

posts, err := pgxStore.ListPostsForNewFeed(ctx, params)
Expand Down Expand Up @@ -154,7 +158,7 @@ func scoreBasedGenerator(gravity float64, postAgeOffset time.Duration) GenerateF

rows, err := pgxStore.ListPostsWithLikes(ctx, store.ListPostsWithLikesOpts{
Limit: 2000,
CursorTime: &cursorTime,
CursorTime: cursorTime,
})
if err != nil {
return nil, fmt.Errorf("executing sql: %w", err)
Expand Down
8 changes: 3 additions & 5 deletions store/gen/candidate_posts.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions store/migrations/000010_add_more_indexes.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX candidate_posts_indexed_at_idx;
1 change: 1 addition & 0 deletions store/migrations/000010_add_more_indexes.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX candidate_posts_indexed_at_idx ON public.candidate_posts (indexed_at,is_hidden);
23 changes: 11 additions & 12 deletions store/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func (s *PGXStore) DeleteFollow(ctx context.Context, opts DeleteFollowOpts) (err
}

type ListPostsForNewFeedOpts struct {
CursorTime *time.Time
CursorTime time.Time
FilterTag string
Limit int
}
Expand All @@ -428,12 +428,11 @@ func (s *PGXStore) ListPostsForNewFeed(ctx context.Context, opts ListPostsForNew
endSpan(span, err)
}()

queryParams := gen.GetFurryNewFeedParams{}
if opts.CursorTime != nil {
queryParams.CursorTimestamp = pgtype.Timestamptz{
queryParams := gen.GetFurryNewFeedParams{
CursorTimestamp: pgtype.Timestamptz{
Valid: true,
Time: *opts.CursorTime,
}
Time: opts.CursorTime,
},
}
if opts.FilterTag != "" {
queryParams.Tag = opts.FilterTag
Expand All @@ -451,7 +450,7 @@ func (s *PGXStore) ListPostsForNewFeed(ctx context.Context, opts ListPostsForNew
}

type ListPostsWithLikesOpts struct {
CursorTime *time.Time
CursorTime time.Time
Limit int
}

Expand All @@ -462,13 +461,13 @@ func (s *PGXStore) ListPostsWithLikes(ctx context.Context, opts ListPostsWithLik
endSpan(span, err)
}()

queryParams := gen.GetPostsWithLikesParams{}
if opts.CursorTime != nil {
queryParams.CursorTimestamp = pgtype.Timestamptz{
queryParams := gen.GetPostsWithLikesParams{
CursorTimestamp: pgtype.Timestamptz{
Valid: true,
Time: *opts.CursorTime,
}
Time: opts.CursorTime,
},
}

if opts.Limit != 0 {
queryParams.Limit = int32(opts.Limit)
}
Expand Down
8 changes: 3 additions & 5 deletions store/queries/candidate_posts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ WHERE
cp.is_hidden = false
AND ca.status = 'approved'
AND (@tag::TEXT = '' OR @tag::TEXT = ANY (cp.tags))
AND (@cursor_timestamp::TIMESTAMPTZ IS NULL OR
cp.created_at < @cursor_timestamp)
AND (cp.indexed_at < @cursor_timestamp)
AND cp.deleted_at IS NULL
ORDER BY
cp.created_at DESC
cp.indexed_at DESC
LIMIT @_limit;

-- name: GetPostsWithLikes :many
Expand All @@ -38,8 +37,7 @@ SELECT
candidate_likes cl
WHERE
cl.subject_uri = cp.uri
AND (@cursor_timestamp::TIMESTAMPTZ IS NULL OR
cl.indexed_at < @cursor_timestamp)
AND (cl.indexed_at < @cursor_timestamp)
AND cl.deleted_at IS NULL) AS likes
FROM
candidate_posts cp
Expand Down

1 comment on commit 0724a54

@vercel
Copy link

@vercel vercel bot commented on 0724a54 Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.