같은 sql의 다른 검색값 속도
본문
<code>
$cnt_sql_1 = "select sum(cnt) from TB_google_cnt where domain = '$url' and wdate = '$today' order by no desc";
$cnt_res_1 = mysql_query($cnt_sql_1) or die($cnt_sql_1);
$cnt_row_1 = mysql_fetch_array($cnt_res_1);
$cnt_sql_2 = "select sum(cnt) from TB_google_cnt where domain = '$url' and wdate = '$day_1' order by no desc";
$cnt_res_2 = mysql_query($cnt_sql_2) or die($cnt_sql_2);
$cnt_row_2 = mysql_fetch_array($cnt_res_2);
$cnt_sql_3 = "select sum(cnt) from TB_google_cnt where domain = '$url' and wdate = '$day_2' order by no desc";
$cnt_res_3 = mysql_query($cnt_sql_3) or die($cnt_sql_3);
$cnt_row_3 = mysql_fetch_array($cnt_res_3);
$cnt_sql_4 = "select sum(cnt) from TB_google_cnt where domain = '$url' and wdate = '$day_3' order by no desc";
$cnt_res_4 = mysql_query($cnt_sql_4) or die($cnt_sql_4);
$cnt_row_4 = mysql_fetch_array($cnt_res_4);
$cnt_sql_5 = "select sum(cnt) from TB_google_cnt where domain = '$url' and wdate = '$day_4' order by no desc";
$cnt_res_5 = mysql_query($cnt_sql_5) or die($cnt_sql_5);
$cnt_row_5 = mysql_fetch_array($cnt_res_5);
$cnt_sql_6 = "select sum(cnt) from TB_google_cnt where domain = '$url' and wdate = '$day_5' order by no desc";
$cnt_res_6 = mysql_query($cnt_sql_6) or die($cnt_sql_6);
$cnt_row_6 = mysql_fetch_array($cnt_res_6);
$cnt_sql_7 = "select sum(cnt) from TB_google_cnt where domain = '$url' and wdate = '$day_6' order by no desc";
$cnt_res_7 = mysql_query($cnt_sql_7) or die($cnt_sql_7);
$cnt_row_7 = mysql_fetch_array($cnt_res_7);
</code>
같은 테이블에 날짜별(지난 1~ - 7일) 긁어오는 sql문인데 페이지 로딩이 너무 느려서 그런데
쉽게 할수 있는 방법 없을까요?
답변 1
SELECT sum(cnt) as cnt FROM TB_google_cnt WHERE wdate >=(CURDATE()-INTERVAL 7 DAY) group by DATE_FORMAT(wdate ,'%Y-%m-%d');
wdate 타입이 datetime일때 DATE_FORMAT를 이용하며 그렇지 않은경우 DATE_FORMAT 은 제외하셔도 됩니다.