Symfony的1.4使得15所DB要求1个查询[复制](Symfony 1.4 makes 15

2019-10-31 07:04发布

这个问题已经在这里有答案 :

可能重复:
有一个选择,做教义10查询(Symfony的)

目前,我有我的symfony应用程序的一个大问题,我有一个配合物的SQL查询,使太多的连接的数据库。

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

这里完整的模型粘贴和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日志

#

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

Answer 1:

您可以启用xdebug_loggin找出您查询发源于:

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

这将跟踪记录每个查询,应该给你一个线索什么地方出了错



Answer 2:

我认为,问题不在于你怎么样你是如何在行动或在视图中使用它做查询,但英寸 我猜你正在做的事情,如:

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

所以就会有一个呼叫dB,各$项目。 尝试使用的addWhere代替,其中,当用户过滤器。



文章来源: Symfony 1.4 makes 15 DB requests for 1 query [duplicate]