学说查询运行缓慢为什么?

时间:2011-06-03 12:03:33

标签: php mysql doctrine

任何人都可以帮助我

我使用doctrine运行查询。

我为必要的表格做了索引和foriegnkey参考

这是我的查询

echo "step1";echo date('h:i:s A');echo "<br/>";
        $query = new Doctrine_Query();
        $whereCond ='';
        $where='eng.id='.$campid;
        $select='';
        $search = '';
        /*if($queryv!=''){

             $where .= " AND c.".$quickSearchType." LIKE '%".$queryv."%' ";
        }*/
        if($letterPressed == 'All')
        {
            $where .="";
        }
        else if($letterPressed == 'Radian6')
        {
            $where .=" AND c.news_type = 1";
        }
        else if($letterPressed == 'Google News')
        {
            $where .=" AND c.news_type = 3";
        }
        else if($letterPressed == 'Google Blogs')
        {
            $where .=" AND c.news_type = 4";
        }
        else if($letterPressed == 'RSS Feeds')
        {
            $where .=" AND (c.news_type = 5 or  c.news_type = 6)";
        }
        else if($letterPressed == 'Hide Twitter')
        {
            $where .=" AND c.url NOT LIKE '%twitter.com%'";
        }
        else if($letterPressed == 'Hide Facebook')
        {
            $where .=" AND c.url NOT LIKE '%facebook.com%'";
        }
        else if($letterPressed == 'Hide Facebook-twitter')
        {
            $where .=" AND (c.url NOT LIKE '%twitter.com%' AND c.url NOT LIKE '%facebook.com%')";
        }
        else if($letterPressed == 'Show Twitter')
        {
            $where .=" AND c.url LIKE '%twitter.com%'";
        }
        else if($letterPressed == 'Show Facebook')
        {
            $where .=" AND c.url LIKE '%facebook.com%'";
        }
        else if($letterPressed == 'show Facebook-twitter')
        {
            $where .=" AND (c.url  LIKE '%twitter.com%' OR c.url LIKE '%facebook.com%')";
        }

        if($queryv!='' && $quickSearchType == 'title' && (trim($letterPressed) != 'Law' && trim($letterPressed) != 'LS Translate') ){

             $search = ' LOWER(c.title) LIKE "%'.mysql_real_escape_string($queryv).'%" ';
             //$search = ' LOWER(c.title) LIKE ?';
        }
        if($queryv!='' && $quickSearchType == 'domain')
        {
             $search = ' LOWER(c.domain) LIKE "%'.mysql_real_escape_string($queryv).'%" ';
             //$search = ' LOWER(c.domain) LIKE ?';

        }
        echo "step2";echo date('h:i:s A');echo "<br/>";

        $select='c.id, c.campaign_id, c.title as title, c.domain, c.news_type,c.url, c.active, c.article_date, c.created_date, c.modified_date, t.language_id, t.tr_translated_date, t.tr_translated_by, t.translation_state, t.tr_title, t.raw_title';
        if(trim($letterPressed) == 'Original')
        {
            $select='c.id, c.campaign_id, c.title as title, c.domain, c.news_type,c.url, c.active, c.article_date, c.created_date, c.modified_date, t.language_id, t.tr_translated_date, t.tr_translated_by, t.translation_state';
        }
        if(trim($letterPressed) == 'Law')
        {
            $select='c.id, c.campaign_id, c.domain, c.news_type,c.url, c.active,c.article_date, c.created_date, c.modified_date, t.raw_title as title, t.language_id, t.tr_translated_date, t.tr_translated_by, t.translation_state';
            if($queryv!='' && $quickSearchType == 'title'){

             $search = ' LOWER(t.raw_title) LIKE "%'.mysql_real_escape_string($queryv).'%"';
             //$search = ' LOWER(t.raw_title) LIKE ?';

            }
        }
        if(trim($letterPressed) == 'LS Translate')
        {
            $select='c.id, c.campaign_id, c.domain, c.news_type,c.url, c.active, c.article_date, c.created_date, c.modified_date, t.tr_title as title, t.language_id, t.tr_translated_date, t.tr_translated_by, t.translation_state';
            $where .=" AND t.translation_state = 1";
            if($queryv!='' && $quickSearchType == 'title'){

             $search = ' LOWER(t.tr_title) LIKE "%'.mysql_real_escape_string($queryv).'%" ';
             //$search = ' LOWER(t.tr_title) LIKE ?';
            }
        }
        $whereCond.=$where;  
        // , array("%".mysql_real_escape_string($queryv)."%")
        echo "step3";echo date('h:i:s A');echo "<br/>";
        $query->select($select)
              ->from('News c')
              ->where($whereCond)
               ->addWhere('eng.tr_language_id=t.language_id')
               ->andWhere('c.created_date >= DATE_SUB(CURDATE(),INTERVAL 90 DAY)' );
        if(!empty($search))
            $query->andWhere($search);     
        $query->leftJoin('c.TranslatedNews t on t.news_id = c.id')
              ->leftJoin('c.Campaigns eng on eng.id = c.campaign_id')            
              ->orderBy('c.'.$sortName. ' ' . $sortOrder)
              ->groupBy('c.title')
              ->addGroupBy('c.url')
              ->limit(10);
            echo "step4";echo date('h:i:s A');echo "<br/>"; 
            //echo $query->getSqlQuery();exit;
            $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
            echo "step5";echo date('h:i:s A');echo "<br/>";exit;
            return $result;

执行查询需要8秒。 如何使此查询快速运行

提前致谢

2 个答案:

答案 0 :(得分:1)

因为表格增长时LIKE很慢。

您应该实施Full Text搜索

所以你可以做到

WHERE MATCH(fields) AGAINST('your search')

答案 1 :(得分:0)

当您在%search-term%上放置通配符书籍结尾时,您的LIKE比较实际上是一个“包含子字符串”搜索。这样的搜索不能提供排序索引的典型优点(因为您的通配符表示忽略值开头的字符);因此必须检查表中的每个值(在相关列中)以查看它是否包含子字符串。

如果您搜索了电话簿WHERE SURNAME LIKE'%els%',则必须检查每个姓氏,而如果您执行WHERE SURNAME LIKE'Nels%',则只能搜索-N-名称,不包括名称从任何其他信开始。后者可以使用索引。

数据库的创建者可以通过从URL中提取域并将域放在其自己的列中并为该列编制索引来帮助您。然后你可以进行相等比较,WHERE DOMAIN ='somedomain',并且可以非常快速有效地找到该值。