w

조회수 배치로 SQL 부하 줄이

· 2025-05-06 (화) 22:47:06 · 2548 · 2

조회수 기능 제거후 아래 코드로 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개

2025-05-08 (목) 12:05:30

유용한 정보네요 감사합니다.

감사합니다.

댓글을 작성하시려면 로그인이 필요합니다.

그누보드5 팁자료실

2,788건
+
제목 글쓴이 날짜 조회
25-06-24 조회 2,364
25-06-22 조회 2,557
25-06-21 조회 3,343
25-06-17 조회 2,426
25-06-17 조회 2,294
25-06-11 조회 2,910
25-06-02 조회 2,567
25-05-28 조회 2,430
25-05-25 조회 2,392
25-05-17 조회 2,447
25-05-17 조회 2,718
25-05-15 조회 2,365
25-05-15 조회 2,714
25-05-14 조회 2,234
25-05-13 조회 2,337
25-05-06 조회 2,554
25-05-06 조회 2,549
25-05-05 조회 2,347
25-05-02 조회 2,769
25-04-27 조회 2,383
25-04-25 조회 2,188
25-04-24 조회 2,212
25-04-23 조회 2,353
25-04-22 조회 2,425
25-04-20 조회 2,522
25-04-19 조회 2,727
25-04-13 조회 2,577
25-04-12 조회 2,332
25-04-10 조회 2,669
25-04-10 조회 2,524