Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 06-27-2012, 08:40 AM
James Birkett James Birkett is offline
 
Join Date: Jun 2009
Posts: 633
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Performance of One Query on fetch_musername Hook

I have one query being ran on the fetch_musername hook to grab the value of one custom user field. The query (off the top of my head) is:
Code:
$field = $vbulletin->db->query_first(sprintf("SELECT field16 FROM " . TABLE_PREFIX ."userfield WHERE userid = %d", $user['userid']));
I'm worried about the performance of this query and how it'll affect my server load.

Will this query be ran once and cached - or will it be ran every time the fetch_musername hook is ran?

If anyone can shed any light, that'd be great - thanks.
Reply With Quote
  #2  
Old 06-27-2012, 08:52 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well, of course fetch_musername() will be called at least once for each different user. The function does check the $user array that's passed as the first parameter to see if the musername field is set already, and if it is it returns right away. But of course that doesn't guarantee that it won't be called twice for a given user, if the same user array isn't passed both times.

You could make your code check for $user['field16'] being set before you do the query. And if still find your query is being done more than once per user, you could build you own cache. (You can see how many times your query is called by turning on debug mode and looking at the list of queries at the bottom of the page).

Edit:

Quote:
Originally Posted by James Birkett View Post
Will this query be ran once and cached - or will it be ran every time the fetch_musername hook is ran?
To answer this specifically, with the code you posted above it will be run each time the fetch_musername hook is run
Reply With Quote
  #3  
Old 06-27-2012, 10:32 AM
James Birkett James Birkett is offline
 
Join Date: Jun 2009
Posts: 633
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yeah, I've got it set so if field 16 doesn't exist then the query is ran. So far, it's ran on the visitor messages and who's online I think.

I forgot about debug mode... oops.

I'll look into that.

--------------- Added [DATE]1340797789[/DATE] at [TIME]1340797789[/TIME] ---------------

OK I've just checked this out and it's running the query once per instance of the username. If my name appears twice, it runs the query twice.

How would I go about caching this? Preferably without writing it to the DB.
Reply With Quote
  #4  
Old 06-27-2012, 11:54 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

No database needed. What I meant by caching is to keep previous answers in a global variable, because you're only caching for a single page view. I was thinking something like this:

PHP Code:
global $field16_cache;

if (!
is_array($field16_cache))
{
    
$field16_cache = array();
}

if (!isset(
$field16_cache[$user['userid']]))
{
    
$row $vbulletin->db->query_first(sprintf("SELECT field16 FROM " TABLE_PREFIX ."userfield WHERE userid = %d"$user['userid']));
    
$field16_cache[$user['userid']] = $row['field16'];
}
$field $field16_cache[$user['userid']]; 
Reply With Quote
  #5  
Old 06-27-2012, 01:01 PM
James Birkett James Birkett is offline
 
Join Date: Jun 2009
Posts: 633
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Why would you want $field16_cache to be an array? It only returns one value - or is that so it stores everyone's user ID?

Also, with a cache, how would I force a cache update when the value of the user field changes?
Reply With Quote
  #6  
Old 06-27-2012, 01:09 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Maybe we're talking about different things. You only have to worry about what happens on a single load of a page, so a user's field16 isn't going to change. On another page it might be different, but the cache goes away once the page is built, so it doesn't matter.

The idea of caching like I posted above would be that if you ended up needing the value of field16 more than once for the same user on the same page, then you'd save doing a query for it more than once. You said it was being called every time - if you meant every time the page loads, then this kind of cache won't help. You probably don't want a database cache just for all users' field16s. The best thing would be if there was already a query for userdata that you could modify to load the custom profile field values in the same query. But you couldn't do that from the fetch_musername hook - we'd have to know more about what you're doing.

I hope I haven't made things too complicated - maybe you shouldn't worry about it unless it becomes a problem.
Reply With Quote
  #7  
Old 06-27-2012, 02:33 PM
James Birkett James Birkett is offline
 
Join Date: Jun 2009
Posts: 633
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

OK, my caching is done thanks.
Reply With Quote
  #8  
Old 07-02-2012, 02:06 PM
Badshah93 Badshah93 is offline
 
Join Date: Jun 2010
Location: India
Posts: 505
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

how many different usernames are there on that page ?
if it is more than 5-10, then you should left join userfield table with user table.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:18 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04099 seconds
  • Memory Usage 2,236KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (1)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (8)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete