我有这个脚本,但它需要很长时间才能加载,有时会超过加载时间,有没有办法可以改善加载时间?
public static function getSubAreas() {
// Get database object from global scope
global $database;
if(isset($_GET['PostAreaRef'])){ $PostAreaRef = trim($_GET['PostAreaRef']); } else { $PostAreaRef = "0"; }
$resultA = $database->execute("SELECT AgentRef FROM SearchGroupsAgent WHERE SearchGroupRef = '21'");
$AgentRefs = array();
while($rowA = $database->fetch($resultA)) {
$AgentRefs[] = '\''.$rowA['AgentRef'].'\'';
}
$AgentRefs = implode(',',$AgentRefs);
// Build database query
$sql = "SELECT SubAreaRef, count(*) AS Count FROM PropertySale WHERE StatusRef = 1 AND Archived = 0 AND AgentRef IN ($AgentRefs) GROUP BY SubAreaRef ORDER BY Count DESC";
// Get data from database
$result = $database->execute($sql);
// Initialize array to hold objects
$objects = array();
// Fetch rows from database cursor
while($row = $database->fetch($result)) {
$object = new self;
$GroupAreaSQL = $database->execute("SELECT ID,SubArea FROM SubArea WHERE ID = '".$row['SubAreaRef']."'");
$GroupArea = $database->fetch($GroupAreaSQL);
if ($GroupArea['SubArea'] == "-- Not Specified --")
{
}
else
{
if ($GroupArea['AreaRef'] == $PostAreaRef)
{
// Initialize object's attributes
$object->ID = $GroupArea['ID'];
$object->SubArea = $GroupArea['SubArea'];
$object->Count = $row['Count'];
$objects[] = $object;
}
}
}
// Return array of objects
return $objects;
}
这段代码:
public static function getAreas2() {
$db = odbc_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD);
$resultA = odbc_exec($db, "SELECT AgentRef FROM SearchGroupsAgent WHERE SearchGroupRef = '21'");
$AgentRefs = array();
while($rowA = odbc_fetch_array($resultA)) {
$AgentRefs[] = '\''.$rowA['AgentRef'].'\'';
}
$AgentRefs = implode(',',$AgentRefs);
$query = odbc_exec($db, "SELECT ID,Area FROM Area ORDER BY Area");
while($Area = odbc_fetch_array($query))
{
$Counter = 0;
$query2 = odbc_exec($db, "SELECT ID,SubArea FROM SubArea WHERE AreaRef = '".$Area['ID']."' ");
while($SubArea = odbc_fetch_array($query2))
{
if ($SubArea['SubArea'] != "-- Not Specified --")
{
$result3 = odbc_exec($db, "SELECT count(*) AS Count FROM PropertySale WHERE StatusRef = 1 AND Archived = 0 AND SubAreaRef = '".$SubArea['ID']."' AND AgentRef IN ($AgentRefs)");
$PropertySale = odbc_fetch_array($result3);
if ($PropertySale['Count'] > 0)
{
$AreaID = $Area['ID'];
$AreaName = $Area['Area'];
$Counter = $Counter + $PropertySale['Count'];
}
}
else
{
$AreaID = $Area['ID'];
$AreaName = "";
$Counter = $Counter;
}
}
if ($Counter != 0)
{
$object = new self;
// Initialize object's attributes
$object->ID = $AreaID;
$object->Area = $AreaName;
$object->Count = $Counter;
$objects[] = $object;
}
}
// Return array of objects
return $objects;
}
这些是我的表格:
区域: ID,面积
子区域: ID,SubArea,AreaRef
PropertySale: ID,地址,SubAreaRef
答案 0 :(得分:0)
因此,我认为您的代码可能会导致执行许多SQL语句。这可能是昂贵的,特别是如果您通过TCP访问您的数据库,这可能是ODBC的情况。您希望寻找一种方法来减少必须执行的SQL语句的数量,从而减少网络往返次数,这是一项代价高昂的操作。我无法将语法拉到最顶层,但我认为您可以通过执行连接保存数据的三个表的语句将其减少为单个SQL语句。然后,您只需要执行一个语句,您想要的信息将返回到单个结果集中。这应该会大大减少网络往返次数,因此您应该看到性能提升。在编写用于数据访问的应用程序时,始终需要考虑减少网络往返。以下是SQL JOIN语法教程的链接,可能有助于您入门:http://ow.ly/yqTYm