Performance gain by not executing ActiveQuery during $this->hasOne(), if key value is NULL


(sanka) #1

Has anyone thought about not making unnecessary DB calls if by default there is no possibilities to get any DB row?

In case of one2many or one2one relation, it is almost sure, that calling $this->hasOne() will result in null, if $this->key_to_one is null.

During debugging I discovered, that there is empty calls to DB. By digging into code I made modifications, that in case of making $this->hasOne() and $this->key_to_one is null, the result of $this->hasOne() is DummyQuery (allowing to execute $this->hasOne() ->with() etc).

Also had to make some minor modifications to ActiveQuery and Query (only checkings like: $table===null or similar), but result was impressive: I cut about 5-10% calls to DB, what would be anyway without result.

public function hasOne($class, $link)
{
return ($this->hasProperty(current($link)) && $this->{current($link)} === null) ? new ActiveQueryDummy(null) : $this->createRelationQuery($class, $link, false);
}

Is it a good way, or this has been already discussed and turned out to be dead-end?