简体   繁体   English

如何优化分页查询?

[英]How to optimize the pagination query?

This is completely optimization question, I have a pagination query like that 这是完全优化的问题,我有这样的分页查询

$this->paginate    = array(
        'fields'    =>  array(
                        'DISTINCT Contact.contact_id',
                        'Contact.first_name',
                        'Contact.last_name',
                        'Contact.email',
                        'Contact.created',
                        'ContactGroup.name',
                      ),  
        'conditions' => array(
                        $this->conditions,
                        'ContactsContactGroup.contact_group_id'=>$viewList,                        
                        isset($keywordQuery)?$keywordQuery:"",
                      ),      
        'limit'      => 5,
         'group'     => array('Contact.contact_id')
                      );
$data = $this->paginate('ContactsContactGroup');
$data = $this->paginate('ContactsContactGroup');

this query is called in every if and else statement, I have four conditions of if and else , and in all conditions the above piece of code is written. 在每个if和else语句中都将调用此查询,我具有ifelse四个条件,并且在所有条件下都编写了以上代码。

I want to optimize, and avoid the big line of code in every condition, how can I optimize it, any answer will be appreciated. 我想进行优化,并在每种情况下都避免使用大量代码,我该如何优化它,任何答案将不胜感激。

You can use the another method in your class that will return the array instead of posting it everywhere. 您可以在类中使用另一个方法,该方法将返回数组,而不是将其发布到任何地方。 Add the following code 添加以下代码

private function getPaginate($viewList, $keywordQuery) {
  $this->paginate = array( 'fields' => array( 'DISTINCT Contact.contact_id', 'Contact.first_name', 'Contact.last_name', 'Contact.email', 'Contact.created', 'ContactGroup.name', ), 'conditions' => array( $this->conditions, 'ContactsContactGroup.contact_group_id'=>$viewList, isset($keywordQuery)?$keywordQuery:"", ), 'limit' => 5, 'group' => array('Contact.contact_id') );

  //optional: return the result
  return $this->paginate;
}

and then you can use 然后你可以使用

$this->getPaginate( $viewlist, $keywordQuery );
$data = $this->paginate('ContactsContactGroup'); 
$data = $this->paginate('ContactsContactGroup');

What i use as regular practice on search page i keep adding condition in if and else and concatenate it at the end to the main query. 我经常在搜索页面上使用的是我不断在if和else中添加条件并将其连接到主要查询的末尾的条件。 In your condition i think we can do like below: 在您的情况下,我认为我们可以做到以下几点:

//here is your if and else statement
$condition = array();
if(someconditions) {
 $condition['user'] = 'suresh'; //your conditions
else if(some conditions)
 $condition['email'] ='abc@gmail.com';

//And than add it to main query 
$this->paginate    = array(
        'fields'    =>  array(
                        'DISTINCT Contact.contact_id',
                        'Contact.first_name',
                        'Contact.last_name',
                        'Contact.email',
                        'Contact.created',
                        'ContactGroup.name',
                      ),  
        'conditions' => array(
                        $condition
                        'ContactsContactGroup.contact_group_id'=>$viewList,                        
                        isset($keywordQuery)?$keywordQuery:"",
                      ),      
        'limit'      => 5,
         'group'     => array('Contact.contact_id')
                      );
$data = $this->paginate('ContactsContactGroup');
$data = $this->paginate('ContactsContactGroup');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM