0번글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1번글 삽입(0번 글의 답변)
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
(부모 parentwid 글의 threadno + 1)
(threadlevel은 부모 threadlevel + 1)
(0 -> 1번 글 순서로 화면에 표시)
2번 글 삽입(0번 글의 답변)
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 2
(부모도 같고, 같은 레벨의 글의 threadno + 1)
(0 -> 1-> 2번 글 화면표시)
3번 글 삽입(새로운 루트)
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 2
3
3
0
0
0
4번 글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 2
3
3
0
0
0
4
0
0 1
3 (부모도 같고, 같은 레벨의 글의 threadno + 1)
5번 글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 2
3
3
0
0
0
4
0
0 1
3
5
3
3 1
1 (부모 parentwid 글의 threadno + 1)
6번 글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 2
3
3
0
0
0
4
0
0 1 4 (
3, 3 이상의 레코드들의 threadno ++ 시킵니다. )
5
3
3 1
1
6
0
2
2
3 (부모도 같고, 같은 레벨의 글 : 없음)
(
자신에게 할당될 threadno 구하기 : 부모의 threadno + 1 = 3)
(3 이상의 레코드들의 threadno ++ 시킵니다.)
(자신의
threadno = 3 할당)
7번 글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 2
3
3
0
0
0
4
0
0 1 4
5
3
3 1
1
6
0
2
2
3
7 3
5
2 2
(부모도 같고, 같은 레벨의 글 : 없음)
(
자신에게 할당될 threadno 구하기 : 부모의 threadno + 1 = 2)
(
자신의 threadno = 2 할당)
8번 글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 3 (
2, 2 이상의 레코드들의 threadno ++ 시킵니다.)
3
3
0
0
0
4
0
0 1
5 (4, 2 이상의 레코드들의 threadno ++ 시킵니다.)
5
3
3 1
1
6
0
2
2 4
(3, 2 이상의 레코드들의 threadno ++ 시킵니다.)
7 3
5
2 2
8 0 1
2
2 (부모도 같고, 같은 레벨의 글 : 없음)
(자신에게 할당될 threadno 구하기 : 부모의 threadno + 1 = 2)
(
2 이상의 레코드들의 threadno ++ 시킵니다.)
(자신의 threadno = 2 할당)
10번 글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 3
3
3
0
0
0
4
0
0 1
6 (5, 5 이상의 레코드들의 threadno ++ 시킵니다)
5
3
3 1
1
6
0
2
2 4
7 3
5
2 2
8 0 1
2
2
10
0 6
3 5
(부모도 같고, 같은 레벨의 글 : 없음)
(
자신에게 할당될 threadno 구하기 : 부모의 threadno + 1 = 5)
(5 이상의 레코드들의
threadno ++ 시킵니다.)
(
자신의 threadno = 5 할당)
11번 글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 3
3
3
0
0
0
4
0
0 1 7 (
6, 6 이상의 레코드들의 threadno ++ 시킵니다)
5
3
3 1
1
6
0
2
2 4
7 3
5
2 2
8 0 1
2
2
10
0 6
3 5
11
0
10
4 6 (
부모도 같고, 같은 레벨의 글 : 없음)
(
자신에게 할당될 threadno 구하기 : 부모의 threadno + 1 = 6)
(
6 이상의 레코드들의 threadno ++ 시킵니다.)
(
자신의 threadno = 6 할당)
13번 글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 4
(3, 3 이상의 레코드들의 threadno ++ 시킵니다)
3
3
0
0
0
4
0
0 1 8 (
7, 3 이상의 레코드들의 threadno ++ 시킵니다)
5
3
3 1
1
6
0
2
2 5 (
4, 3 이상의 레코드들의 threadno ++ 시킵니다)
7 3
5
2 2
8 0 1
2
2
10
0 6
3 6
(5, 3 이상의 레코드들의 threadno ++ 시킵니다)
11
0
10
4 7
(6, 3 이상의 레코드들의 threadno ++ 시킵니다)
13
0
8
3
3
(
부모도 같고, 같은 레벨의 글 : 없음)
(
자신에게 할당될 threadno 구하기 : 부모의 threadno + 1 = 3)
(
3 이상의 레코드들의 threadno ++ 시킵니다.)
(
자신의 threadno = 3 할당)
14번 글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 4
3
3
0
0
0
4
0
0 1 9
(8, 8 이상의 레코드들의 threadno ++ 시킵니다)
5
3
3 1
1
6
0
2
2 5
7 3
5
2 2
8 0 1
2
2
10
0 6
3 6
11
0
10
4 7
13
0
8
3
3
14
0
2
2
8
(
부모도 같고, 같은 레벨의 글 : 6번 글)
(
6번 글의 (재귀) 차일드 중 가장 threadno 가 큰 것: 11번 글의 7
(
자신에게 할당될 threadno 구하기 : 7 + 1 = 8)
(
8 이상의 레코드들의 threadno ++ 시킵니다.)
(
자신의 threadno = 8 할당
)
15번 글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 5 (
4, 4 이상의 레코드들의 threadno ++ 시킵니다)
3
3
0
0
0
4
0
0 1 10
(9, 4 이상의 레코드들의 threadno ++ 시킵니다)
5
3
3 1
1
6
0
2
2 6
(5, 4 이상의 레코드들의 threadno ++ 시킵니다)
7 3
5
2 2
8 0 1
2
2
10
0 6
3 7 (
6, 4 이상의 레코드들의 threadno ++ 시킵니다)
11
0
10
4 8 (
7, 4 이상의 레코드들의 threadno ++ 시킵니다)
13
0
8
3
3
14
0
2
2 9
(8, 4 이상의 레코드들의 threadno ++ 시킵니다)
15
0 1
2
4
(
부모도 같고, 같은 레벨의 글 : 8번 글)
(
8번 글의 (재귀) 차일드 중 가장 threadno가 큰 것: 13번 글의 3
(
자신에게 할당될 threadno 구하기 : 3 + 1 = 4)
(4 이상의 레코드들의
threadno ++ 시킵니다.)
(자신의 threadno = 4
할당)
16번 글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 6
(5, 4 이상의 레코드들의 threadno ++ 시킵니다)
3
3
0
0
0
4
0
0 1 11
(10, 4 이상의 레코드들의 threadno ++ 시킵니다)
5
3
3 1
1
6
0
2
2 7
(6, 4 이상의 레코드들의 threadno ++ 시킵니다)
7 3
5
2 2
8 0 1
2
2
10
0 6
3 8
(7, 4 이상의 레코드들의 threadno ++ 시킵니다)
11
0
10
4 9
(8, 4 이상의 레코드들의 threadno ++ 시킵니다)
13
0
8
3
3
14
0
2
2 10
(9, 4 이상의 레코드들의 threadno ++ 시킵니다)
15
0 1
2 5
(4, 4 이상의 레코드들의 threadno ++ 시킵니다)
16
0
13
4
4
(
부모도 같고, 같은 레벨의 글 : 없음)
(
자신에게 할당될 threadno 구하기 : 부모의 threadno + 1 = 4)
(
4 이상의 레코드들의 threadno ++ 시킵니다.)
(
자신의 threadno = 4 할당)
17번 글 삽입
rootwid parentwid threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 7 (
6, 6 이상의 레코드들의 threadno ++ 시킵니다)
3
3
0
0
0
4
0
0 1 12
(11, 6 이상의 레코드들의 threadno ++ 시킵니다)
5
3
3 1
1
6
0
2
2 8 (
7, 6 이상의 레코드들의 threadno ++ 시킵니다)
7 3
5
2 2
8 0 1
2
2
10
0 6
3 9
(8, 6 이상의 레코드들의 threadno ++ 시킵니다)
11
0
10
4 10
(9, 6 이상의 레코드들의 threadno ++ 시킵니다)
13
0
8
3
3
14
0
2
2 11
(10, 6 이상의 레코드들의 threadno ++ 시킵니다)
15
0 1
2 5
16
0
13
4
4
17
0
15
3
6
(
부모도 같고, 같은 레벨의 글 : 없음)
(
자신에게 할당될 threadno 구하기 : 부모의 threadno + 1 = 6)
(
6 이상의 레코드들의 threadno ++ 시킵니다.)
(
자신의 threadno = 6 할당)
18번 글 삽입
rootwid parentwid
threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 8 (
7, 7 이상의 레코드들의 threadno ++ 시킵니다)
3
3
0
0
0
4
0
0 1 13
(12, 7 이상의 레코드들의 threadno ++ 시킵니다)
5
3
3 1
1
6
0
2
2 9
(8 , 7 이상의 레코드들의 threadno ++ 시킵니다)
7 3
5
2 2
8 0 1
2
2
10
0 6
3 10
(9, 7 이상의 레코드들의 threadno ++ 시킵니다)
11
0
10
4 11
(10 , 7 이상의 레코드들의 threadno ++ 시킵니다)
13
0
8
3
3
14
0
2
2 12
(11, 7 이상의 레코드들의 threadno ++ 시킵니다)
15
0 1
2 5
16
0
13
4
4
17
0
15
3
6
18
0
1
2
7
(
부모도 같고, 같은 레벨의 글 : max( 8, 15 )번 글)
(
15번 글의 (재귀) 차일드 중 가장 threadno가 큰 것:17번 글의 6
(
자신에게 할당될 threadno 구하기 : 6 + 1 = 7)
(
7 이상의 레코드들의 threadno ++ 시킵니다.)
(
자신의 threadno = 7 할당
)
자. 이제 되었습니다. 0번에서 18번까지의 순으로 해서 글을 삽입할 때마다 4가지 변수 값들이 어떻게 변했는 지를
보았습니다. 이제 위와 같이 구성된 것을 어떻게 단 한 번의 SELECT 구문으로 글을 나열할 수 있는지를 보이겠습니다.
------------- 최종 DB 결과 -------------------------------
rootwid parentwid
threadlevel threadno
0
0 0
0
0
1
0
0 1 1
2 0
0
1 8
3
3
0
0
0
4
0
0 1 13
5
3
3 1
1
6
0
2
2 9
7 3
5
2 2
8 0 1
2
2
10
0 6
3 10
11
0
10
4 11
13
0
8
3
3
14
0
2
2 12
15
0 1
2 5
16
0
13
4
4
17
0
15
3
6
18
0
1
2
7
-------- SELECT 시에 ORDER BY rootwid DESC 적용하게 되면 다음과 같이
정렬
rootwid parentwid
threadlevel threadno
3
3
0
0
0
5
3
3 1
1
7 3
5
2 2
0
0 0
0
0
1
0
0 1 1
2 0
0
1 8
4
0
0 1 13
6
0
2
2 9
8 0 1
2
2
10
0 6
3 10
11
0
10
4 11
13
0
8
3
3
14
0
2
2 12
15
0 1
2 5
16
0
13
4
4
17
0
15
3
6
18
0
1
2
7
-------- 위의 정렬 옵션에 더해서 ORDER BY threadno를 적용하게 되면.
rootwid parentwid
threadlevel threadno
3
3
0
0
0
5
3
3 1
1
7 3
5
2 2
0
0 0
0
0
1
0
0 1 1
8 0 1
2
2
13
0
8
3
3
16
0
13
4
4
15
0 1
2 5
17
0
15
3
6
18
0
1
2
7
2 0
0
1 8
6
0
2
2 9
10
0 6
3 10
11
0
10
4 11
14
0
2
2 12
4
0
0 1 13
------- 단 한 번의 SELECT ORDER BY rootwid DESC, threadno
으로 리스트를
구해오고, 화면에 출력 시에는 threadlevel로 들여쓰기를 하게 되면
최종적으로 다음과 같이
출력이 됩니다.
3
5
7
0
1
8
13
16
15
17
18
2
6
10
11
14
4
자, 위와 같이 가져오게 되었습니다. 이걸로, 정확한 스레드 순서를 보장하게 되었습니다. 이제 "2번째 문제"를 해결해 보겠습니다.
위와 같이 SELECT로 가져올 수 있게 되면, 다음과 같은 SELECT를 통해서 DB에서 한 페이지 분량만큼의 데이터만 가져올 수
있습니다.
SELECT TOP 10 *
FROM ( SELECT TOP 100 * FROM TestBoard ORDER BY
rootwid DESC, threadno )
FIRSTTABLE
ORDER BY rootwid, threadno DESC;
우선, 100개의 게시물을 역순으로 선택한 다음, 최상위 10개를 선택해 옵니다. Devpia 식의 경우에는 내부의
SELECT TOP 100을 해서 모든 데이터를 가져오게 되는데, 위의 쿼리에서는 DB 내부에서 첫 번째 결과 ResultSet에서
또다시 최상위 게시물 10개만을 선택하여 반환하게 되는 것입니다.
즉, 단 10개의 게시물 데이터만이 DB 서버에서 웹 서버로 반환되는 것이죠. 이로써, "2번째 문제" 도 해결되었습니다.