0

I have 45 queries each one of them update a value in the table odds this value is taken from an array $odds_results for example $odds_results['1'] saved if the value of the column tahmin equals to 1 and so on Like this:

$GLOBALS['db']->query("UPDATE odds_tahminler SET result = $odds_results[1], timestamp = timestamp where match_id= $match_id AND tahmin = 1");
    $GLOBALS['db']->query("UPDATE odds_tahminler SET result = $odds_results[2], timestamp = timestamp where match_id= $match_id AND tahmin = 2");
    $GLOBALS['db']->query("UPDATE odds_tahminler SET result = $odds_results[3], timestamp = timestamp where match_id= $match_id AND tahmin = 3");
    $GLOBALS['db']->query("UPDATE odds_tahminler SET result = $odds_results[4], timestamp = timestamp where match_id= $match_id AND tahmin = 4");
    $GLOBALS['db']->query("UPDATE odds_tahminler SET result = $odds_results[5], timestamp = timestamp where match_id= $match_id AND tahmin = 5");

I have 45 queries that execute in the same page in a cron job each 5 minutes that makes my CPU usage about 77% for this process . and that is killing my web server . Is there a way to solve this because I am not very familiar with MySQL queries ? Is there anyway to optimize this to reduce the CPU usage ?

  • Do you have indexes on `match_id` and `tahmin`? – Andy Gee Oct 20 '14 at 06:47
  • You could start by not injecting PHP variables directly into your query string. You're wide open to an SQL injection attack. – GordonM Oct 20 '14 at 06:49
  • 1
    @GordonM Those values are tested before inserting in the queries – Basel Shbeb Oct 20 '14 at 06:50
  • @BaselShbeb That's a start but whatever solution you have is probably not flawless. It would be an awfully good idea to switch out string substitution for prepared statements to give you an additional safety net. – GordonM Oct 20 '14 at 06:53

0 Answers0