简体   繁体   English

为什么我的桌子需要这么长时间才能加载

[英]Why does my table take so long to load

I have a site that is going to show businesses near the user. 我有一个网站,它将向用户展示商家。 It uses Google APIs to do this. 它使用Google API来执行此操作。 It first grabs nearby businesses using the radar search, grabs the place_id and saves it into the database(which I will need later) and then uses the place ID to grab details about the place. 它首先使用雷达搜索来抓取附近的商家,抓取place_id并将其保存到数据库(稍后将需要),然后使用地点ID来抓取有关该地点的详细信息。 If certain criteria is met, then it displays the results in a table. 如果满足某些条件,则将结果显示在表格中。 However, it is taking a long time to load and I am trying to figure out why. 但是,加载需要很长的时间,我正试图找出原因。 If it is just too much information and that's the way it has to be then fine, but I feel like I am doing something inside the code to slow it down more than it should. 如果信息太多,那就必须这样,但是我觉得我在代码内部做一些事情,以使其减慢的速度超过预期。

<?php
$xml = simplexml_load_file("https://maps.googleapis.com/maps/api/place/radarsearch/xml?location=39.53,-89.33&radius=10000&type=establishment&key=MYKEY") or die("Error: Cannot create object");
foreach($xml->result as $get)
{
if($i==7) break;
$xml2 = simplexml_load_file("https://maps.googleapis.com/maps/api/place/details/xml?placeid=" . $get->place_id . "&key=MYKEY") or die("Error: Cannot create object");
$sql = "SELECT * FROM Places WHERE GoogleID = '".$get->place_id."'";
$records = $conn->query($sql);
$grab = $records->fetch_assoc();
if($records->num_rows > 0)
{
    //yay
}
Else
{
    $MakeNew = "INSERT INTO Places (GoogleID, ConfirmedHiring) VALUES ('".$get->place_id."', 'No')";
    if(mysqli_query($conn, $MakeNew))
    {
        $records = $conn->query($sql);
        $grab = $records->fetch_assoc();
    }
}
foreach($xml2->result->address_component as $item){if($item->type == "locality"){$placecity = $item->long_name;}}
echo "<tr>";
echo "<td data-title='Business'>" . $xml2->result->name . "</td>";  
echo "<td data-title='Location'>" . $placecity . "</td>";
echo "<td data-title='Confirmed Hiring'>" .$grab["ConfirmedHiring"]. "</td>";
echo "</tr>";
$i++;
}
?>

If you used a nearby search instead of a radar search, you wouldn't have to get the place details separately afterwards, the results would contain all of the details already. 如果您使用附近搜索而不是雷达搜索,则以后不必分别获取地点详细信息,结果将包含所有详细信息。 If that's not an option and you need to do radar search, you could at least do all of the details requests in parallel. 如果这不是一个选择,并且您需要进行雷达搜索,则至少可以并行执行所有详细信息请求。

Similarly, you could select all of the matching records from the DB using an IN query instead of selecting them one at a time, and you could insert all of the ones that are found to be missing with a single query as well. 同样,您可以使用IN查询从数据库中选择所有匹配的记录,而不是一次选择一个,并且也可以插入一个查询中所有丢失的记录。

Finally, if something is slow, use a profiler to find out why it's slow; 最后,如果速度缓慢,请使用探查器找出速度缓慢的原因。 it's faster and more reliable than asking the internet. 比询问互联网更快,更可靠。

  1. Avoid doing db insertion / select . 避免进行数据库插入/选择。 because 7 (+7) queries might trigger, you can reduce this to two queries. 由于可能会触发7(+7)个查询,因此您可以将其减少为两个查询。
$xml = simplexml_load_file("https://maps.googleapis.com/maps/api/place/radarsearch/xml?location=39.53,-89.33&radius=10000&type=establishment&key=MYKEY") or die("Error: Cannot create object");
    $places[] = array();
    foreach($xml->result as $get)
    {
        if($i==7) break;
        $xml2 = simplexml_load_file("https://maps.googleapis.com/maps/api/place/details/xml?placeid=" . $get->place_id . "&key=MYKEY") or die("Error: Cannot create object");
        $places[] = $get->place_id;
        foreach($xml2->result->address_component as $item){if($item->type == "locality"){$placecity =     $item->long_name;}}
            echo "";
            echo "" . $xml2->result->name . "";  
            echo "" . $placecity . "";
            echo "" .$grab["ConfirmedHiring"]. "";
            echo "";
            $i++;
        }
        if (count($places)) {
            $place_ids = implode(",", $places)
            $sql = "SELECT $place_id FROM Places WHERE GoogleID IN ($place_ids)";
            $records = $conn->query($sql);
            $grab = $records->fetch_array();
            if (count($grab)) {
                $new_place_ids = array_diff($place_ids, $grab)
            } else {
                $new_place_ids = $place_ids;
        }
        $sql_values = "";
        foreach ($new_place_ids as $index => $place_id)
        {
            if (!$sql_values) $sql_values .= ","
            $sql_values .= ('".$get->place_id."', 'No')
        } 
        if ($sql_values != "") {
            $MakeNew = "INSERT INTO Places (GoogleID, ConfirmedHiring) VALUES $sql_values";
            if(mysqli_query($conn, $MakeNew))
            {
                $records = $conn->query($sql);
                $grab = $records->fetch_assoc();
            }
        }
    }
  1. Store the xml response into a file and cache it for 24 hrs in server. 将xml响应存储到文件中,并将其在服务器中缓存24小时。 so you avoid further hits. 因此您避免了进一步的打击。 Before hitting the Google, you can check whether the file exist and if it is not older than 24 hrs. 在点击Google之前,您可以检查文件是否存在以及是否不超过24小时。 instead of the below 而不是下面
$xml2 = simplexml_load_file("https://maps.googleapis.com/maps/api/place/details/xml?placeid=" . $get->place_id . "&key=MYKEY") 
//do this 
    $contents= get_content("uploadfolder/".$get->place_id, "https://maps.googleapis.com/maps/api/place/details/xml?placeid=" . $get->place_id . "&key=MYKEY");

Ref - how to to this for get_content function definition at https://davidwalsh.name/php-cache-function 参考-如何在https://davidwalsh.name/php-cache-function上获取get_content函数定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM