Ir al contenido

Count Vs Exists (ARCH5063)

Esta página aún no está disponible en tu idioma.

Key go.orm.count-vs-exists · Severity info · Category orm

Using count > 0 to test for row presence makes the DB count every matching row. Use an exists query that can stop at the first row instead.

var count int64
db.Model(&User{}).Where("email = ?", email).Count(&count)
if count > 0 {
// present
}
var exists bool
db.Model(&User{}).
Select("1").
Where("email = ?", email).
Limit(1).
Find(&exists)
if exists {
// present
}

For presence checks use a Limit(1) short-circuit query instead of Count, which scans all matching rows.