Optimize gorm queries