php - High CPU usage when using cron job and sending emails -


i have website sends daily writing reminders users based on timezone , hour settings. eg: user in new york (america/new_york) prefers receive reminder @ 9pm. in current implementation (after many changes), store timezone , notify_hour in settings table , find users complex , heavy mysql query.

the query slow, added indexes , made faster. after find users try send emails through external smtp server (sendgrid.com). ive set cron job run every 10 minutes , check 200 users. have 2000 active users. works fine this. of users located in iran (asia/tehran) , of them have selected 9pm receive reminder. peak hour between 8pm , 11pm irst.

but server admin says cron job uses cpu. 25% high. tried debug script, query fast when comes sending emails through smtp server, takes longer:

email reminder has been sent 4 people - query: 0.75444 seconds ---- system: 11.18443 seconds. ----  email reminder has been sent 0 people - query: 0.65488 seconds ---- system: 0.68821 seconds. ----  email reminder has been sent 5 people - query: 0.75551 seconds ---- system: 3.45176 seconds. ----  email reminder has been sent 1 people - query: 2.37117 seconds ---- system: 9.05586 seconds. ---- 

first number how long query took , second time whole system load.

this script use cron job: http://pastebin.com/eee3lzys
mailer class connects smtp server on construction , sends email on each iteration of script. (sorry english!)

can suggest better way achieve whole thing or minimizing cpu usage or something?
thanks.

please not insert in loop

foreach($users $k => $v){  

use multi raw insert fast other sql query pefomance depends on data on live server.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -