Getting Performace issue on the query and its internally calling getUnrestrictedNids function.
SQL query:
"{{SELECT tid, rid FROM permissions_by_term_role
UNION
SELECT tid, uid FROM permissions_by_term_user;}}"
Drupal Function:
private function getUnrestrictedNids() {
$tidsRestrictedUserQuery = $this->database->select('permissions_by_term_user', 'u')
->fields('u', ['tid']);
$restrictedTids = $this->database->select('permissions_by_term_role', 'r')
->fields('r', ['tid'])
->union($tidsRestrictedUserQuery)
->execute()
->fetchCol();
if (empty($restrictedTids)) {
return $this->getAllNids();
}
$restrictedNids = $this->database->select('taxonomy_index', 't')
->fields('t', ['nid'])
->condition('t.tid', $restrictedTids, 'IN')
->distinct(TRUE)
->execute()
->fetchCol();
if (empty($restrictedNids)) {
return $this->getAllNids();
}
$unrestrictedNids = $this->database->select('taxonomy_index', 't')
->fields('t', ['nid'])
->condition('t.nid', $restrictedNids, 'NOT IN')
->distinct(TRUE)
->execute()
->fetchCol();
return $unrestrictedNids;
}
Comments
Comment #2
harshabanala91 commentedTo improve the performance of the slow query, storing the value in cache.
Comment #3
baskaran commented@harshabanala91 Patch is working fine, looks good.
Comment #4
damienmckennaIt might be worth turning it into a merge request so the automated tests could run.
Comment #5
damienmckennaThe \Drupal::cache() call should also be changed to use dependency injection.
Comment #6
damienmckennaComment #7
damienmckennaHonestly this should be redone to use Drupal's caching system and use cache tags to control it, then the cache would be invalidated automatically when that vocabulary or terms were changed, rather than locking it to a time based system.