移动大型记录表mysql的最快方法

时间:2012-08-08 09:00:53

标签: php mysql

请帮我找到这个解决方案。我处理一个非常大的数据记录,如2M的学生测试结果记录。所有记录都存储在一个表中,我需要做的是将这个记录分成两个不同的表,一个对于所有通过学生,另一个是失败的学生。在分离此记录之后,我需要从源表中删除原始记录。运行此代码时需要花费数小时才能完成。如果有人可以帮我查找解决方案会很棒。谢谢。

这是示例代码:

<?php ob_start(); ?>
<?php
function spread($classselect,$subjectselect)
{include "connectdb.php";

//movepass

$selectpass = mysql_query("SELECT * FROM testresultAUG2012 WHERE actualmonth='AUG' AND testresult = 'PASS' AND classname = '$classselect' AND educationsubject = $subjectselect;",$conn);
$totalpass = mysql_num_rows($selectpass);
$pass=0;
while($pass<$totalpass)
{
    $name=mysql_result($selectpass, $pass, "Studentname");
    $subject=mysql_result($selectpass, $pass, "Subject");
    $testbatch=mysql_result($selectpass, $pass, "testbatch");
    $classname=mysql_result($selectpass, $pass, "class");
    $lecturer=mysql_result($selectpass, $pass, "lecturer");
    $date=mysql_result($selectpass, $pass, "date");
    $month=mysql_result($selectpass, $pass, "month");
    $time=mysql_result($selectpass, $pass, "time");
    $state=mysql_result($selectpass, $pass, "result");

    //count total pass subject and add 1
    $totalpass = mysql_query("SELECT * FROM Student WHERE Studentname = '$name';",$conn);
    $only = mysql_fetch_array($theone);
    $currentpass = $only['totalpass'];
    $newpass = $currentpass + 1;
    mysql_query("UPDATE Student SET totalpass = $newpass WHERE Studentname = '$name';",$conn);

    //save record to pass table
    mysql_query("INSERT INTO passtab(......) VALUES(....);",$conn);

    //remove data from origin table
    mysql_query("DELETE FROM testresultAUG2012 WHERE Studentname = '$name' AND testbatch = $testbatch AND Subject=$subject AND result = 'PASS' AND class = '$classname';",$conn);
    $pass++;
}

//moveFailed

$selectfail = mysql_query("SELECT * FROM testresultAUG2012 WHERE actualmonth='AUG' AND testresult = 'PASS' AND classname = '$classselect' AND educationsubject = $subjectselect;",$conn);
$totalfail = mysql_num_rows($selectfail);
$fail=0;
while($fail<$totalfail)
{
    $name=mysql_result($selectfail, $fail, "Studentname");
    $subject=mysql_result($selectpass, $fail, "Subject");
    $testbatch=mysql_result($selectfail, $fail, "testbatch");
    $classname=mysql_result($selectfail, $fail, "class");
    $lecturer=mysql_result($selectfail, $fail, "lecturer");
    $date=mysql_result($selectfail, $fail, "date");
    $month=mysql_result($selectfail, $fail, "month");
    $time=mysql_result($selectfail, $fail, "time");
    $state=mysql_result($selectfail, $fail, "result");

    //count total fail subject and add 1
    mysql_query("UPDATE Student SET totalfail = totalfail + 1  WHERE Studentname = '$name';",$conn);

    //save record to fail table
    mysql_query("INSERT INTO failtab(....) VALUES(...);",$conn);


    //remove data from origin table
    mysql_query("DELETE FROM testresultAUG2012 WHERE Studentname = '$name' AND testbatch = $testbatch AND Subject=$subject AND result = 'FAIL' AND class = '$classname';",$conn);
    $fail++;
    }
}
?>
<?php
$relatedclass=$_POST[classup];
$relatedsubject=$_POST[subject];
spread($relatedclass,$relatedsubject);
echo "<center>Spread Completed !!!</CENTER>";

?>
<? ob_flush(); ?> 

2 个答案:

答案 0 :(得分:1)

您不应该从数据库中获取所有数据,然后逐行重新发送。

如果你的草坪上有一袋沙子,你想把它给你的邻居,你会怎么做?你会留下一张纸条告诉他他能接受吗?或者你会先把整个行李寄给你在俄罗斯的叔叔,然后告诉你的叔叔一次把它贴回你的邻居吗?处理数据的方式更像是后一种方法。

做这样的事情:

INSERT INTO table1
(col1, col2, ... coln)
SELECT col1, col2, ... coln
FROM table2
WHERE result = 'PASS'

答案 1 :(得分:0)

你可以从while循环中删除查询并在循环外运行查询但是在loop.i中生成查询字符串而不是每次都可以使用一个查询插入所有数据时运行查询,你可以尝试以下解决方案

while($pass<$totalpass)
{
    $name=mysql_result($selectpass, $pass, "Studentname");
    $subject=mysql_result($selectpass, $pass, "Subject");
    $testbatch=mysql_result($selectpass, $pass, "testbatch");
    $classname=mysql_result($selectpass, $pass, "class");
    $lecturer=mysql_result($selectpass, $pass, "lecturer");
    $date=mysql_result($selectpass, $pass, "date");
    $month=mysql_result($selectpass, $pass, "month");
    $time=mysql_result($selectpass, $pass, "time");
    $state=mysql_result($selectpass, $pass, "result");

    //count total pass subject and add 1
    $totalpass = mysql_query("SELECT * FROM Student WHERE Studentname = '$name';",$conn);
    $only = mysql_fetch_array($theone);
    $currentpass = $only['totalpass'];
    $newpass = $currentpass + 1;


    //save record to pass table
   $values[]='('.$valuestring.')'(//whatever you want to ineser)

    //remove data from origin table

    $pass++;
}

mysql_query(插入tyablename(&#39;&#39;)值(implode(&#39;,&#39;,$ values)))