【问题标题】:Improve Loading time for query提高查询的加载时间
【发布时间】:2014-06-25 10:04:55
【问题描述】:

我有这个脚本,但是加载时间很长,有时会超过加载时间,有什么办法可以改善加载时间吗?

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

物业销售: ID、地址、SubAreaRef

【问题讨论】:

    标签: php sql sql-server odbc


    【解决方案1】:

    我认为您的代码可能会导致执行许多 SQL 语句。这可能代价高昂,尤其是当您通过 TCP 访问数据库时,这很可能是 ODBC 的情况。

    您必须寻找一种方法来减少必须执行的 SQL 语句的数量,从而减少成本高昂的网络往返次数。我无法将语法从脑海中抽离出来,但我认为您可以通过执行一条连接保存数据的三个表的语句来将其简化为单个 SQL 语句。

    然后你只需要执行一个语句,你想要的信息就会在一个结果集中返回。这应该会大大减少网络往返次数,因此您应该会看到性能提升。在为数据访问编写应用程序时,始终需要考虑减少网络往返次数。

    Here 是指向 SQL JOIN 语法教程的链接,它可能会帮助您入门。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多