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 12-31-2002, 09:19 PM
Link14716's Avatar
Link14716 Link14716 is offline
 
Join Date: Jun 2002
Location: Georgia, USA
Posts: 2,519
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Optimization Needed

I created a script to manage cheat codes for my gaming site, but to count the codes per game, it adds a query per game. Example:

Code:
--------------------------------------------------------------------------------

Query: SELECT COUNT(*) AS codes FROM post WHERE site='2' && threadid='176'
Time before: 0.32487499713898
Time after:  0.32634603977203

--------------------------------------------------------------------------------

Query: SELECT COUNT(*) AS codes FROM post WHERE site='2' && threadid='177'
Time before: 0.32819998264313
Time after:  0.32953000068665

--------------------------------------------------------------------------------

Query: SELECT COUNT(*) AS codes FROM post WHERE site='2' && threadid='138'
Time before: 0.33139204978943
Time after:  0.3329199552536

--------------------------------------------------------------------------------

Query: SELECT COUNT(*) AS codes FROM post WHERE site='2' && threadid='369'
Time before: 0.33477103710175
Time after:  0.33609294891357

--------------------------------------------------------------------------------
Now, it's not a big deal now, being such a small site, but I wouild like to see how I could optimize the code. Here is the complete action taken from the script:

Code:
// START GAME LIST
if ($do=="glist") {

$alphaparent=$DB_site->query_first("SELECT * FROM forum WHERE forumid='$alphaid' ORDER BY displayorder ASC");
$sysname=$DB_site->query_first("SELECT * FROM forum WHERE forumid='$alphaparent[parentid]' ORDER BY displayorder ASC");
$sysname=$sysname[title];
$sysletter=$alphaparent[title];

$typequery=$DB_site->query("SELECT * FROM thread WHERE forumid='$alphaid' ORDER BY title ASC LIMIT 100");
while ($glist=$DB_site->fetch_array($typequery)) {

$gname=$glist[title];
$gid=$glist[threadid];

$countjokes2=$DB_site->query_first("SELECT COUNT(*) AS codes FROM post WHERE site='2' && threadid='$gid'");
$totalgcodes=number_format($countjokes2['codes']);

if ($totalgcodes=="1") {
$c="code";
} else {
$c="codes";
}

  eval("\$gbit .= \"".gettemplate("site_v$v" . "_codes_gbits")."\";");
}
eval("dooutput(\"".gettemplate("site_v$v" . "_codes_glistdisplay")."\");");
}
Thanks in advance.
Reply With Quote
  #2  
Old 01-01-2003, 12:10 AM
futureal futureal is offline
 
Join Date: Feb 2002
Location: Del Mar, CA, USA
Posts: 556
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

It sounds like you would be better off just grabbing all of the threadids from the post table and then counting those using a PHP loop, without any additional queries.

Something like:

PHP Code:
$allthreads $DB_site->query("SELECT threadid FROM post WHERE site='2'");

while (
$eachthread $DB_site->fetch_array($allthreads))
  
$thread_table[$eachthread[threadid]]++;

foreach (
$thread_table as $k => $v)
  echo 
"threadid $k has $v entries<br>"
That would output a list of how many posts are in each thread that matches site=2. Basically, the same thing that your original code did, but with a single query.
Reply With Quote
  #3  
Old 01-01-2003, 09:16 PM
Link14716's Avatar
Link14716 Link14716 is offline
 
Join Date: Jun 2002
Location: Georgia, USA
Posts: 2,519
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well, I guess I am going to have to remember that.

[high]* Link14716 bookmarks for reference.
[/high]

Thanks.
Reply With Quote
  #4  
Old 01-22-2003, 02:07 AM
Link14716's Avatar
Link14716 Link14716 is offline
 
Join Date: Jun 2002
Location: Georgia, USA
Posts: 2,519
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I can't find a good way to put that into the above action. It keeps repeating things, and I still have no knowlege of the correct syntax/placement to make it where that query is only executed once, doesn't keep repeating other things, doesn't keep repeating other queries, and actually works. Can someone help me?
Reply With Quote
  #5  
Old 01-22-2003, 02:17 AM
mr e's Avatar
mr e mr e is offline
 
Join Date: Dec 2001
Posts: 461
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

couldn't you do

PHP Code:
SELECT count(*) AS codes176count(*) AS codes177count(*) AS codes138count(*) AS codes369 FROM post WHERE site='2' && threadid='176'WHERE site='2' && threadid='177'WHERE site='2' && threadid='138'WHERE site='2' && threadid='369' 
or can you not do that, i dunno
Reply With Quote
  #6  
Old 01-22-2003, 02:21 AM
Link14716's Avatar
Link14716 Link14716 is offline
 
Join Date: Jun 2002
Location: Georgia, USA
Posts: 2,519
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Nope, becuase that'd kill the whole purpose of the threads being dynamic. It'd require me to add 1,000 or so things to one query, and a concious effort to do so. But hey, thanks anyways

Also, there are 36 threads to deal with now. So, that'd be a long query, and not only would it be hard to manage and unnecessary. I know there is a better way, and if I could get that loop futureal posted to not be looped itself by the fetch_array, then it should all work

EDIT: Also, that would work if I were dealing with static HTML basically, but that is not the case this time.
Reply With Quote
  #7  
Old 01-22-2003, 02:32 AM
mr e's Avatar
mr e mr e is offline
 
Join Date: Dec 2001
Posts: 461
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

well foo there goes all my mysql knowledge out the window
Reply With Quote
  #8  
Old 01-22-2003, 02:35 AM
Link14716's Avatar
Link14716 Link14716 is offline
 
Join Date: Jun 2002
Location: Georgia, USA
Posts: 2,519
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

lol, don't worry, it would be useful, but since the threadid's are dynamic and they all must be running on the same variable, it would not be suitable for my problem. My way still works, but sooner or later there will be 40-50 queries with a lot of people viewing the page, and I want to prevent that from happening.
Reply With Quote
  #9  
Old 01-31-2003, 09:30 PM
Link14716's Avatar
Link14716 Link14716 is offline
 
Join Date: Jun 2002
Location: Georgia, USA
Posts: 2,519
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

^^BuMp^^
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 10:38 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.06243 seconds
  • Memory Usage 2,248KB
  • Queries Executed 11 (?)
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
  • (2)bbcode_code
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)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_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