Cakephp 페이지 매기기가 LIMIT을 사용하는 동안 총 행 수를 반환하기 위해 mysql의 SQL_CALC_FOUND_ROWS 기능을 활용하도록하려고합니다. 다행히 paginateCount(), paginate()의 이중 쿼리를 제거 할 수 있기를 바랍니다.mysql을 사용하여 cakephp 페이지 매기기 SQL_CALC_FOUND_ROWS
내 app_model.php에 넣고 한, 그것은 기본적으로 작동하지만, 더 잘 할 수있다. 누군가가 paginate/paginateCount를 재정의하는 방법을 알아내어 1 SQL stmt 만 실행하도록 도와 줄 수 있습니까?
/**
* 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}`.*";
Q : paginate()에 사용 된 SAME 한도는 어떻게 받습니까?
$options['limit'] = 1; // ideally, should return same rows as in paginate()
Q : 추가 쿼리 없이도 쿼리 결과가 어떻게해서 페이지 매김되도록 할 수 있습니까?
$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);
Q : 우리가 어떻게 든) (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 초 이상 0.004 초). – Oddman