vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Count words in PHP or mysql? (https://vborg.vbsupport.ru/showthread.php?t=313144)

tpearl5 07-22-2014 11:17 AM

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?

Dave 07-22-2014 11:28 AM

It depends; how much load do both versions cause and how long do both versions need to finish its execution?

cellarius 07-22-2014 12:08 PM

On what occasion, i.e. when and where do you intend to run this count?

tpearl5 07-22-2014 12:14 PM

Quote:

Originally Posted by Dave (Post 2507782)
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.

Dave 07-22-2014 12:32 PM

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.

kh99 07-22-2014 12:49 PM

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.

tpearl5 07-22-2014 02:09 PM

Quote:

Originally Posted by Dave (Post 2507790)
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 (Post 2507793)
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.

kh99 07-22-2014 02:41 PM

Quote:

Originally Posted by tpearl5 (Post 2507801)
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.


All times are GMT. The time now is 01:56 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.01096 seconds
  • Memory Usage 1,731KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (4)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (8)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete