조회수 배치로 SQL 부하 줄이
조회수 기능 제거후 아래 코드로 txt 파일에 조회수 넣어두기
<?php
$bo_table = $_GET['bo_table'] ?? '';
$wr_id = $_GET['wr_id'] ?? '';
if (!$bo_table || !$wr_id) exit;
// 경로 설정
$dir = __DIR__ . "/data/viewcount";
if (!is_dir($dir)) mkdir($dir, 0777, true);
$file = "$dir/{$bo_table}_{$wr_id}.txt";
// 파일에 조회수 누적
if (file_exists($file)) {
$count = (int)file_get_contents($file);
file_put_contents($file, $count + 1);
} else {
file_put_contents($file, 1);
}
?>
batch_viewcount_update.php
<?php
include_once('./common.php'); // 그누보드 DB 연결을 위해 필요
$dir = __DIR__ . "/data/viewcount";
if (!is_dir($dir)) exit;
$files = glob("$dir/*.txt");
foreach ($files as $file) {
$filename = basename($file); // e.g. notice_123.txt
[$bo_table, $wr_id_with_ext] = explode('_', $filename);
$wr_id = (int)str_replace('.txt', '', $wr_id_with_ext);
$count = (int)file_get_contents($file);
if ($count > 0 && $bo_table && $wr_id) {
// 조회수 증가
sql_query("UPDATE {$g5['write_prefix']}{$bo_table} SET wr_hit = wr_hit + $count WHERE wr_id = '$wr_id'");
}
// 파일 삭제 또는 초기화
unlink($file);
}
크론탭으로 배치 파일 실행 (5분마다 실행)
*/5 * * * * /usr/bin/php /home/your_site/public_html/batch_viewcount_update.php
이런식으로해서 부하를 cpu사용량 300%에서 60%까지 줄였습니다. 조회수 말고도 다른것들도 튜닝을 하면서요.. 다들 화이팅하세요.
댓글 2개
유용한 정보네요 감사합니다.
감사합니다.
게시판 목록
그누보드5 팁자료실
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 공지 | 3년 전 | 4404 | ||
| 2634 | 6개월 전 | 609 | ||
| 2633 | 6개월 전 | 599 | ||
| 2632 |
|
6개월 전 | 508 | |
| 2631 | 6개월 전 | 570 | ||
| 2630 |
세르반데스
|
6개월 전 | 538 | |
| 2629 | 6개월 전 | 728 | ||
| 2628 | 6개월 전 | 412 | ||
| 2627 | 7개월 전 | 418 | ||
| 2626 |
이슈DEV
|
7개월 전 | 639 | |
| 2625 |
welcome
|
7개월 전 | 646 | |
| 2624 |
이슈DEV
|
7개월 전 | 449 | |
| 2623 | 7개월 전 | 407 | ||
| 2622 | 7개월 전 | 496 | ||
| 2621 | 7개월 전 | 353 | ||
| 2620 |
|
7개월 전 | 370 | |
| 2619 | 7개월 전 | 479 | ||
| 2618 | 7개월 전 | 466 | ||
| 2617 | 7개월 전 | 556 | ||
| 2616 | 7개월 전 | 678 | ||
| 2615 | 7개월 전 | 582 | ||
| 2614 | 8개월 전 | 411 | ||
| 2613 |
바닐라코드
|
8개월 전 | 731 | |
| 2612 | 8개월 전 | 594 | ||
| 2611 | 8개월 전 | 731 | ||
| 2610 | 8개월 전 | 969 | ||
| 2609 | 8개월 전 | 508 | ||
| 2608 | 8개월 전 | 654 | ||
| 2607 | 8개월 전 | 631 | ||
| 2606 | 8개월 전 | 577 | ||
| 2605 | 8개월 전 | 602 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기