使用PHP时查询速度较慢

时间:2013-08-25 15:46:00

标签: php mysql sql

我在本地计算机上运行php,我的本地计算机上也有heidisql。我刚刚在digitalocean上设置了一个服务器,我在其上安装了mysql。当我运行时,我在本地计算机上使用PHP运行查询,页面运行查询大约需要7.81秒。当我将查询复制到heidisql时,它需要运行相同的查询0.141秒。什么会导致PHP变慢?

注意:在我切换到数字海洋之前,PHP版本大约运行1秒,并且php没有改变。

这是我正在运行的PHP:

$db = new Database();

$start = microtime(true);

$db->query("
set @miles = 10;

select
@lat := latitude,
@lon := longitude,
@city := accent,
@country := country,
#@region := region,
@city_id := cc.city_id
from cities cc
inner join countries c on cc.country_id = c.country_id
inner join regions r on cc.region_id = r.region_id and c.country_id = r.country_id
left join locations l on cc.city_id = l.city_id
where city = 'rosemount' and country = 'us' and r.region = 'mn'
limit 1;
");

$cities = $db->getAll("
SELECT c.city_id as id, 
accent as city, 
country as country, 
region, 
latitude, 
longitude,
distance(@lat, @lon, latitude, longitude) as `distance`,
direction(@lat, @lon, latitude, longitude) `direction`
FROM locations l
inner join cities c on l.city_id = c.city_id
inner join regions r on c.region_id = r.region_id
inner join countries cu on c.country_id = cu.country_id
WHERE MBRContains(
    LineString(
        Point(
            @lat + @miles / 68.703, 
            @lon + @miles / (68.703 / COS(RADIANS(@lat)))
        ), 
        Point(
            @lat - @miles / 68.703, 
            @lon - @miles / (68.703 / COS(RADIANS(@lat)))
        )
    ), location)
and l.city_id != @city_id
order By `distance`
");

echo "<p>" . round(microtime(true) - $start, 2) . "</p>";

echo "<pre>";
print_r($cities);
echo "</pre>";

0 个答案:

没有答案