时间:2010-07-26 17:50:19

标签: mysql

3 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

这是我写的内容限制。您必须修改限制以匹配您的环境。我将结果分成每个记录批次500个并处理,然后循环到下一个$records_per_batch

<?php
// variables
$counter = 0;
$records_per_batch = 500;
$total_records = 0;
$app_root = "/var/run/consumers";

// mark the start time
system("/bin/touch $app_root/clean.last_start");
$link = mysqli_connect("localhost", "username", "password", "database") or die(mysqli_error());

// get the total amount of records to process
$sql = "SELECT COUNT(*) from table";
$result = mysqli_query($link, $sql) or die("couldn't execute sql: $sql" . mysql_error());

if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_array($result);
    $total_records = $row[0];
}

// iterate through the records at $records_per_batch at a time
$sql_template = "SELECT * FROM table order by table_id limit %s, %s";

while ($counter < $total_records) {
    $sql = sprintf($sql_template,$counter,$records_per_batch);
    $result = mysqli_query($link, $sql) or die("couldn't execute sql: $sql" . mysql_error());

    if ($result) {
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result)) {
                // do your work here.                
            }
        }
    } else {
        print "hmm, no result\n";
        exit;
    }
    $counter += $records_per_batch;
}

mysqli_close($link);
system("/bin/touch $app_root/clean.last_end");
?>

答案 2 :(得分:1)

我知道这篇文章已经过时了,但我认为值得一提的是or也可能导致问题。

这里有一些连接,并且仅为or值指定t2,这是我认为你想要的。这应该会限制您的结果集。

select t1.c1,t2.c2 
from table1 t1
inner join table2 t2 on t2.column1=t1.column1
inner join table3 t3 pm t2.column2=t3.column2 
where t3.column3='<value>' 
and t2.column2 IN('<value1>','<value2>');

50K记录不是太大,无法提供所描述的问题类型。您可以尝试通过索引加入或限制的列来优化表格,例如t2.column1t2.column2等。