Symfony 1.4为1个查询提出15个DB请求

时间:2011-02-21 22:55:49

标签: symfony1 doctrine

  

可能重复:
  got a select that does 10 query in doctrine (Symfony)

我目前在我的symfony应用程序中遇到了一个大问题,我有一个复杂的SQL查询,它使数据库连接过多。

查询顺序,使用doctrine searchable搜索关键字,并检查地理位置。

这里粘贴完整的模型和sql日志,任何人都知道我做错了什么? :/

public function getListItems($orderby, $budget, $motscles, $userid, $catID, $useDistance = true)
{

   // CHANGE ORDER 
   if(!$orderby){
     $orderby = "a.created_at DESC";
   }else if($orderby == "price"){
     $orderby = "a.price ASC";
   }else if($orderby == "date") {
     $orderby = "a.created_at DESC";
    }else{
     $orderby = "a.created_at DESC";
   }

   // Search Keywords in table
   if($motscles){
    $searchItem = Doctrine_Core::getTable('csw_Article');
    $results = $searchItem->search($motscles);
    $ids = array();

    foreach ($results as $result) {

        $ids[] = $result['id'];
    }
    if(sizeof($ids) == 0){
      $ids[] = 0;
    }
   }        
    $q = Doctrine_Core::getTable('csw_Article')
        ->createQuery("a")
        ->leftJoin('a.csw_CategorieArticle ca');

    $sfContext = sfContext::getInstance()->getUser();

    if($useDistance){
        $lat = (string)($sfContext->getAttribute('userLat')) ? $sfContext->getAttribute('userLat') : sfConfig::get("app_user_lat");
       $long = (string)($sfContext->getAttribute('userLong')) ? $sfContext->getAttribute('userLong') : sfConfig::get("app_user_long");  
        $radius = 18;
        $q->select("a.longitude, a.latitude, (3959 * acos(cos(radians('".$lat."')) * cos(radians(latitude)) * cos(radians(longitude) - radians('".$long."')) + sin(radians('".$lat."')) * sin(radians(latitude)))) AS distance");
        $q->having("distance < ?", $radius);
    }
    if($orderby == "distance") {            
        $q->orderBy("distance desc");   
    }               
        $q->addOrderBy($orderby);

     if($catID){
       $q->where('ca.categorie_id = ?', $catID);
     }
     if($budget != 0){

      $budget_min = $budget - ($budget * 0.20);
      $budget_max = $budget + ($budget * 0.20);
      $q->addwhere('a.price > ?',$budget_min)
        ->addwhere('a.price < ?',$budget_max);
     }  
     if($userid){
       $q->addwhere('a.userid = ?', $userid);
     }


     if($motscles){
       $q->whereIn('a.id', $ids);

     }        
     $q->execute();      
     return $q;
}

Sql log

#

SELECT c.id AS c__id, c.longitude AS c__longitude, c.latitude AS c__latitude, (3959 * ACOS(COS(radians('45.854719')) * COS(radians(c.latitude)) * COS(radians(c.longitude) - radians('-73.23246')) + SIN(radians('45.854719')) * SIN(radians(c.latitude)))) AS c__0 FROM csw__article c LEFT JOIN csw__categorie_article c2 ON c.id = c2.article_id WHERE (c2.categorie_id = '1') HAVING c__0 < '18' ORDER BY c.created_at DESC
0.00s, "doctrine" connection
#

SELECT COUNT(*) AS num_results FROM (SELECT c.id, (3959 * ACOS(COS(radians('45.854719')) * COS(radians(c.latitude)) * COS(radians(c.longitude) - radians('-73.23246')) + SIN(radians('45.854719')) * SIN(radians(c.latitude)))) AS c__0 FROM csw__article c LEFT JOIN csw__categorie_article c2 ON c.id = c2.article_id WHERE c2.categorie_id = '1' GROUP BY c.id HAVING c__0 < '18') dctrn_count_query
0.00s, "doctrine" connection
#

SELECT DISTINCT c3.id, (3959 * ACOS(COS(radians('45.854719')) * COS(radians(c3.latitude)) * COS(radians(c3.longitude) - radians('-73.23246')) + SIN(radians('45.854719')) * SIN(radians(c3.latitude)))) AS c3__0 FROM csw__article c3 LEFT JOIN csw__categorie_article c4 ON c3.id = c4.article_id WHERE c4.categorie_id = '1' HAVING c3__0 < '18' ORDER BY c3.created_at DESC LIMIT 10
0.00s, "doctrine" connection
#

SELECT c.id AS c__id, c.longitude AS c__longitude, c.latitude AS c__latitude, (3959 * ACOS(COS(radians('45.854719')) * COS(radians(c.latitude)) * COS(radians(c.longitude) - radians('-73.23246')) + SIN(radians('45.854719')) * SIN(radians(c.latitude)))) AS c__0 FROM csw__article c LEFT JOIN csw__categorie_article c2 ON c.id = c2.article_id WHERE c.id IN ('1', '20', '11', '2', '21', '12', '3', '13', '4', '14') AND (c2.categorie_id = '1') HAVING c__0 < '18' ORDER BY c.created_at DESC
0.00s, "doctrine" connection
#

SELECT c.id AS c__id, c.userid AS c__userid, c.title AS c__title, c.city AS c__city, c.description AS c__description, c.image AS c__image, c.price AS c__price, c.featured AS c__featured, c.state AS c__state, c.zipcode AS c__zipcode, c.is_public AS c__is_public, c.is_activated AS c__is_activated, c.expires_at AS c__expires_at, c.created_at AS c__created_at, c.updated_at AS c__updated_at, c.latitude AS c__latitude, c.longitude AS c__longitude FROM csw__article c WHERE (c.id = '1') LIMIT 1
0.00s, "doctrine" connection
#

SELECT c.id AS c__id, c.userid AS c__userid, c.title AS c__title, c.city AS c__city, c.description AS c__description, c.image AS c__image, c.price AS c__price, c.featured AS c__featured, c.state AS c__state, c.zipcode AS c__zipcode, c.is_public AS c__is_public, c.is_activated AS c__is_activated, c.expires_at AS c__expires_at, c.created_at AS c__created_at, c.updated_at AS c__updated_at, c.latitude AS c__latitude, c.longitude AS c__longitude FROM csw__article c WHERE (c.id = '20') LIMIT 1
0.00s, "doctrine" connection
#

SELECT c.id AS c__id, c.userid AS c__userid, c.title AS c__title, c.city AS c__city, c.description AS c__description, c.image AS c__image, c.price AS c__price, c.featured AS c__featured, c.state AS c__state, c.zipcode AS c__zipcode, c.is_public AS c__is_public, c.is_activated AS c__is_activated, c.expires_at AS c__expires_at, c.created_at AS c__created_at, c.updated_at AS c__updated_at, c.latitude AS c__latitude, c.longitude AS c__longitude FROM csw__article c WHERE (c.id = '14') LIMIT 1
0.00s, "doctrine" connection
#

SELECT c.id AS c__id, c.userid AS c__userid, c.title AS c__title, c.city AS c__city, c.description AS c__description, c.image AS c__image, c.price AS c__price, c.featured AS c__featured, c.state AS c__state, c.zipcode AS c__zipcode, c.is_public AS c__is_public, c.is_activated AS c__is_activated, c.expires_at AS c__expires_at, c.created_at AS c__created_at, c.updated_at AS c__updated_at, c.latitude AS c__latitude, c.longitude AS c__longitude FROM csw__article c WHERE (c.id = '13') LIMIT 1
0.00s, "doctrine" connection
#

SELECT c.id AS c__id, c.userid AS c__userid, c.title AS c__title, c.city AS c__city, c.description AS c__description, c.image AS c__image, c.price AS c__price, c.featured AS c__featured, c.state AS c__state, c.zipcode AS c__zipcode, c.is_public AS c__is_public, c.is_activated AS c__is_activated, c.expires_at AS c__expires_at, c.created_at AS c__created_at, c.updated_at AS c__updated_at, c.latitude AS c__latitude, c.longitude AS c__longitude FROM csw__article c WHERE (c.id = '12') LIMIT 1
0.00s, "doctrine" connection
#

SELECT c.id AS c__id, c.userid AS c__userid, c.title AS c__title, c.city AS c__city, c.description AS c__description, c.image AS c__image, c.price AS c__price, c.featured AS c__featured, c.state AS c__state, c.zipcode AS c__zipcode, c.is_public AS c__is_public, c.is_activated AS c__is_activated, c.expires_at AS c__expires_at, c.created_at AS c__created_at, c.updated_at AS c__updated_at, c.latitude AS c__latitude, c.longitude AS c__longitude FROM csw__article c WHERE (c.id = '11') LIMIT 1
0.00s, "doctrine" connection
#

SELECT c.id AS c__id, c.userid AS c__userid, c.title AS c__title, c.city AS c__city, c.description AS c__description, c.image AS c__image, c.price AS c__price, c.featured AS c__featured, c.state AS c__state, c.zipcode AS c__zipcode, c.is_public AS c__is_public, c.is_activated AS c__is_activated, c.expires_at AS c__expires_at, c.created_at AS c__created_at, c.updated_at AS c__updated_at, c.latitude AS c__latitude, c.longitude AS c__longitude FROM csw__article c WHERE (c.id = '4') LIMIT 1
0.00s, "doctrine" connection
#

SELECT c.id AS c__id, c.userid AS c__userid, c.title AS c__title, c.city AS c__city, c.description AS c__description, c.image AS c__image, c.price AS c__price, c.featured AS c__featured, c.state AS c__state, c.zipcode AS c__zipcode, c.is_public AS c__is_public, c.is_activated AS c__is_activated, c.expires_at AS c__expires_at, c.created_at AS c__created_at, c.updated_at AS c__updated_at, c.latitude AS c__latitude, c.longitude AS c__longitude FROM csw__article c WHERE (c.id = '3') LIMIT 1
0.00s, "doctrine" connection
#

SELECT c.id AS c__id, c.userid AS c__userid, c.title AS c__title, c.city AS c__city, c.description AS c__description, c.image AS c__image, c.price AS c__price, c.featured AS c__featured, c.state AS c__state, c.zipcode AS c__zipcode, c.is_public AS c__is_public, c.is_activated AS c__is_activated, c.expires_at AS c__expires_at, c.created_at AS c__created_at, c.updated_at AS c__updated_at, c.latitude AS c__latitude, c.longitude AS c__longitude FROM csw__article c WHERE (c.id = '2') LIMIT 1
0.00s, "doctrine" connection
#

SELECT c.id AS c__id, c.userid AS c__userid, c.title AS c__title, c.city AS c__city, c.description AS c__description, c.image AS c__image, c.price AS c__price, c.featured AS c__featured, c.state AS c__state, c.zipcode AS c__zipcode, c.is_public AS c__is_public, c.is_activated AS c__is_activated, c.expires_at AS c__expires_at, c.created_at AS c__created_at, c.updated_at AS c__updated_at, c.latitude AS c__latitude, c.longitude AS c__longitude FROM csw__article c WHERE (c.id = '21') LIMIT 1
0.00s, "doctrine" connection
#

SELECT c.id AS c__id, c.sluggish AS c__sluggish, c.name AS c__name, c.parent_id AS c__parent_id, c.lft AS c__lft, c.rgt AS c__rgt, c.level AS c__level FROM csw__categorie c WHERE (c.parent_id = '1') ORDER BY c.parent_id ASC, c.lft ASC
0.00s, "doctrine" connection
#

SELECT s.id AS s__id, s.first_name AS s__first_name, s.last_name AS s__last_name, s.email_address AS s__email_address, s.username AS s__username, s.algorithm AS s__algorithm, s.salt AS s__salt, s.password AS s__password, s.is_active AS s__is_active, s.is_super_admin AS s__is_super_admin, s.last_login AS s__last_login, s.created_at AS s__created_at, s.updated_at AS s__updated_at FROM sf_guard_user s WHERE (s.id = '5') LIMIT 1

2 个答案:

答案 0 :(得分:1)

您可以启用xdebug_loggin来查找查询的来源:

//factories.yml
    dev:
      logger:
        param:
          loggers:
            sf_web_debug:
              param:
                xdebug_logging: true

这会将跟踪记录到每个查询中,并且应该为您提供出错的线索

答案 1 :(得分:0)

我认为问题不在于您是如何进行查询的,而是如何在操作或视图中使用它。我想你正在做的事情如下:

foreach($items as $item){
   $model->getListItems(...);
}

因此每个$ item都会有一次db调用。在按用户过滤时尝试使用WhereIn而不是addWhere。