Skip to content

Commit

Permalink
pef: fillLivestreamResponseのN+1
Browse files Browse the repository at this point in the history
  • Loading branch information
sor4chi committed Nov 25, 2023
1 parent a1785ca commit fb40485
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions go/livestream_handler.go
Expand Up @@ -499,16 +499,20 @@ func fillLivestreamResponse(ctx context.Context, tx *sqlx.Tx, livestreamModel Li
return Livestream{}, err
}

tags := make([]Tag, len(livestreamTagModels))
tagIDs := make([]int64, len(livestreamTagModels))
for i := range livestreamTagModels {
tagModel := TagModel{}
if err := tx.GetContext(ctx, &tagModel, "SELECT * FROM tags WHERE id = ?", livestreamTagModels[i].TagID); err != nil {
tagIDs[i] = livestreamTagModels[i].TagID
}

tags := make([]Tag, 0, len(tagIDs))

if len(tagIDs) > 0 {
query, params, err := sqlx.In("SELECT * FROM tags WHERE id IN (?)", tagIDs)
if err != nil {
return Livestream{}, err
}

tags[i] = Tag{
ID: tagModel.ID,
Name: tagModel.Name,
if err := tx.SelectContext(ctx, &tags, query, params...); err != nil {
return Livestream{}, err
}
}

Expand Down

0 comments on commit fb40485

Please sign in to comment.