A bit of query optimization

main
Max Ignatenko 2024-02-15 18:39:29 +00:00
parent 63a767d890
commit 8561d90caf
2 changed files with 7 additions and 8 deletions

View File

@ -106,12 +106,11 @@ func (s *Scheduler) fillQueue(ctx context.Context) error {
for _, remote := range remotes {
repos := []repo.Repo{}
err := s.db.Model(&repos).
Where(`pds = ? AND (
last_indexed_rev is null OR last_indexed_rev = ''
OR (first_rev_since_reset is not null AND first_rev_since_reset <> '' AND last_indexed_rev < first_rev_since_reset))`,
remote.ID).
Limit(perPDSLimit).Find(&repos).Error
err := s.db.Raw(`SELECT * FROM "repos" WHERE pds = ? AND (last_indexed_rev is null OR last_indexed_rev = '')
UNION
SELECT * FROM "repos" WHERE pds = ? AND (first_rev_since_reset is not null AND first_rev_since_reset <> '' AND last_indexed_rev < first_rev_since_reset) LIMIT ?`,
remote.ID, remote.ID, perPDSLimit).
Scan(&repos).Error
if err != nil {
return fmt.Errorf("querying DB: %w", err)

View File

@ -17,9 +17,9 @@ import (
type Repo struct {
gorm.Model
PDS models.ID `gorm:"index:rev_state_index,priority:2"`
PDS models.ID `gorm:"index:rev_state_index,priority:2;index:was_indexed,priority:2"`
DID string `gorm:"uniqueIndex;column:did"`
LastIndexedRev string `gorm:"index:rev_state_index,expression:(last_indexed_rev < first_rev_since_reset),priority:1"`
LastIndexedRev string `gorm:"index:rev_state_index,expression:(last_indexed_rev < first_rev_since_reset),priority:1;index:was_indexed,expression:(last_indexed_rev is null OR last_indexed_rev = ''),priority:1"`
FirstRevSinceReset string
FirstCursorSinceReset int64
TombstonedAt time.Time