我想讓CakePHP頁面利用mysql中的SQL_CALC_FOUND_ROWS功能來返回使用LIMIT時總行數。希望這可以消除paginateCount()和paginate()的雙重查詢。cakephp分頁使用mysql SQL_CALC_FOUND_ROWS
我已經把這個在我app_model.php,它基本上工作,但它可以做的更好。有人能幫我弄清楚如何重寫paginate/paginateCount,因此它只執行1個SQL語句嗎?
/**
* Overridden paginateCount method, to using SQL_CALC_FOUND_ROWS
*/
public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$options = array_merge(compact('conditions', 'recursive'), $extra);
$options['fields'] = "SQL_CALC_FOUND_ROWS `{$this->alias}`.*";
問:如何獲得paginate()中使用的SAME限制值?
$options['limit'] = 1; // ideally, should return same rows as in paginate()
問:我們可以以某種方式直接將查詢結果分頁,而不需要額外的查詢嗎?
$cache_results_from_paginateCount = $this->find('all', $options);
/*
* note: we must run the query to get the count, but it will be cached for multiple paginates, so add comment to query
*/
$found_rows = $this->query("/* do not cache for {$this->alias} */ SELECT FOUND_ROWS();");
$count = array_shift($found_rows[0][0]);
return $count;
}
/**
* Overridden paginate method
*/
public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {
$options = array_merge(compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'), $extra);
問:我們可以以某種方式直接從paginateCount()獲得$ cache_results_for_paginate嗎?
return $cache_results_from_paginateCount; // paginate in only 1 SQL stmt
return $this->find('all', $options); // ideally, we could skip this call entirely
}
我只是想在這裏補充一點,SQL_CALC_FOUND_ROWS往往比使用COUNT()和2個查詢慢得多。這是Mysql尋找數據的原因。特別是在大型數據集中,SQL_CALC_FOUND_ROWS解決方案可以花費數百萬次(100多萬秒,而在100萬條記錄中爲0.004秒)。 – Oddman 2013-11-22 14:03:19