list->mysql query iteration for cron job


/ Published in: PHP
Save to your folder(s)

I needed to have a cron job that did some MySQL work iterating through a long list of items based on the content of a table in MySQL. I wanted each item in the table to be iterated over roughly once every 5 minutes, but building up the in-memory list of items to be processed based on the full contents of the table (~100 rows) was killing server memory and the process was dying. I wanted to find a way to, based on the time, only process a segment of the table's results, and to be reasonable confident that they would all be processed approximately every 5 minutes.

Really simple problem, actually, but it took me a while to find the right logic...


Copy this code and paste it in your HTML
  1. $x = ceil( substr( date( "i" ), 1, 1) / 2 ); //get number from 1 to 5
  2. $min = ( 20 * ( $x - 1 ) ) + 1; //gets 1, 21, 41, 61, 81
  3. $max = ( 20 * $x ); //gets 20, 40, 60, 80, 100
  4.  
  5. $feedListSql = quotesmart( "SELECT DISTINCT id, feed_url FROM {$this->feedTable} WHERE active = 1 AND id >= {$min} AND id <= {$max}" );

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.