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 07-22-2014, 11:17 AM
tpearl5's Avatar
tpearl5 tpearl5 is offline
 
Join Date: Nov 2001
Location: PA
Posts: 1,014
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Count words in PHP or mysql?

I'm writing a plugin that needs to count the number of words in the first post of a thread. What would be better in this case? Selecting the pagetext and then counting in php or just doing everything with one mysql select statement?

OR should I just build something to count the number of words in each thread, store the value in the post table, and just select that when needed?
Reply With Quote
  #2  
Old 07-22-2014, 11:28 AM
Dave Dave is offline
 
Join Date: May 2010
Posts: 2,583
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

It depends; how much load do both versions cause and how long do both versions need to finish its execution?
Reply With Quote
  #3  
Old 07-22-2014, 12:08 PM
cellarius's Avatar
cellarius cellarius is offline
 
Join Date: Aug 2005
Posts: 1,987
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

On what occasion, i.e. when and where do you intend to run this count?
Reply With Quote
  #4  
Old 07-22-2014, 12:14 PM
tpearl5's Avatar
tpearl5 tpearl5 is offline
 
Join Date: Nov 2001
Location: PA
Posts: 1,014
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Dave View Post
It depends; how much load do both versions cause and how long do both versions need to finish its execution?
Yeah, that's what I'm thinking about. I have no easy way to measure the load of each and was hoping someone could provide some insight. The query I would be using looks something like this:
Code:
SELECT LENGTH(pagetext) - LENGTH(REPLACE(pagetext, ' ', ''))+1 AS words FROM post WHERE parentid='0' AND threadid=123;
And would potentially run on every thread page load. It seems redundant to run this every time if the count of the words isn't absolutely critical. It would only change if someone edits the post, and that isn't really a big deal.

Maybe I'm answering my own question here and should just store the value. I'm sure a select statement without math is less expensive, especially if you have a really long thread.
Reply With Quote
  #5  
Old 07-22-2014, 12:32 PM
Dave Dave is offline
 
Join Date: May 2010
Posts: 2,583
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm pretty sure it would be faster to do this PHP. However, I think that storing the value would also be a nice alternative.
Reply With Quote
  #6  
Old 07-22-2014, 12:49 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yeah, it may or may not make enough difference to worry about whether to have the db server do the actual counting or to do it in php, but storing it sidesteps the issue and avoids an extra query, so I'd probably do that. And you mentioned above adding it to the post table, but from what you said it sounds like the thread table would be better.
Reply With Quote
  #7  
Old 07-22-2014, 02:09 PM
tpearl5's Avatar
tpearl5 tpearl5 is offline
 
Join Date: Nov 2001
Location: PA
Posts: 1,014
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Dave View Post
I'm pretty sure it would be faster to do this PHP. However, I think that storing the value would also be a nice alternative.
Quote:
Originally Posted by kh99 View Post
Yeah, it may or may not make enough difference to worry about whether to have the db server do the actual counting or to do it in php, but storing it sidesteps the issue and avoids an extra query, so I'd probably do that. And you mentioned above adding it to the post table, but from what you said it sounds like the thread table would be better.
Well, either way you'd have a query since you still have to get the data. Storing in the thread table would require a join that could slow things down on very large forums. The data is available in the post table, so I figured that would be easier. Either way, I'm not seeing any slowness with running the query on the fly. Meh, I don't know, I'll have to see how it works.
Reply With Quote
  #8  
Old 07-22-2014, 02:41 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by tpearl5 View Post
Well, either way you'd have a query since you still have to get the data. Storing in the thread table would require a join that could slow things down on very large forums. ...
I was basing that on the idea that if you add a column to the thread table it would get read in with the rest of the row for the current thread, so you wouldn't have to do anything else. But I'm not 100% sure of that, I'd have to check the code.

If I understood you, you want the word count for the first post of a thread even if it's not being displayed on the current page? Then you'd either have to store that same count in each post of the thread or else always read the first post.
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 02:38 PM.


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.05883 seconds
  • Memory Usage 2,231KB
  • 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
  • (1)bbcode_code
  • (4)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_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