มหากาพย์แห่งการ Join
11 May 2025 16:20
Written by: Yosapol Jitrak
เล่าถึง Project A ต่อจากบทความก่อนหน้านี้ Wrong Purpose Destructuring Don’t use ternary like this
ปกติเราใช้ SQL ก็จะมีการ Join ในการ Query ถือว่าเป็นเรื่องปกติถูกไหมครับ เคยเจอการ Join เยอะสุดกันเท่าไหร่ ลองมาดูที่กันครับว่าการ Join มันจะมากได้ขนาดไหนกันเชียว โดยใน Code นี้จะใช้ Prisma นะครับ
1 async getDetails(uuid: string) {
2 const result = await this.prismaService.tableA.findUnique({
3 where: { uuid },
4 include: {
5 TableB: true,
6 TableC: {
7 include: {
8 TableD: {
9 include: {
10 TableE: true,
11 },
12 },
13 TableF: true,
14 TableG: true,
15 TableH: true,
16 TableI: true,
17 },
18 },
19 TableJ: true,
20 TableK: {
21 orderBy: {
22 id: 'desc',
23 },
24 include: {
25 TableL: {
26 include: {
27 TableM: true
28 }
29 },
30 TableN: true,
31 },
32 },
33 TableO: {
34 orderBy: {
35 fieldA: 'desc',
36 },
37 include: {
38 TableP: {
39 include: {
40 TableQ: true,
41 TableR: true,
42 },
43 },
44 TableS: {
45 include: {
46 TableT: {
47 include: {
48 TableU: {
49 include: {
50 TableV: true,
51 },
52 },
53 },
54 },
55 },
56 },
57 TableW: {
58 include: {
59 TableX: {
60 include: {
61 TableY: {
62 include: {
63 TableZ: true,
64 },
65 },
66 },
67 },
68 },
69 },
70 TableAA: {
71 include: {
72 TableAB: {
73 include: {
74 TableAC: {
75 include: {
76 TableAD: true,
77 },
78 },
79 },
80 },
81 },
82 },
83 },
84 },
85 TableAE: true,
86 TableAF: {
87 include: {
88 TableAG: {
89 include: {
90 TableAH: true,
91 },
92 },
93 },
94 },
95 TableAI: {
96 include: {
97 TableAJ: true,
98 },
99 },
100 TableAK: {
101 include: {
102 TableAL: true,
103 },
104 },
105 TableAM: {
106 include: {
107 TableAN: true,
108 TableAO: {
109 include: {
110 TableAP: true,
111 },
112 },
113 },
114 },
115 TableAQ: {
116 include: {
117 TableAR: true,
118 },
119 },
120 TableAS: {
121 include: {
122 TableAT: true,
123 },
124 },
125 TableAU: {
126 include: {
127 TableAV: true,
128 TableAW: true,
129 },
130 },
131 TableAX: {
132 include: {
133 TableAY: true,
134 },
135 },
136 TableAZ: true,
137 TableBA: {
138 include: {
139 TableBB: true,
140 },
141 },
142 TableBC: {
143 include: {
144 TableBD: true,
145 },
146 },
147 },
148 });
149 if (!result) {
150 throw new Error('Data not found');
151 }
152 return result;
153}
สำหรับคนไม่เคยใช้ Prisma คำสั่ง include เป็นการ Join ใน Prisma ครับ ชื่อ Table ถึงขั้นตัวอักษรภาษาอังกฤษ 26 ตัวอักษรไม่พอ (อันนี้ผมใช้ AI ช่วยเปลี่ยนชื่อให้นะครับ เดี๋ยวจะหาว่าผมเอาข้อมูลภายในมาเผยแพร่)
Project A มีการบ่นเรื่องความช้าในการดึงข้อมูล ดูจากการ Join แล้วก็ไม่ได้รู้สึกแปลกใจเลย ซึ่่งใน Project ไม่ได้มี Join แบบนี้ที่เดียว มีเต็ม Project ซึ่งถ้าจะ Join กันขนาดนี้ คงต้องไปคุย และเริ่มออกแบบ Database กันใหม่เลยครับ เราทำ Normalization กันมากไปหรือเปล่า หรือเราควรไปใช้ NoSQL ไหม ผมไม่ได้บอกว่า NoSQL จะแก้ปัญหานี้ได้นะครับ ตราบใดที่ออกแบบมาไม่ดี ก็เละได้เหมือนกัน
ที่เขียนมานี้ไม่ได้ Blame นะครับ แต่อยากจะให้เห็นว่าการออกแบบสำคัญมากสำหรับการเขียน Program
Project A ยังไม่จบเท่านี้นะครับ ยังมีภาคต่ออีก ไว้จะมาเขียนต่อในบทความหน้าครับ