Add composite index to og_membership table for group membership lookups
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3593919. --> Reported by: [herved](https://www.drupal.org/user/2197136) Related to !73 >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>The <code>og_membership</code> table has no index for looking memberships up by group. The <code>state</code> index doesn't help either, since almost every membership is <em>active</em>.</p> <p>So methods like <code>getGroupMembershipCount()</code> and <code>getGroupMembershipIdsByRoleNames()</code>, which fetch a group's members, end up scanning the whole table.</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>Add a composite index on <code>og_membership</code> over (entity_id, entity_type, state), via a storage schema handler plus a hook_post_update for existing sites.</p> <p><code>entity_id</code> comes first because it's the selective column and is present in every group lookup (getGroupMembershipCount() doesn't filter on entity_type, so an entity_type-first index wouldn't work for it).</p> <h3 id="summary-remaining-tasks">Remaining tasks</h3> <p>?</p> <h3 id="summary-ui-changes">User interface changes</h3> <p>None.</p> <h3 id="summary-api-changes">API changes</h3> <p>None.</p> <h3 id="summary-data-model-changes">Data model changes</h3> <p>Adds one composite index on <code>og_membership</code> (entity_id, entity_type, state). No column changes; existing data untouched.</p> > Related issue: [Issue #3501637](https://www.drupal.org/node/3501637)
issue