我目前从不同的表格中获得200万条记录,以生成一个用于创建站点地图的网址。该脚本占用了太多资源,并占用了100%的服务器性能

询问

 SELECT CONCAT("/url/profile/id/",u.id,"/",nickname) as url FROM users AS u
    UNION ALL
    Select CONCAT("url/city/", c.id, "/paramId/",p.id,"/",Replace(p.title, " ", "+"),"/",r.region_Name,"/",c.city_Name) AS url
    From city c
    Join region r On r.id = c.id_region
    Join country country On country.id = c.id_country
    cross join param p
    Where country.used = 1
    And p.active = 1


// i将其存储在$ url_list数组中,然后创建站点地图..但是这需要时间和大量资源

//我尝试使用LIMIT 0,50000批量获取数据
但是获取分页的最大行数需要时间。代码也不太适合我必须运行两个查询,这些查询具有大数据

$url_list = array();


$maxrow = SELECT COUNT(*) AS max from (
 SELECT CONCAT("/url/profile/id/",u.id,"/",nickname) as url FROM users AS u
        UNION ALL
        Select CONCAT("url/city/", c.id, "/paramId/",p.id,"/",Replace(p.title, " ", "+"),"/",r.region_Name,"/",c.city_Name) AS url
        From city c
        Join region r On r.id = c.id_region
        Join country country On country.id = c.id_country
        cross join param p
        Where country.used = 1
        And p.active = 1) as tmp

$limit = 50,000;
$bybatch = ceil($maxrow/$limit);
$start = 0;
for($i = 0;$i < $bybatch; $i++){
   // run query and store to $result
       (SELECT CONCAT("/url/profile/id/",u.id,"/",nickname) as url FROM users AS u
        UNION ALL
        Select CONCAT("url/city/", c.id, "/paramId/",p.id,"/",Replace(p.title, " ", "+"),"/",r.region_Name,"/",c.city_Name) AS url
        From city c
        Join region r On r.id = c.id_region
        Join country country On country.id = c.id_country
        cross join param p
        Where country.used = 1
        And p.active = 1 LIMIT $start,$limit);

     $start += $limit;
     //push to $url_list
     $url_list = array_push($result);
}


//完成后,我将使用它来创建站点地图

$linkCount = 1;
        $fileNomb = 1;
        $i = 0;
foreach ($url_list as $ul) {

            $i += 1;
            if ($linkCount == 1) {
                $doc  = new DOMDocument('1.0', 'utf-8');
                $doc->formatOutput = true;
                $root = $doc->createElementNS('http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset');
                $doc->appendChild($root);
            }


            $url= $doc->createElement("url");
            $loc= $doc->createElement("loc", $ul['url']);
            $url->appendChild($loc);
            $priority= $doc->createElement("priority",1);
            $url->appendChild($priority);


            $root->appendChild($url);

            $linkCount += 1;

            if ($linkCount == 49999) {
                $f = fopen($this->siteMapMulti . $fileNomb .'.xml', "w");
                fwrite($f,$doc->saveXML());
                fclose($f);

                $linkCount = 1;
                $fileNomb += 1;
            }

        }


还有更好的方法吗?还是要加快性能?

添加

为什么这比sql查询要快,但却消耗了100%的服务器资源和性能

$this->db->query('SELECT c.id, c.city_name, r.region_name, cr.country_name FROM city AS c, region AS r, country AS cr  WHERE r.id = c.id_region AND cr.id = c.id_country AND cr.id IN (SELECT id FROM country WHERE use = 1)');

$arrayCity = $this->db->recordsArray(MYSQL_ASSOC);

 $this->db->query('SELECT id, title FROM param WHERE active = 1');

$arrayParam = $this->db->recordsArray(MYSQL_ASSOC);

foreach ($arrayCity as $city) {
        foreach ($arrayParam as $param) {
          $paramTitle = str_replace(' ', '+', $param['title']);
          $url = 'url/city/'. $city['id'] .'/paramId/'. $param['id'] .'/'. $paramTitle .'/'. $city['region_name'] .'/'. $city['city_name'];
          $this->addChild($url);
        }
}

最佳答案

我建议您不要使用UNION,而只发出两个分开的查询。它将加速查询本身。
另外,正如您上面提到的,最好分批获取数据。

最后,不要收集内存中的所有数据。立即将其写入循环中的文件。

只需在开头打开文件,在循环中写入每个URL条目,然后在结尾关闭文件。

—打开文件进行写入

—计数查询用户表

—用LIMIT循环进行几次选择(就像您已经做的那样)

—就在循环while ($row = mysql_fetch_array())中,将每一行写入文件

然后再针对另一张表重复这种算法。
实现将数据写入文件的功能将很有用,因此您可以调用该功能并遵守DRY原则。

10-08 02:14