5년만에 물어봅니다
본문
<a href="../bbs/board.php?bo_table=test&sca=&sfl=wr_datetime&stx=<?php $today = date("Y-m-d",strtotime("+0 day")); echo $today; ?>">
<font color="red">오늘:</font>
<?php $sql_common ="select sum(wr_39) as tot from $write_table where wr_datetime between";
$sql = "$sql_common '" . date("Y-m-d") . "' and '" . date("Y-m-d") . " 23:59:59'";
$result = sql_fetch($sql);
echo number_format($result['tot']);
?></a>
어제:<a href="../bbs/board.php?bo_table=order&sca=&sfl=wr_datetime&stx=<?php $yesterday = date("Y-m-d",strtotime("-1 day")); echo $yesterday;?>">
<?php $sql = "$sql_common '" . date("Y-m-d",strtotime("-1 day")) . "' and '" . date("Y-m-d",strtotime("-1 day")) . " 23:59:59'"; $result = sql_fetch($sql); echo number_format($result['tot']); ?>
</a>
게시판 카테고리 위에 넣었는데 엄청 느려졌습니다, 글은 3만개되구요,
참고로 다른 도메인의 디비를 새 도메인의 디비에 꾸겨넣었습니다, 새 도메인은 php8.2버전입니다,
이 코드없으면 빠른데 이거 넣으면 많이 느립니다,
!-->
답변 4
당연히 글 누적갯수에 따른 쿼리도 느려집니다.
검색 대상을 전체글이 아닌 board_new 새글 정도나 기간을 설정해서 가져와야할것 같네요.
<?php
$today = date("Y-m-d");
$yesterday = date("Y-m-d", strtotime("-1 day"));
$sql ="select sum(wr_39) as tot from $write_table where left(wr_datetime,10) ='{$today}'";
$result = sql_fetch($sql);
?>
<a href="../bbs/board.php?bo_table=test&sca=&sfl=wr_datetime&stx=<?php echo $today ?>">
<font color="red">오늘:</font><?php echo number_format($result['tot']); ?>
</a>
<?php
$sql ="select sum(wr_39) as tot from $write_table where left(wr_datetime,10) ='{$yesterday}'";
$result = sql_fetch($sql);
?>
어제:<a href="../bbs/board.php?bo_table=order&sca=&sfl=wr_datetime&stx=<?php echo $yesterday ?>">
<?php echo number_format($result['tot']); ?>
</a>
g5_board_new 에서 세서 가져오는 게 더 빠르지 않을까요?
<?php
session_start();
// 날짜
$today = date("Y-m-d");
$yesterday = date("Y-m-d", strtotime("-1 day"));
// 오늘 캐시
if (!isset($_SESSION['cache']['today']) || $_SESSION['cache']['today']['expire'] < time()) {
$sql = "SELECT SUM(wr_39) AS tot FROM {$write_table} WHERE wr_datetime BETWEEN '{$today} 00:00:00' AND '{$today} 23:59:59'";
$result = sql_fetch($sql);
$_SESSION['cache']['today'] = [
'data' => number_format($result['tot']),
'expire' => time() + 3600 // 1시간 후 만료
];
}
// 어제 캐시
if (!isset($_SESSION['cache']['yesterday']) || $_SESSION['cache']['yesterday']['expire'] < time()) {
$sql = "SELECT SUM(wr_39) AS tot FROM {$write_table} WHERE wr_datetime BETWEEN '{$yesterday} 00:00:00' AND '{$yesterday} 23:59:59'";
$result = sql_fetch($sql);
$_SESSION['cache']['yesterday'] = [
'data' => number_format($result['tot']),
'expire' => time() + 3600 // 1시간 후 만료
];
}
// 캐시 사용
$result_today = $_SESSION['cache']['today']['data'];
$result_yesterday = $_SESSION['cache']['yesterday']['data'];
?>
<a href="../bbs/board.php?bo_table=test&sfl=wr_datetime&stx=<?php echo $today; ?>">
<font color="red">오늘:</font> <?php echo $result_today; ?>
</a>
<a href="../bbs/board.php?bo_table=order&sfl=wr_datetime&stx=<?php echo $yesterday; ?>">
어제: <?php echo $result_yesterday; ?>
</a>
답변을 작성하시기 전에 로그인 해주세요.