큰 사이트에서는 SQL이 다른 결과를 주네요 ㅠ.ㅠ > 그누4 질문답변

그누4 질문답변

그누보드4 관련 질문은 QA 로 이전됩니다. QA 그누보드4 바로가기
기존 게시물은 열람만 가능합니다.

큰 사이트에서는 SQL이 다른 결과를 주네요 ㅠ.ㅠ 정보

큰 사이트에서는 SQL이 다른 결과를 주네요 ㅠ.ㅠ

본문

select n1.bo_table, n1.wr_parent
from
(select * from g4_board_new where mb_id != 'echo4me' and datediff(curdate(), bn_datetime) <= 3 ) as n1,
(select wr_id, bn_datetime from g4_board_new where wr_id = wr_parent and mb_id='echo4me' ) as n2
where n1.wr_parent <> n1.wr_id and n1.wr_parent = n2.wr_id
group by n1.bo_table, n1.wr_parent
order by n1.bn_datetime desc
 
테스트 사이트에서 할 때는 잘 되었는데 g4_board_new가 28만건 정도 되는 곳에서 했더니
결과가 이상 합니다. ㅠ..ㅠ
 
n1.wr_parent = n2.wr_id 이고 n2의 조건에 mb_id='echo4me'가 있으므로 echo4me가 올린 게시글만 나와야
정상인데... 다른 사람들의 글이 같이 나오고 있습니다.
 
왜 그런걸까요???? ㅠ..ㅠ
  • 복사

댓글 전체

조금전 알려주신 쿼리에요. 마이페이지... ㅠ..ㅠ
n1을 좀 수정했어요.
날짜가 최근일 이내의 것으로만 골라내고... 내꺼 아닌거 빼구(내가 내꺼에 덧글 단거 빼려구요...)

소스코드는 이곳에...
http://www.sir.co.kr/bbs/board.php?bo_table=g4_skin&wr_id=57091
select n1.bo_table, n1.wr_parent
from
(select * from g4_board_new where mb_id <> 'echo4me' and datediff(curdate(), bn_datetime) <= 3 and n1.wr_parent <> n1.wr_id) as n1,
(select wr_id, bn_datetime from g4_board_new where wr_id = wr_parent and mb_id='echo4me' ) as n2
where n1.wr_parent = n2.wr_id
group by n1.bo_table, n1.wr_parent
order by n1.bn_datetime desc
실행속도는 빨라졌는데.. 결과값이 이상한거는 여전하네요. ㅠ..ㅠ

$sql = " select
        n1.bo_table, n1.wr_parent
  from
    (select *
        from $g4[board_new_table] where mb_id <> '$member[mb_id]'   
        and datediff(curdate(), bn_datetime) <= $my_date
        and wr_parent <> wr_id
    ) as n1,
    (select wr_id, bn_datetime
        from $g4[board_new_table]
          where wr_id = wr_parent
            and mb_id='$member[mb_id]'
    ) as n2
  where
        n1.wr_parent = n2.wr_id
    group by n1.bo_table, n1.wr_parent
    order by n1.bn_datetime desc  ";
아래 쿼리를 이용해보세요.
참고로  bn_datetime 에도 인덱스를 걸어주셔야 할 것 같습니다.


select
    n1.bo_table, n1.wr_id, max(n2.wr_id) as comment_wr_id
from
    (select *
      from g4_board_new
      where wr_id = wr_parent
        and mb_id='echo4me'
    ) as n1,

    (select *
      from g4_board_new
      where mb_id <> 'echo4me'
        and wr_parent <> wr_id
        and datediff(curdate(), bn_datetime) <= 3
    ) as n2
where
    n1.wr_id = n2.wr_parent
group by
    n1.bo_table, n1.wr_id
order by
    n1.bn_datetime desc
코드를 바꿔서 했더니... 문제가 역시 있네요.

1. 내가 쓰고 답글이 없는 것이 나오구요
2. bn_datetime을 3일로 했는데 작년도 글도 나오구요.
3. comment_wr_id는 아래와는 값이 다르구요. ㅠ..ㅠ
//        $sql3 = " select max(wr_id) as max_cn from $g4[write_prefix]$row[bo_table] where wr_parent = '$row[wr_parent]' ";
//        $row3 = sql_fetch($sql3);
datediff(curdate(), bn_datetime) <= 3  ...
이부분도 좀 이상한거 같아요. 작년 오늘과 금년 오늘을 같이 보는 것인지... ㅠ..ㅠ

<?
include_once("./_common.php");

if (!defined("_GNUBOARD_")) exit; // 개별 페이지 접근 불가

$g4[title] = "my page";
include_once("$g4[path]/_head.php");
 
// 최근 $my_date일 사이에 코멘트가 있는 게시글만 검색한다.
if (! $my_date)
    $my_date = 3; // 최근

/*
$sql = " select
        n1.bo_table, n1.wr_parent
  from
    (select wr_parent, wr_id, bo_table, bn_datetime
        from $g4[board_new_table] where mb_id <> '$member[mb_id]'   
        and datediff(curdate(), bn_datetime) <= $my_date
        and wr_parent <> wr_id
        group by bo_table, wr_parent
        ) as n1,
    (select wr_parent, wr_id, bo_table, bn_datetime
        from $g4[board_new_table]
          where wr_id = wr_parent
            and mb_id='$member[mb_id]'
    ) as n2
  where
        n1.wr_parent = n2.wr_id
    group by n1.bo_table, n1.wr_parent
    order by n1.bn_datetime desc  ";
    */
$sql = " select
    n1.bo_table, n1.wr_id, max(n2.wr_id) as comment_wr_id
from
    (select *
      from g4_board_new
      where wr_id = wr_parent
        and mb_id='echo4me'
      group by bo_table, wr_id
    ) as n1,
    (select *
      from g4_board_new
      where mb_id <> 'echo4me'
        and wr_parent <> wr_id
        and datediff(curdate(), bn_datetime) <= 3
    ) as n2
where
    n1.wr_id = n2.wr_parent
group by
    n1.bo_table, n1.wr_id
order by
    n2.bn_datetime desc ";
$result = mysql_query($sql);
echo $sql;
$total_count = mysql_num_rows($result);
?>

<table width=100% cellpadding=0 cellspacing=0>

<tr><td>최근 <?=$my_date?> 일 동안 코멘트가 있는 <?=$member[mb_nick]?> 님의 게시글은 <b><?=$total_count?></b> 건 입니다.
</td></tr>

<tr><td height="10"></td></tr>

<tr><td>
<?//출력
for ($i=0; $row = sql_fetch_array($result); $i++){
?>
    <?
        $sql2 = " select wr_subject from $g4[write_prefix]$row[bo_table] where wr_id = '$row[wr_id]' ";
        $row2 = sql_fetch($sql2);
//        $sql3 = " select max(wr_id) as max_cn from $g4[write_prefix]$row[bo_table] where wr_parent = '$row[wr_parent]' ";
//        $row3 = sql_fetch($sql3);
        $sql4 = " select bo_subject from $g4[board_table] where bo_table = '$row[bo_table]' ";
        $row4 = sql_fetch($sql4);
    ?>
    <?=$row4[bo_subject]?>
    <a href='<?=$g4[bbs_path]?>/board.php?bo_table=<?=$row[bo_table]?>&wr_id=<?=$row[wr_id]?>#c_<?=$row[comment_wr_id]?>' onfocus='this.blur()'>
    <?=cut_str($row2[wr_subject], 60)?><br>
    </a>
<? } ?>

</td></tr>
</table>

<?
include_once("$g4[path]/_tail.php");
?>
수정했습니다.

n1.wr_id = n2.wr_parent ... 이것이 문제였더군요.

n1.wr_id = n2.wr_parent and n1.bo_table = n2.bo_table ... 같은 테이블에서 찾았어야징... ㅋㅋ
© SIRSOFT
현재 페이지 제일 처음으로