我不是一个CakePHP或mySQL的专家,任何建议都很感激。
我设置了一个HABTM搜索基本上从CakeDC手动复制/粘贴/改变我的领域。
宝藏模型[相关]代码:
public $filterArgs = array(
array('name' => 'makers', 'type' => 'subquery', 'method' => 'findByMaker', 'field' => 'Treasure.id'));
public function findByMaker($data = array()) {
$this->MakersTreasure->Behaviors->attach('Containable', array('autoFields' => false));
$this->MakersTreasure->Behaviors->attach('Search.Searchable');
$query = $this->MakersTreasure->getQuery('all', array(
'conditions' => array("Maker.name LIKE '%" . $data['makers'] ."%'"),
'fields' => array('treasure_id'),
'contain' => array('Maker')
));
return $query;
}
带分页器的财务管理员:
$this->Paginator->settings['conditions'] = $this->Treasure->parseCriteria($this->Prg->parsedParams());
$this->set('treasures', $this->Paginator->paginate());
这个数据库包含了大约20000件珍宝和大约2000个制造者——对我来说,这并不是很大。
我查看了COUNT和SELECT(带限制)查询,它们运行得很慢。我想保险条款真的要付出代价。关于如何从CakePHP中修复这个缓慢的查询有什么想法吗?
最佳答案
啊,一路上我对mySQL有了更多的了解。答案(对我来说):
在连接表上索引FKs。我想当我了解更多关于mySQL的信息时,我应该知道这一点。
对我来说是:
use oc1;
alter table makers_treasures add index (maker_id);
alter table makers_treasures add index (treasure_id);
计数查询现在需要.12秒来运行,而不是超过3分钟。