Skip to content

Commit

Permalink
refactor: improve efficiency of commitlog lookup query on larger offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
Flexicon committed Jul 14, 2024
1 parent 8c0b01f commit 7a6a96e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ func commitLogHandler(db *gorm.DB) echo.HandlerFunc {

var commits []*Commit
offset := (page - 1) * limit
db.Order("created_at desc").Limit(limit).Offset(offset).Find(&commits)
dbSession := db.Session(&gorm.Session{PrepareStmt: true})

if err := dbSession.Raw(`
SELECT c.*
FROM commits c
INNER JOIN (
SELECT id FROM commits ORDER BY created_at DESC LIMIT ? OFFSET ?
) cid ON c.id = cid.id
ORDER BY created_at DESC
`, limit, offset).Scan(&commits).Error; err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "failed to retrieve commits")
}

return c.JSON(http.StatusOK, CommitLogResponse{
Log: commits,
Expand Down

0 comments on commit 7a6a96e

Please sign in to comment.