PDA

View Full Version : Unnecessary query?


Andreas
01-30-2004, 03:24 PM
In global.php
$show['modcplink'] is being set (for the ModCP-Link in template footer).

This seems to trigger a query in can_moderate() if the user is not an admin or supermod - for every page being generated.

I wonder if it wouldn't be possible to use some cache (datastore) instead to avoid this query and if this could improve performance?

Xenon
01-31-2004, 12:06 AM
the first if does just use the permission cache, so no query is done until:
if ($forumid == 0)

then as forumid is 0 on that position, it's going through the modcache itself and looking if the user is in so, no query is done there either, just if the cache has not been created, it will do a query.

Andreas
01-31-2004, 12:49 AM
> just if the cache has not been created, it will do a query.
That's the point.

$imodcache is being build in cache_moderators() which itself gets called from forumdisplay.php, index.php, search.php & usercp.php - after processing global.php.

And what about the others like showthread.php, newreply.php and so on?

NTLDR
01-31-2004, 07:00 PM
Seeing as cache_moderators() requires a query and so does can_moderate() using can_moderate() would be prefered as its doing less work. It does seem that in my RC3 copy that they have 'forgotten/not included yet' the caching in the can_moderate() function, the $modcache variable is there, yet its never set, which causes extra queries every time can_moderate() is called where it could quite possibly return a cached value instead.

Andreas
02-10-2004, 07:46 PM
*bump*
Now that RC4 is available it seems like this doesn't have changed :(

Example
-----------
If a thread is being displayed (by a normal user, 40 posts/page) and can_moderate() is being called from the postbit I get 42 (!) times the same query:


SELECT moderatorid, permissions FROM moderator WHERE userid = 3


So once again, is that necessary? ;)

Zachery
02-10-2004, 07:50 PM
ugh you querry freaks :P

NTLDR
02-10-2004, 07:53 PM
So you have 42+ queries on that page? Thats not good. I'm sure some caching is ment to be used in the function, create a bug report at vB.com.

Zachery
02-10-2004, 08:00 PM
So you have 42+ queries on that page? Thats not good. I'm sure some caching is ment to be used in the function, create a bug report at vB.com.
Whoa... ive never seen more than 12 querrys on any page in vB3 >.< (cept admincp)

Andreas
02-10-2004, 08:07 PM
@Faranth
It's very easy to reproduce:

1) Take a vanilla vB3 RCwhatever
2) Enable debug mode (to see # of queries)
3) Put an <if condition="can_moderate()"></if> at the start of template postbit
4) Display a thread with > 40 posts as a normal user (setting display-mode to 40 posts/page)

Xenon
02-10-2004, 09:52 PM
you should definitely report it.

vb.com will surely look into it :)

Andreas
02-11-2004, 01:31 PM
http://www.vbulletin.com/forum/bugs.php?do=view&bugid=2224

Status: Closed (Fixed)

But no reply :( So I guess i'll have to wait for the next RC ...

Xenon
02-11-2004, 01:43 PM
yes you have...

btw, you're showing up unlicensed now...

Andreas
02-11-2004, 02:15 PM
I do? I don't get that "You're unlicensed remider"
Anyway, that's bollox :)

I've had problems to get rid of that "you're unlicensed" at vB-germany.com and they suggested to remove my email from the member area and afterwards add it again.
I did so ... that could be the reason.

NTLDR
02-11-2004, 02:23 PM
Either way its worked and you appear licenced again. Good to see this nasty bug fixed too :)

Xenon
02-11-2004, 02:40 PM
yes, that would explain the short unlicensed period ;)

Natch
02-11-2004, 02:52 PM
To confirm, does the bug report showing fixed in RC4 mean that it was reported under RC4 ? or fixed for RC4 ?

Freddie Bingham
02-11-2004, 02:56 PM
To confirm, does the bug report showing fixed in RC4 mean that it was reported under RC4 ? or fixed for RC4 ?
Fixed for Gold. Try this: else
{ // imodcache is not set - do a query

if (isset($modcache["$userid"]))
{
return $modcache["$userid"];
}

$modcache["$userid"] = 0;

DEVDEBUG('QUERY: is the user a moderator (any forum)?');
$ismod_all = $DB_site->query("SELECT moderatorid, permissions FROM " . TABLE_PREFIX . "moderator WHERE userid = $userid");
while ($ismod = $DB_site->fetch_array($ismod_all))
{
if ($do)
{
if ($ismod['permissions'] & $_BITFIELD['moderatorpermissions']["$do"])
{
$modcache["$userid"] = 1;
break;
}
}
else
{
$modcache["$userid"] = 1;
break;
}
}

return $modcache["$userid"];
}

As for can_moderate generating a query on many pages for non admins/supermods, I didn't even realize that was happening. I don't think that is really acceptable so we will see what we can do about it.

Freddie Bingham
02-11-2004, 02:58 PM
Fixed for Gold. Try this: else
{ // imodcache is not set - do a query

if (isset($modcache["$userid"]))
{
return $modcache["$userid"];
}

$modcache["$userid"] = 0;

DEVDEBUG('QUERY: is the user a moderator (any forum)?');
$ismod_all = $DB_site->query("SELECT moderatorid, permissions FROM " . TABLE_PREFIX . "moderator WHERE userid = $userid");
while ($ismod = $DB_site->fetch_array($ismod_all))
{
if ($do)
{
if ($ismod['permissions'] & $_BITFIELD['moderatorpermissions']["$do"])
{
$modcache["$userid"] = 1;
break;
}
}
else
{
$modcache["$userid"] = 1;
break;
}
}

return $modcache["$userid"];
}

As for can_moderate generating a query on many pages for non admins/supermods, I didn't even realize that was happening. I don't think that is really acceptable so we will see what we can do about it.
Oops I see that could have a problem if you call can_moderate() on the same user using different 'do' actions. I'll have to fix that one but this should get you by.

filburt1
02-12-2004, 01:55 AM
Good thing it was fixed...I relied on this function for a lot of stuff in custom templates I develop.

Boofo
02-12-2004, 02:01 AM
Where would you add that code at?

Xenon
02-12-2004, 12:10 PM
@Bob: in the can_moderate function in functions.php

good thing, i used my selfwritten ismod() function for such things which remained from vb2 times ^^

Boofo
02-12-2004, 12:21 PM
In place of this? Or is there some more?

else
{ // imodcache is not set - do a query
DEVDEBUG('QUERY: is the user a moderator (any forum)?');
$ismod_all = $DB_site->query("SELECT moderatorid, permissions FROM " . TABLE_PREFIX . "moderator WHERE userid = $userid");
while ($ismod = $DB_site->fetch_array($ismod_all))
{
if ($do)
{
if ($ismod['permissions'] & $_BITFIELD['moderatorpermissions']["$do"])
{
return 1;
}
}
else
{
return 1;
}
}
return 0;
}

Xenon
02-12-2004, 03:20 PM
correct just that one :)

Boofo
02-12-2004, 03:42 PM
Thank you, sir. ;)

Natch
02-13-2004, 12:07 AM
Thanks for the clarification :)

KuraFire
02-13-2004, 07:53 AM
I reported this bug back in beta 5 or so already, and then they just told me "you shouldn't use can_moderate() in a postbit" :|