2

我正在尝试从具有巨大表的数据库构建 XML 提要,几乎有 4k 条记录。我想使用输出缓冲来让它吐出 XML,但脚本仍然会超时。

ob_start();
$what = 'j.*, (select description from tb_job_type as jt WHERE jt.jobtype_id =  j.job_type_id) as job_type,';
$what .= '(select description from tb_location as l WHERE l.location_id = j.location_id) as location,';
$what .= '(select description from tb_industry as i WHERE i.industry_id = j.industry_id) as industry';
$where = ('' != $SelectedType) ?  'j.job_ad_type="' . $SelectedType .'"' : '';
$process = $db->executeQuery('SELECT ' . $what . ' FROM tb_job_ad as j' . $where);

while($result = mysql_fetch_array($process))
{
    $result['job_title_url']        = $form->urlString($result['job_title']);
    $result['job_title']            = htmlspecialchars($result['job_title'], ENT_QUOTES, 'UTF-8');
    $result['short_description']    = htmlspecialchars($result['short_description'], ENT_QUOTES, 'UTF-8');
    $result['full_description']     = htmlspecialchars($result['full_description'], ENT_QUOTES, 'UTF-8');
    $result['company_name']         = ucwords(strtolower($result['company_name']));
    $tpl->assignToBlock('ITEMS', $result);
}
$cheese = ob_get_contents();    
$actualize = $tpl->actualize('FEED');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/xml");
echo $actualize;
ob_flush();
print $cheese;
ob_end_clean();

这似乎是使脚本窒息的那一行:

$tpl->assignToBlock('ITEMS', $result);

请帮忙?

谢谢

米黛安。

4

3 回答 3

1

可能是您的查询速度相当慢吗?
比较输出

set_time_limit(60);
$process = $db->executeQuery('EXPLAIN SELECT ' . $what . ' FROM tb_job_ad as j' . $where);
while($result = mysql_fetch_array($process, MYSQL_ASSOC)) {
  echo join(' | ', $result), "<br />\n";
}

使用EXPLAIN 优化查询

于 2009-09-15T11:28:42.917 回答
0

您可以使用 set_time_limit(0) 让您的脚本在没有任何超时的情况下永远运行并等待它完成执行。

于 2009-09-15T13:07:38.200 回答
0

几乎可以肯定会发生超时,因为您的查询速度很慢——您几乎可以肯定地通过确保索引正确的列并执行一些 JOIN 来提高它的性能。

如果您像这样重写查询构造会怎样:

$q = 'SELECT 
  j.*, 
  jt.description as job_type, 
  l.description as location, 
  i.description as industry
FROM tb_jobs AS j
INNER JOIN tb_job_type AS jt ON j.job_type_id = jt.jobtype_id 
INNER JOIN tb_location AS l ON j.location_id = l.location_id
INNER JOIN tb_industry AS i ON j.indsutry_id = i.industry_id';

if (! empty($SelectedType)){
  $where = "WHERE j.job_ad_type = '$SelectedType'";
}else{
  $where = '';
}

$q .= $where;

确保您的所有外键列(j.location_id 等)都已编入索引。

如果您希望输出更快开始,您将需要 - 查询数据库 - 输出所有标题等 - 编写 while 循环,如:

ob_end_flush();flush();
while($row = mysql_fetch_assoc($process)
{
  ob_start();
  //build your ITEM and echo it here
  ob_end_flush(); flush();
}

PS:我还注意到您在 SO 上提出的另一个与 SQL 相关的问题。我认为通过更好地理解 SQL 的工作原理,你会得到很好的服务。我强烈建议您获取一份“ SLQ Clearly Explained ”的副本——它提供了标题所承诺的内容——对 SQL 的清晰解释(一般来说,不会陷入讨论各种实现的困境)

于 2009-09-15T17:47:38.913 回答