zend-frameworkzend-dbzend-db-tablezend-paginator

Zend Paginator Limit Rows(Optimization)


This is my query:

class services extends Zend_Db_Table{

 function Get_Services($user_id){
    $DB = Zend_Db_Table_Abstract::getDefaultAdapter();

    $select = $DB->select()
                ->from(array('p' => 'phone_service'))
                ->join(array('u' => 'user_preferences'), 'u.phone_service_id = p.phone_service_id')
                ->where('u.user_preferences_name = ?', 'is_user_package_active')
                ->where('p.user_id = ?', $user_id);
           return $select;
    }
  }

This is my controller:

class ServicesController extends Zend_Controller_Action{
       $instance = new services();
       $select = $instance->Get_Services($user_id);

       $adapter   = new Zend_Paginator_Adapter_DbSelect($select);
       $paginator = new Zend_Paginator($adapter);

       $page=$this->_getParam('page',1);
       $paginator->setItemCountPerPage(10);
       $paginator->setCurrentPageNumber($page);
       $paginator->setPagerange(5);
 }

What I am doing is that first I query all rows and then do this:

 $adapter   = new Zend_Paginator_Adapter_DbSelect($select);
 $paginator = new Zend_Paginator($adapter);

My question is, how can I optimize it so that it queries only 10 rows from the result and not all of them?


Solution

  • With DbSelect adapter it does query only 10 rows. Profile your queries and inspect - you should see 2 selects AFAIR - one manipulated by ZF to obtain total count of rows and one to get the actual data with LIMIT and OFFSET.