We’re using an expression in a workflow and use the result in a loop. The expression that gives the right results takes more than 10 min to execute. The expected amount of records is ca. 10K.
Required expression
This expression takes 6768ms in the expression debugger (with a TOP 100)
persons,
this,
boolean:and([
int:count({
compliancychecks,
1,
boolean:and([contact.equals(parent.inherits_from_relation()),date_executed.equals(date:now())])
}).equals(0),
boolean:or([
this.in({
systemversions,
persons:if(
this.owner.contact_type().equals(contacttype:load(0)),
this.owner_contact_person.connection_to.person(),
this.owner.person()
),
boolean:and([
this.is_current_version.equals(yesno:load(1)),
this.product.is_policy().equals(yesno:load(1)),
boolean:not(this.is_ended_or_suspended())
])
}),
this.in({
connectioncontacts,
this.connection_to.person(),
boolean:and([
this.connection_from.has_active_insurances().equals(yesno:load(1)),
this.connection_to.contact_type().equals(contacttype:load(1))
])
})
])
])
}
The bottleneck
I’ve found the bottleneck in the expression above. When I remove the int:count() in the first boolean:and(), the expression takes 36ms in the expression debugger (also with a TOP 100). That is almost 200x faster!
This is the expression:
{
persons,
this,
boolean:and([
boolean:or([
this.in({
systemversions,
persons:if(
this.owner.contact_type().equals(contacttype:load(0)),
this.owner_contact_person.connection_to.person(),
this.owner.person()
),
boolean:and([
this.is_current_version.equals(yesno:load(1)),
this.product.is_policy().equals(yesno:load(1)),
boolean:not(this.is_ended_or_suspended())
])
}),
this.in({
connectioncontacts,
this.connection_to.person(),
boolean:and([
this.connection_from.has_active_insurances().equals(yesno:load(1)),
this.connection_to.contact_type().equals(contacttype:load(1))
])
})
])
])
}
But, the int:count() in the first expression is necessary.
We’ve validated correct indices on the compliancychecks table, eg. contact and date_executed as included columns. I assume it has something to do with the date check.
Who is the genius that can help us in the right direction?