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;
  }
CommentFileSizeAuthor
#2 performance-slow-query-fix.patch1.23 KBharshabanala91

Comments

harshabanala91 created an issue. See original summary.

harshabanala91’s picture

StatusFileSize
new1.23 KB

To improve the performance of the slow query, storing the value in cache.

baskaran’s picture

@harshabanala91 Patch is working fine, looks good.

damienmckenna’s picture

Assigned: harshabanala91 » Unassigned
Status: Active » Needs work

It might be worth turning it into a merge request so the automated tests could run.

damienmckenna’s picture

The \Drupal::cache() call should also be changed to use dependency injection.

damienmckenna’s picture

Issue tags: +Performance
damienmckenna’s picture

Honestly 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.