Skip to content

Commit

Permalink
Optimise VersionForDocumentValidator query
Browse files Browse the repository at this point in the history
We found that in `StateForDocumentValidator`, `order(nil).first` was
causing a query to take a long time to complete (~15s). We only needed
the ID for the record and found that using `pick(:id)` brought the query
time down below a second, so we're doing the same in this validator,
which has an equivalent requirement
  • Loading branch information
yndajas authored and kevindew committed Mar 7, 2025
1 parent 7def2c5 commit 8582132
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions app/validators/version_for_document_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ def validate(record)
user_facing_version: record.user_facing_version,
}

conflict = Edition.where(criteria).where.not(id: record.id).order(nil).first
conflict_id = Edition.where(criteria).where.not(id: record.id).pick(:id)

if conflict
if conflict_id
error = "user_facing_version=#{record.user_facing_version} and "\
"document=#{record.document_id} conflicts with edition "\
"id=#{conflict[:id]}"
"id=#{conflict_id}"
record.errors.add(:base, error)
end
end
Expand Down

0 comments on commit 8582132

Please sign in to comment.