With just about 7.000 users, the following query in og_user_roles_getgid() is causing huge delays for us (about 4-500 msec):
$result = db_query("SELECT na.gid from {node_access} na INNER JOIN {og_uid} ogu ON na.gid = ogu.nid WHERE na.realm = 'og_subscriber' AND ogu.uid = %d AND (na.nid = %d OR na.gid = %d)", $uid, $nid, $nid); // modified to check for either the node or group ID in node_access
EXPLAIN tells us why (note the size of the result set - approx. 75.000 rows):
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE na index PRIMARY PRIMARY 775 NULL 74475 Using where; Using index
1 SIMPLE ogu eq_ref PRIMARY PRIMARY 8 iofc_drupal_protobuild.na.gid,const 1 Using where; Using indexWe then added a new index to the {node_access} table in an attempt to speed up data access:
ALTER TABLE node_access ADD INDEX og_subscriber (realm(30), nid, gid)
However, since there are too much matching rows, there is almost no visible effect (still 30.184 rows to examine):
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE na ref PRIMARY,og_subscriber og_subscriber 92 const 30184 Using where; Using index
1 SIMPLE ogu eq_ref PRIMARY PRIMARY 8 iofc_drupal_protobuild.na.gid,const 1 Using where; Using indexI then tried to rewrite the query in order to split it into two simple queries vs. one complex:
$result = db_query("SELECT gid FROM {node_access} WHERE realm = 'og_subscriber' AND nid = %d AND (gid IN (SELECT nid FROM {og_uid} WHERE uid = %d) OR gid = %n)", $nid, $uid, $nid);
By avoiding a direct INNER JOIN, the resulting queries make proper use of indexes:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY node_access ref PRIMARY PRIMARY 4 const 1 Using where; Using index
2 DEPENDENT SUBQUERY og_uid unique_subquery PRIMARY PRIMARY 8 func,const 1 Using index; Using whereHowever, the question remains whether the new query is 100% the same as the old one.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | og_user_roles-getgid.patch | 1.86 KB | smk-ka |
Comments
Comment #1
somebodysysop commentedHow about two separate queries this way:
and
Comment #2
smk-ka commentedThat seems to work equally well. Turned #1 into a patch.
Comment #3
somebodysysop commentedOK. Code has been modified. Will be included in next release. Thanks for your assistance on this.
Comment #4
sunWhere? I do not see a commit on http://drupal.org/project/cvs/149373
Comment #5
somebodysysop commentedI still don't know how to update the dev release. I only know how to issue new ones.
Comment #6
sunComment #7
somebodysysop commentedHere's where I get tripped up by cvs voodoo. In my local directories, the 5.x code for ogur is in "contributions/modules/og_user_roles". The 6.x version is in "contributions/og_user_roles".
So, when I issue this command:
Should the directory reflect where my 6.x files are locally, or where they are under Drupal cvs?
I am so sorry, but this confuses the living daylights out of me.
Comment #8
suncontributions/modules/og_user_rolesdefines the "module folder" in CVS, not your local directory. Above command performs a checkout of contributions/modules/og_user_roles into the current directory.Comment #9
somebodysysop commentedOK, thanks. Followed the instructions:
These are the results:
Is there anything else I need to do to get it updated on the project page?
BTW, it's updated here: http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/og_user_rol...
Comment #10
sunYes, that's all you have to do to commit a changeset. (Isn't it simple? ;)
Your commit is now displayed on
http://drupal.org/project/cvs/149373
http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/og_user_rol...
...and will be in the development snapshot for 6.x once the packaging scripts ran (every 12 hours).
It is recommended to commit each bug or feature in a separate commit, also linking to the project issue and providing credit. Ideally, you'll also add a CHANGELOG.txt, so users of a development snapshot know what is contained in the snapshot (and what not).
I highly recommend you to read about this in detail here: http://drupal.org/node/52287
Comment #11
somebodysysop commented@sun:
That *was* pretty simple. I really, really appreciate you taking the time to help me with this. I've just had the worse time trying to figure out CVS with respect to creating and maintaining a 6.x branch. This has helped me tremendously. I will read the documentation.
Again, thanks.