简体   繁体   English

PHP非常慢-MySQL脚本

[英]Very slow PHP - MySQL script

I am new at using PHP-MySQL. 我是使用PHP-MySQL的新手。 I have two MySQL tables: 我有两个MySQL表:

  • Concreteness: A table that contains concreteness scores for 80K words 具体性:包含80K个单词的具体性得分的表格
  • Brian: A table with 1 million rows, each containing one or two words. 布莱恩:一张有一百万行的表格,每行包含一个或两个单词。

I have a small PHP script that takes each row in "Brian", parses it, looks for the scores in "Concreteness" and records it in "Brian." 我有一个小的PHP脚本,它将“ Brian”中的每一行进行分析,在“ Concreteness”中查找分数并将其记录在“ Brian”中。

I have been running this script with several other tables that had 300-400k rows with each hundreds of words. 我一直在与其他几个具有300-400k行(每百个单词)的表一起运行此脚本。 "Brian" is different because it has 1 million rows with 1 or 2 words per row. “布莱恩”是不同的,因为它有100万行,每行1或2个单词。 For some reason, my script is SUPER slow with Brian. 由于某些原因,我的脚本在Brian的帮助下运行得太慢了。

Here is the actual script: 这是实际的脚本:

 <?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);
?> 

I guess the real problem is the 1 million mysql update statements you fire to the database. 我想真正的问题是您向数据库触发了100万个mysql更新语句。 Consider bundling the update statements (and also remove the print): 考虑捆绑更新语句(并删除打印):

$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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM