Problem/Motivation

We manage a site for a client that uses an external identity store for user authentication. At any given time, we have approximately 1 million users, and a corresponding 1 million rows in our authmap table. We also maintain rolling deletes of stale users.

Review with New Relic showed that we were getting slower than expected performance with some selects and delete queries on the authmap table. It traced back to the following queries:

From user_delete_multiple():

db_delete('authmap')
        ->condition('uid', $uids, 'IN')
        ->execute();

From user_set_authmaps:

if ($value) {
  db_merge('authmap')
    ->key(array(
      'uid' => $account->uid,
      'module' => $module[1],
    ))
    ->fields(array('authname' => $value))
    ->execute();
}
else {
  db_delete('authmap')
    ->condition('uid', $account->uid)
    ->condition('module', $module[1])
    ->execute();
}

There is no index on either the uid or module columns, yielding to slower performance for these queries.

Proposed resolution

Add an index to authmap to handle these queries:

CREATE INDEX uid_module ON authmap (uid, module)

Because uid is the first column in the index, the delete query will use the multi-column index as well.

Remaining tasks

None

User interface changes

None

API changes

None

Data model changes

As above, a new index on authmap (uid, module).

CommentFileSizeAuthor
#2 drupal-authmap_indexes-2634840-2.patch808 bytesbrianV

Comments

brianV created an issue. See original summary.

brianV’s picture

StatusFileSize
new808 bytes

Patch attached.

brianV’s picture

Status: Active » Needs review
angrytoast’s picture

Status: Needs review » Reviewed & tested by the community

Confirming that the added index has a significant effect on authmap related database queries. Implemented the patch and seeing an UPDATE query drop from an average of ~280ms to ~1.5ms over ~300K rows.

fabianx’s picture

We need to check if this is still present in D8 first.

stefan.r’s picture

Status: Reviewed & tested by the community » Needs review
fabianx’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Pending Drupal 7 commit, +Avoid commit conflicts

Checked and it is not present anymore, but replaced by https://www.drupal.org/project/externalauth.

Added commit conflicts tag as update functions are generally prone to commit conflicts.

Marked for commit.

stefan.r’s picture

Assigned: brianV » Unassigned
stefan.r’s picture

Issue tags: +Performance

  • stefan.r committed a8c1ebd on 7.x
    Issue #2634840 by brianV: Add index on uid and module columns to authmap
    
stefan.r’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -Pending Drupal 7 commit, -Avoid commit conflicts

Committed and pushed to 7.x, thanks!

David_Rothstein’s picture

Issue tags: +7.50 release notes

Tagging since this is in CHANGELOG.txt.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.