我的自动生成xml站点地图代码工作得很慢,我可以提高它的性能吗?

时间:2009-11-22 01:03:53

标签: php html xml

我在下面编写了一个自动生成站点地图代码,每隔12小时调用一次。 它查看mysql数据库,然后为它在mysql表中找到的每一行生成xml文件的输入。

我不是最好的PHP编码器,所以我希望你们能帮我改进下面的内容。

它可以正常工作,但拥有超过400,000个广告可能会让它变得非常缓慢。

 function xml_sitemap() {
    $website_url="/SV/Sitemap/";
    $ads_query_results = mysql_query("SELECT * FROM cars_db ORDER BY insert_date DESC") or die(mysql_error());
    $nr_of_sitemaps = count(glob('../../Sitemap/*')); 
    //$nr_files=1;
    $row_data_seek=1;

    for ($i=1; $i<$nr_of_sitemaps; $i++){
        if ($nr_of_sitemaps!='.' || $nr_of_sitemaps!='..'){
            $nr_files++;
        }
    }
    for ($i=1; $i<=$nr_of_sitemaps; $i++){
        unlink('../../Sitemap/Sitemap'.$i.'.xml');
    }
    $number_of_ads = mysql_num_rows($ads_query_results);
    $files_needed = ($number_of_ads/49500);
    $files_needed = ceil($files_needed);

for ($i=1; $i<=$files_needed; $i++){
    $xml_file = fopen ('../../Sitemap/Sitemap'.$i.'.xml','w');
    fwrite ($xml_file,"<?xml version='1.0' encoding='UTF-8'?>\n");
    fwrite ($xml_file,"<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>\n");

        for ($z=0; $z<($number_of_ads/$files_needed); $z++){
        mysql_data_seek($ads_query_results, $z);
        $row_results=mysql_fetch_array($ads_query_results);

        $current_ad_id= $website_url.$row_results['ad_category'].'/';
        $current_ad_id = $current_ad_id.$row_results['ad_id'].'.htm';
        $last_modified = date('Y-m-d', strtotime($row_results['insert_date']));
        $change_frequency = 'weekly';

            fwrite ($xml_file, "<url>\n");
                fwrite ($xml_file, "<loc>");
                fwrite ($xml_file, $current_ad_id);
                fwrite ($xml_file, "</loc>\n");
                fwrite ($xml_file, "<lastmod>");
                fwrite ($xml_file, $last_modified);
                fwrite ($xml_file, "</lastmod>\n");
                fwrite ($xml_file, "<changefreq>");
                fwrite ($xml_file, $change_frequency);
                fwrite ($xml_file, "</changefreq>\n");
                fwrite ($xml_file, "<priority>");
                fwrite ($xml_file, 0.8);
                fwrite ($xml_file, "</priority>\n");
            fwrite ($xml_file, "</url>\n");
        } //end for

    fwrite ($xml_file, "</urlset>");    
    fclose ($xml_file);
}//end main for
}

1 个答案:

答案 0 :(得分:1)

据我所知,您的代码已经过相当优化。你看到了什么样的构建时间?分钟?小时?

您可以直接进行一些更改:

更改此代码:

for ($z=0; $z<($number_of_ads/$files_needed); $z++){

$count = ($number_of_ads/$files_needed);
for ($z=0; $z<$count; $z++){

循环的每次迭代都将运行终止表达式。如果你有很多记录(如400,000),你将会遇到明显的减速。

合并您的fwrite声明:

fwrite ($xml_file, "<url>\n");
    fwrite ($xml_file, "<loc>");
    fwrite ($xml_file, $current_ad_id);
    fwrite ($xml_file, "</loc>\n");
    fwrite ($xml_file, "<lastmod>");
    fwrite ($xml_file, $last_modified);
    fwrite ($xml_file, "</lastmod>\n");
    fwrite ($xml_file, "<changefreq>");
    fwrite ($xml_file, $change_frequency);
    fwrite ($xml_file, "</changefreq>\n");
    fwrite ($xml_file, "<priority>");
    fwrite ($xml_file, 0.8);
    fwrite ($xml_file, "</priority>\n");
fwrite ($xml_file, "</url>\n");

可能会成为:

fwrite($xml_file, "<url><loc>$current_ad_id</loc><lastmod>$last_modified</lastmod><changefreq>$change_frequency</changefreq><priority>0.8</priority></url>");

当然,它更麻烦,但是一个fwrite优于10 +。

另外,尽量避免使用strtotime。这是一个缓慢,肮脏的功能。如果可以,尝试使用一些简单的逻辑来绕过它。

希望这有帮助!