can any one help me out
iam running a query using doctrine.
i made indexing and foriegnkey reference for necessary tables
here is my query
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;
for executing query it is taking 8 sec .
How to make this query to run fast
Thanks in advance
Your LIKE comparison is actually a “contains-substring” search when you put wildcard book-ends on the %search-term%. The typical advantages of a sorted index are not available on searches like this (because your wildcard says to ignore the characters at the start of the value); and so every value in the table (in the relevant column) must be examined to see if it contains the substring.
If you searched the phone book WHERE SURNAME LIKE ‘%els%’ you would have to examine every surname, whereas if you did WHERE SURNAME LIKE ‘Nels%’ you could search through the -N- names only, excluding names that began with any other letter. The latter could use an index.
The creators of the database could help you out by extracting the domain from the url and placing the domain in its own column and indexing that column. Then you could do an equality comparison, WHERE DOMAIN = ‘somedomain’ and the value would be found very quickly and efficiently.