php - Most efficient way to calculate 'popularity' of objects on website -


ok i'm building site people can post news, comments, questions, etc. people can rate of these objects, favorite of them, share them, etc. site php+mysql. wrote script in php following:

  1. grab comments , scores added them in past 5 minutes. add record 'popularity' table change in popularity each comment object.
  2. grab news , scores/views/favorites/shares added them. calculate popularity each news story (taking account change in popularity of comments attached them step 1) , insert record popularity table change in popularity each news object.
  3. repeat step 2 questions , other object types

i tried run script (it's symfony task) every 5 minutes cron job , php started choking , eating of server resources.

what preferred way run background analytics script calculates new data based on data in mysql db, inserts calculated data db? i'm sure i'm missing basic procedures here. should note db on different server , server had no resource problems. problem seems confined php choking on application server looping through objects, calculating popularity (simple calculations), , inserting db.

thanks

-- edit

how replicating db server used calculations. run popularity script on calculation server replicated db , insert calculated popularity records live db. of course delayed that's not huge deal. i'm not sure if fix php resource consumption issue though.

well first thing try reduce number of queries execute. important if sql , web servers on different machines. try use joins calculate popularity of news items without getting through comments individually.

well can calculate popularity of comments , popularity of new items in same query. (eg select sum(rating) news, comments, rating comments.news_id = news.id , rating.comment_id = comments.id (this query oversimplified still...)) cuz main problem amount of queries have execute , there enough resources on mysql server. because of time sql server wait next query arrive. communications across network gazillion times slower between cpu , ram. happens is: php sends query mysql server , waits response. mysql gets query processes, sends response , waits next query. waiting taking time... either reduce amount of queries or send quires in same time using mysqli http://php.net/manual/en/mysqli.multi-query.php


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -