在 http://www.modoer.org/beijing/item/list-8 的頁面中,會執行以下2個sql
SELECT s.sid,pid,catid,domain,name,avgsort,sort1,sort2,sort3,sort4,sort5,sort6,sort7,sort8,best,finer,pageviews,reviews,pictures,favorites,thumb,aid,map_lat,map_lng,c_tel,c_dz FROM modoer_subject s LEFT JOIN modoer_subject_shishang sf ON (s.sid = sf.sid) WHERE exists(SELECT 1 FROM modoer_subjectatt st WHERE s.sid=st.sid AND attid=173) AND exists(SELECT 1 FROM modoer_subjectatt st WHERE s.sid=st.sid AND attid=4) ORDER BY finer DESC LIMIT 0, 20
SELECT COUNT(*) FROM modoer_subject s WHERE exists(SELECT 1 FROM modoer_subjectatt st WHERE s.sid=st.sid AND attid=173) AND exists(SELECT 1 FROM modoer_subjectatt st WHERE s.sid=st.sid AND attid=4)
如果數據量不大的話,還湊活,如果數據量大的話呢?以本人的數據為例,分別是subjectatt數據量和subject數據量
sql運行結果如圖:
僅僅一個查詢數據量的sql花了44.16秒,網站根本打不開,難道技術沒有測試沒有優化嗎?復合語句用起來很爽但是效果很差
經本人暫時優化,subject_class.php中改掉以下代碼,性能大大提升
if($atts) {
$attlist = array_values($atts);
$num = count($attlist);
if($num>0){
$or = '';
$sql ='select GROUP_CONCAT(sid) sids from(
select count(sid) count,sid from (
select sid,attid from modoer_subjectatt where ';
foreach($attlist as $attid) {
//$this->db->where_exist("SELECT 1 FROM dbpre_subjectatt st WHERE s.sid=st.sid AND attid=$attid");
if($attid<1)continue;
$sql .= $or . ' attid='.$attid;
$or = ' or ';
}
$sql .= ') as temp
group by sid) as temp1
where count='.$num;
}
$query=$this->db->query($sql);$b=$query->fetch_array();$sids=$b['sids'];
//$where = ' s.sid in('.$sids.') ';
$this->db->where('s.sid', explode(',', $sids), '');
}else{
$where = '';
}
還有查詢數量的地方
if($atts) {
$attlist = array_values($atts);
$num = count($attlist);
if($num>0){
$or = '';
$sql ='select count(*) as nums from(
select count(sid) count from (
select sid,attid from modoer_subjectatt where ';
foreach($attlist as $attid) {
//$this->db->where_exist("SELECT 1 FROM dbpre_subjectatt st WHERE s.sid=st.sid AND attid=$attid");
if($attid<1)continue;
$sql .= $or . ' attid='.$attid;
$or = ' or ';
}
$sql .= ') as temp
group by sid) as temp1
where count='.$num;
}
}
希望有用到的朋友可以改下,也只是暫時解決辦法。另有問題請聯系qq 272164179