MYSQL的關鍵詞 :
SQL_CALC_FOUND_ROWS
查看手冊後發現此關鍵詞的作用是在查詢時統計滿足過濾條件後的結果的總數(不受 Limit 的限制)
例如:
假設滿足條件的有1000條,這裡返回10條。
立即使用
則返回的 rowcount 為1000;
這樣節省了SELECT count(*) AS rowcount的重復查詢,可以節省比較可觀的時間。
以下是該放在在游味中的應用:
function mail_list_sent( $uid, $start ) {
// 注意SQL_CALC_FOUND_ROWS uid之間沒有逗號
$query = "SELECT SQL_CALC_FOUND_ROWS uid, real_name, current_city, msg_uid, sender_flag, ".
"msg_title, msg_content FROM " . TT_DBTABLEPRE . "mailbox as mb1, " . TT_DBTABLEPRE .
"user as usr1 WHERE mb1.sender_id=usr1.uid AND mb1.sender_id=$uid AND sender_flag > 0 LIMIT $start, " . TT_PAGESIZE;
$mails = $this->db->fetch_all( $query );
//查詢SELECT中滿足條件的行數,與LIMIT子句無關
$max_count = $this->db->fetch_first( "SELECT found_rows() AS rowcount" );
$tmp['state_code'] = 200;
$tmp['info'] = "OK";
$tmp['list'] = $mails;
$data = json_encode( $tmp );
return $data;
}