Help solving ObjectCustomFieldValue performance problem (maybe impact RT 4.4 upgrade)

Hi,

we have a external CF (type Select) where we don’t want that users can
add the same value to different tickets.

I implemented this by building a list of the used values:

my %used_values;
my $ocfvs = RT::ObjectCustomFieldValues->new(RT->SystemUser);
$ocfvs->LimitToCustomField(1);
while (my $ocfv = $ocfvs->Next) {
$used_values{ $ocfv->Content } = 1;
}

After this I noticed much slower page loadtimes for the ticket create
and modify pages.
Investigating this with the, by the way wonderful and very helpful, “SQL
Queries” tool, it shows that for every create/modify page load RT runs
900 queries against the Tickets table.
The 900 comes from 900 tickets which have a CF.1 value (select
count(distinct ObjectId) from ObjectCustomFieldValues where CustomField
= 1 and Disabled = 0;)
Investigating further shows that Content [1] calls CustomFieldObj [2]
which SetContextObject based on Object [3] which loads, in my case, the
ticket.

I was really surprised to see queries against the Tickets table because
I used the RT::ObjectCustomFieldValues to fetch the values directly,
without going the other way around starting from tickets.

Actually removing SetContextObject from the CustomFieldObj method avoids
the queries against the Tickets table. This was introduced by [4], but
the commit message says nothing about the reason for this. I assume this
was necessary for the right check in Content [5] to also account for
ticket or queue role level rights.

If you look at the Content method [1], there are also multiple calls for
“$self->CustomFieldObj->Type” to fetch the CF type. This also involves
loading the object, which isn’t necessary, as CF can’t have a different
type depending on the object they are applied to.

I hope to get some feedback from the RT developers regarding this.

I think this could also have an impact on the RT 4.4 upgrade, because
the 4.3.6 upgrade step [6] calls RT::ObjectCustomFieldValues->Content
which maybe slow down the upgrade if you have many CF values with Category.

Chris

[1]

[2]

[3]

[4]

[5]

[6]