非常慢的PHP - MySQL脚本

时间:2014-10-11 21:30:55

标签: php mysql sql

我是使用PHP-MySQL的新手。我有两个MySQL表:

  • 具体性:包含80K字的具体分数的表
  • Brian:一张有100万行的表,每行包含一两个单词。

我有一个小的PHP脚本,它将每一行放在" Brian",解析它,在" Concreteness"中寻找分数。并将其记录在" Brian。"

我一直在运行这个脚本,其他几个表有300-400k行,每行数百个字。 "布赖恩"是不同的,因为它有100万行,每行1或2个字。出于某种原因,我的脚本对Brian来说太慢了。

这是实际的脚本:

 <?php
include "functions.php";
set_time_limit(0); // NOTE: no time limit
if (!$conn)
    die('Not connected : ' . mysql_error());
$remove = array('{J}','{/J}','{N}','{/N}','{V}','{/V}','{RB}','{/RB}'); // tags to remove       
$db = 'LCM';
mysql_select_db($db);

$resultconcreteness = mysql_query('SELECT `word`, `score` FROM `concreteness`') or die(mysql_error());
$array = array(); // NOTE: init score cache
while($row = mysql_fetch_assoc($resultconcreteness))
    $array[strtolower($row['word'])] = $row['score']; // NOTE: php array as hashmap
mysql_free_result($resultconcreteness);

$data = mysql_query('SELECT `key`, `tagged` FROM `brian`') or die(mysql_error()); // NOTE: single query instead of multiple
while ($row = mysql_fetch_assoc($data)) {
    $key = $row['key'];
    $tagged = $row['tagged'];
    $weight = $count = 0;
    $speech = explode(' ', $tagged);
    foreach ($speech as $word) {
        if (preg_match('/({V}|{J}|{N}|{RB})/', $word, $matches)) {
            $weight += $array[strtolower(str_replace($remove, '', $word))]; // NOTE: quick access to word's score
            if(empty($array[strtolower(str_replace($remove, '', $word))])){}else{$count++;}

        }
    }
    mysql_query('UPDATE `brian` SET `weight`='.$weight.', `count`='.$count.' WHERE `key`='.$key, $conn) or die(mysql_error());
// Print out the contents of the entry 
        Print "<b>Key:</b> ".$info['key'] .  " <br>";  
}
mysql_free_result($data);
?> 

1 个答案:

答案 0 :(得分:0)

我猜真正的问题是你发送到数据库的100万个mysql更新语句。考虑捆绑更新语句(并删除打印):

$i=0;
while ($row = mysql_fetch_assoc($data)) {

  // ... left out the obvious part

  $sql .= "'UPDATE `brian` SET `weight`='.$weight.', `count`='.$count.' WHERE `key`='.$key;";

  $i++;
  if ($i%1000 == 0) {
    mysql_query($sql) or die(mysql_error());
    $i=0;
    $sql = "";
  }
}
// remember to save the last few updates
mysql_query($sql) or die(mysql_error());