我已经破解了一些可能在效率和加载速度方面有所改进的PHP代码。我怎样才能改进它?我绝不记得编码!!
目前该页面需要大约5-10秒才能加载。
它循环遍历数百个wordpress帖子,将它们放在一个新的mysql表中并对lat / long进行地理编码。它还检查帖子是否已经在数据库中。
以下是以下代码:
<?php
require("database.php");
// Opens a connection to a MySQL server
$con = mysql_connect("localhost", $username, $password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("medicom_wp", $con);
$pages = get_posts(array(
'orderby' => 'title',
'post_type' => 'members',
'numberposts' => 300,
'post_status' => 'any'
));
foreach($pages as $post) {
setup_postdata($post);
$company = get_field('company_name');
$address = get_field('address');
$city = get_field('city');
$post_code = get_field('post_code');
$sql = sprintf("select count('x') as cnt from markers where `name` = '%s'", mysql_real_escape_string($company));
$row_dup = mysql_fetch_assoc(mysql_query($sql,$con));
if ($row_dup['cnt'] == 0) {
mysql_query("INSERT INTO markers (`name`, `address`, `lat`, `lng`, `type`) VALUES ('".$company."', '".$address.", ".$city.", ".$post_code."', '0.0', '0.0', '')");
}
}
wp_reset_query();
define("MAPS_HOST", "maps.google.com");
define("KEY", "");
// Opens a connection to a MySQL server
$connection = mysql_connect("localhost", $username, $password);
if (!$connection) {
die("Not connected : " . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die("Can\'t use db : " . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}
// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;
// Iterate through the rows, geocoding each address
while ($row = @mysql_fetch_assoc($result)) {
$geocode_pending = true;
while ($geocode_pending) {
$address = $row["address"];
$id = $row["id"];
$request_url = $base_url . "&q=" . urlencode($address);
$xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
// Successful geocode
$geocode_pending = false;
$coordinates = $xml->Response->Placemark->Point->coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];
$query = sprintf("UPDATE markers " .
" SET lat = '%s', lng = '%s' " .
" WHERE id = '%s' LIMIT 1;",
mysql_real_escape_string($lat),
mysql_real_escape_string($lng),
mysql_real_escape_string($id));
$update_result = mysql_query($query);
if (!$update_result) {
die("Invalid query: " . mysql_error());
}
} else if (strcmp($status, "620") == 0) {
// sent geocodes too fast
$delay += 1000;
} else {
// failure to geocode
$geocode_pending = false;
echo "Address " . $address . " failed to geocoded. ";
echo "Received status " . $status . "
\n";
}
usleep($delay);
}
}
?>
答案 0 :(得分:3)
您需要重新构建代码以使用multi-insert SQL查询。这应该会大大提高性能。
而不是做这样的事情,数百次:
INSERT INTO table ( col1, col2) VALUES ( val1, val2)
INSERT INTO table ( col1, col2) VALUES ( val3, val4)
...
你这样做了一次:
INSERT INTO table ( col1, col2) VALUES ( val1, val2), ( val3, val4)
现在,让我们将它应用到您的代码中:
$values = array();
foreach($pages as $post) {
setup_postdata($post);
$company = get_field('company_name');
$address = get_field('address');
$city = get_field('city');
$post_code = get_field('post_code');
$values[] = "( '$company', '$address', '$city', '$post_code', 0.0, 0.0, '')";
}
$query = 'INSERT IGNORE INTO markers (`name`, `address`, `lat`, `lng`, `type`)
VALUES ' . implode( ', ', $values);
现在,不是检查SQL行是否存在(使用$row_dup
查询),而是确保name
列上有主键,并执行INSERT IGNORE
或{ {1}}。
您可以从this SO question了解有关这些查询的详情。
答案 1 :(得分:1)
许多插入语句会降低你的速度,如果你正在做很多事情你可以将你的数据解析成csv文件,然后使用LOAD DATA INFILE加载,在大数据集的情况下,性能差异可能很大,
链接
http://php.net/manual/en/function.fputcsv.php
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
@nickb有一个肯定有帮助的建议
答案 2 :(得分:0)
您还可以通过缓存地理编码请求中的数据来加快速度,以限制下次请求时对Google地理编码服务的调用次数。我通常会保存请求的时间以及请求的准确性或“location_type”。然后我检查每个标记的准确性和上次请求的时间,看看我是否应该更新信息。准确度越高,请求之间的时间就越长。
https://developers.google.com/maps/documentation/geocoding/
这是基本的,但你有几个准确性选项
if($accuracy != 'ROOFTOP' || time() > ($update_time + 2592000))//check every 30 days
{
//make a geocode request
}
ROOFTOP的准确性与Google的地理编码一样好。除非Google改变某些内容,否则无需继续更新。这就是我将它设置为30天的原因。