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

Reply
 
Thread Tools Display Modes
  #1  
Old 10-06-2016, 12:54 AM
Mattwhf Mattwhf is offline
 
Join Date: May 2016
Posts: 190
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Slow queries in this command, how to fix it?

Hello,

I read the log file and it noticed me this Mysql command is making my forum slow, with more than 15 seconds to query it.

I found it in an addon that I am using on my forum

$rcthrds = $vbulletin->db->query_read("
SELECT thread.threadid, thread.title, thread.lastpost, thread.lastpostid, thread.forumid, thread.lastposter, thread.lastposterid, thread.postusername, thread.dateline, thread.views, thread.replycount, thread.visible, thread.open, thread.prefixid, forum.title AS forumtitle, user.username, user.userid, user.usergroupid, user.displaygroupid, user.avatarid, user2.avatarid AS avatarid2
" . iif($vbulletin->options['threadpreview'], ",post.pagetext AS preview") . "
" . iif($vbulletin->options['avatarenabled'], ',avatar.avatarpath, NOT ISNULL(customavatar.userid) AS hascustomavatar, customavatar.dateline AS avatardateline') . "
" . iif($vbulletin->options['avatarenabled'], ',avatar2.avatarpath AS avatarpath2, NOT ISNULL(customavatar2.userid) AS hascustomavatar2, customavatar2.dateline AS avatardateline2') . "
FROM " . TABLE_PREFIX . "thread AS thread
LEFT JOIN " . TABLE_PREFIX . "user AS user ON (user.username = thread.postusername)
LEFT JOIN " . TABLE_PREFIX . "user AS user2 ON (user2.username = thread.lastposter)
INNER JOIN " . TABLE_PREFIX . "forum AS forum ON (forum.forumid = thread.forumid)
" . iif($vbulletin->options['threadpreview'], "LEFT JOIN " . TABLE_PREFIX . "post AS post ON (post.postid = thread.firstpostid)") . "
" . iif($vbulletin->options['avatarenabled'], "LEFT JOIN " . TABLE_PREFIX . "avatar AS avatar ON(avatar.avatarid = user.avatarid) LEFT JOIN " . TABLE_PREFIX . "customavatar AS customavatar ON(customavatar.userid = user.userid)") . "
" . iif($vbulletin->options['avatarenabled'], "LEFT JOIN " . TABLE_PREFIX . "avatar AS avatar2 ON(avatar2.avatarid = user2.avatarid) LEFT JOIN " . TABLE_PREFIX . "customavatar AS customavatar2 ON(customavatar2.userid = user2.userid)") . "
WHERE NOT ISNULL(thread.threadid) AND thread.visible = '1' AND thread.open!='10' $forumfilter
ORDER BY rand(), lastpost DESC
LIMIT 0, {$vbulletin->options['rcmt_reclimit']}
");

I think this command

ORDER BY rand(), lastpost DESC

is the reason caused slow queries on my forum.

How to fix this?
Reply With Quote
  #2  
Old 10-06-2016, 12:22 PM
noypiscripter's Avatar
noypiscripter noypiscripter is offline
 
Join Date: Jul 2013
Posts: 468
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Is that for vB5? That seems to be for older version. vB5 has no thread table. Either way, you have to ask the plugin author.

What is the query supposed to do, btw?
Reply With Quote
Благодарность от:
Mattwhf
  #3  
Old 10-06-2016, 12:42 PM
Mattwhf Mattwhf is offline
 
Join Date: May 2016
Posts: 190
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by noypiscripter View Post
Is that for vB5? That seems to be for older version. vB5 has no thread table. Either way, you have to ask the plugin author.

What is the query supposed to do, btw?
You are right, I wanted to post to this forum https://vborg.vbsupport.ru/forumdisplay.php?f=252

Don't know why I posted here. Mod please moves it to vB4 Programming Discussions. Thanks

In the codes above

I doubted this line causes error slow query

ORDER BY rand(), lastpost DESC

It will choose a random thread in all forums to show on the box. I think it made my apache (httpd) down and Mysql server run slowly.
Reply With Quote
  #4  
Old 10-06-2016, 02:39 PM
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Location: Nottingham, UK
Posts: 23,748
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yep, that will probably be slow as hell on a forum with loads of threads.

It wont directly bring apache down, although if you have loads of visitors and everyone is running that query, and taking that long, its going to have some serious effects, and you will likely run out of resources for httpd to run properly (and mysql if its on the same box).

The lastpost in the Order By seems a bit redundant as well.

Turn that plugin off.
Reply With Quote
  #5  
Old 10-06-2016, 02:47 PM
Dave Dave is offline
 
Join Date: May 2010
Posts: 2,583
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

More specifically, the rand() function in the query causes it to be slow.
A "solution" would be to generate the random number in PHP and use that as the threadid in a where statement in the query. The only downside is that the threadid may not exist if it has been deleted.
Reply With Quote
  #6  
Old 10-06-2016, 03:11 PM
cellarius's Avatar
cellarius cellarius is offline
 
Join Date: Aug 2005
Posts: 1,987
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The probability of the id not existing should be comparatively small on an average forum, and you could get around that issue easily. Just run the randomly generated id against the database, and if it does not exist, i.e. the result is empty, start over: generate another one, and so on.
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 05:50 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.08848 seconds
  • Memory Usage 2,223KB
  • 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_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (6)post_thanks_box
  • (1)post_thanks_box_bit
  • (6)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (6)post_thanks_postbit_info
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)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
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete