CakePHP如何更有效地检索具有条件的HABTM数据
我在CakePHP 2.x中有下面的MySQL查询,它可以工作,但是需要很长时间,如果我删除“连接”,它可以很快工作,但是'Tag.tags LIKE'=>'%'。$busqueda.'%'部分不能工作,我有没有办法让它工作得更快?更好的说法是:如何删除连接并仍然能够在标记表上搜索? 它是内联网上搜索引擎的一部分 先谢谢你CakePHP如何更有效地检索具有条件的HABTM数据,php,mysql,cakephp,Php,Mysql,Cakephp,我在CakePHP 2.x中有下面的MySQL查询,它可以工作,但是需要很长时间,如果我删除“连接”,它可以很快工作,但是'Tag.tags LIKE'=>'%'。$busqueda.'%'部分不能工作,我有没有办法让它工作得更快?更好的说法是:如何删除连接并仍然能够在标记表上搜索? 它是内联网上搜索引擎的一部分 先谢谢你 $year = $this->request->query['year']; $years = "year(`Saliente`.`fecha_rem`) = "
$year = $this->request->query['year'];
$years = "year(`Saliente`.`fecha_rem`) = ".$year;
$this->loadModel('Saliente');
$this->Saliente->recursive = -1;
$busqueda_c = mysql_escape_string($busqueda);
$this->Saliente->bindModel(
array('hasAndBelongsToMany' => array(
'Tag' => array(
'className' => 'Tag',
'jointable' => 'salientes_tags',
'foreignKey' => 'saliente_id',
'associationForeignKey' => 'tag_id'
)
)
)
);
$options = array('joins' => array(
array('table' => 'salientes_tags',
'alias' => 'SalientesTag',
'type' => 'left',
'conditions' => array(
'Saliente.id = SalientesTag.saliente_id'
)
),
array('table' => 'tags',
'alias' => 'Tag',
'type' => 'left',
'conditions' => array(
'SalientesTag.tag_id = Tag.id'
)
)
), 'maxLimit' => 300, 'limit' => 10, 'contain' => array('Tag' => array('fields' => array('Tag.tags') )),
'fields' => array('Saliente.id', 'Saliente.usuario_id', 'Saliente.codigo', 'Saliente.asunto', 'Saliente.observacion',
'MATCH(Saliente.asunto, Saliente.observacion) AGAINST("+'.$busqueda_c.'*") AS relevance'), 'countField' => 'DISTINCT Saliente.id',
'conditions' => array($years, 'OR' => array("MATCH(Saliente.asunto, Saliente.observacion) AGAINST('+".$busqueda_c."*')",
'Saliente.codigo LIKE' => '%'.$busqueda.'%', 'Tag.tags LIKE' => '%'.$busqueda.'%' )),
'order' => 'relevance DESC,
fecha_rem DESC', 'group' => 'Saliente.id');
$this->Paginator->settings = $options;
$resultado = $this->Paginator->paginate('Saliente');
$this->set('resultado', $resultado);
以SQL查询为例。用explain()检查它,然后在需要的地方设置索引。(). 但是你应该知道,在开始时使用%的搜索在默认情况下不会很快工作。谢谢你的回复,索引已经完成,问题确实是连接,我需要一种方法来删除连接,但仍然能够搜索标记。