[英]“not exists” or “exists” query with pagination taking too much time to executing in cakephp
我嘗試使用“不存在”和“存在”命令來獲取記錄。
它與findAll ..的工作正常,但帶有分頁“ MAX_JOIN_SIZE”的問題
代碼:
$this->User->unbindModelAll();
$this->Booking->unbindModelAll();
$this->User->bindModel(array('hasMany' => array('UserImage'), 'hasOne' =>array('Booking','RentalGoal', 'Verification', 'AbcCheck')), false);
$cond_user = array(
'User.id NOT' => $a,
'User.is_hide' =>1, 'User.status' =>1,
'RentalGoal.looking_for' =>1,'RentalGoal.room_type <>' => null, 'RentalGoal.step1_completed' =>1, 'Verification.rent_gaol' => 1,
'OR'=>array(
array('not exists ' . '(select id from dev_bookings ' . 'where dev_bookings.user_id = ' . 'User.id AND dev_bookings.move_in =1 AND dev_bookings.move_out = 0 AND dev_bookings.rent_term = "m")'),
array('exists ' . '(select id from dev_user_images ' . 'where dev_user_images.user_id = ' . 'User.id AND dev_user_images.status =1 AND dev_user_images.default = 1)'),
false),);
可以正常使用>>
$new_arr = $this->User->find('all', array(
'conditions' => $cond_user,
'limit' =>400, 'recursive' => 2,
'order' => array('User.user_verification DESC'),
));
但不能與分頁一起使用
$this->paginate = array(
'conditions' => $cond_user,
'limit' =>100,
'recursive' =>2,
'order' => array('User.user_verification DESC'),
);
$new_arr = $this->paginate('User');
錯誤>
2014-07-04 14:38:51 Error: [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1104 The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
AppModel>
class AppModel extends Model {
function beforeFind($query)
{
$this->query('SET SQL_BIG_SELECTS=1');
}
}
怎么了? 我也使用,false進入綁定模型。
請幫忙
謝謝
MySQL根據“ max_join_size”的值確定查詢是否為“大選擇”。 如果查詢可能需要檢查的行數超過此數目,則將其視為“大選擇”。 使用“顯示變量”查看最大連接大小的值。
我認為,建立索引以及特別好的where子句可以防止發生此問題。
SQL_BIG_SELECTS用於防止用戶意外執行過大的查詢。 可以在mysql.cnf中將其設置為ON或在啟動時使用命令行選項。
您可以在my.cnf或服務器啟動時設置SQL_BIG_SELECTS。 也可以使用'SET SESSION SQL_BIG_SELECTS = 1'在會話的基礎上進行設置。
不是我能想到的。 我只是檢查您的查詢以確保您確實需要使用它。 我們的服務器默認情況下將其打開,並且max_join_size非常大。
設置最大連接大小SET GLOBAL max_join_size = 1844674407370955148795;
http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.