-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnested_loop.html
More file actions
329 lines (297 loc) · 11 KB
/
Copy pathnested_loop.html
File metadata and controls
329 lines (297 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MySQL Nested Loop Join Visualization</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.container {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
.table-container {
width: 45%;
border: 1px solid #ddd;
padding: 10px;
border-radius: 5px;
}
.result-container {
width: 100%;
border: 1px solid #ddd;
padding: 10px;
border-radius: 5px;
margin-top: 20px;
min-height: 100px;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
.highlight {
background-color: #ffeb3b;
transition: background-color 0.3s;
}
.matched {
background-color: #a5d6a7;
transition: background-color 0.3s;
}
.controls {
margin: 20px 0;
}
button {
padding: 8px 16px;
margin-right: 10px;
cursor: pointer;
}
.arrow {
position: absolute;
width: 100px;
height: 2px;
background-color: red;
top: 50%;
left: 50%;
transform-origin: left center;
display: none;
}
.arrow::after {
content: '';
position: absolute;
right: 0;
top: -4px;
width: 0;
height: 0;
border-left: 8px solid red;
border-top: 5px solid transparent;
border-bottom: 5px solid transparent;
}
.explanation {
margin: 20px 0;
padding: 15px;
background-color: #f8f9fa;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>MySQL Nested Loop Join Visualization</h1>
<div class="explanation">
<h3>How Nested Loop Join Works:</h3>
<ol>
<li>For each row in the <strong>outer table</strong> (driving table):</li>
<li>Scan through all rows in the <strong>inner table</strong> (driven table)</li>
<li>If the join condition matches, combine the rows and add to results</li>
<li>Repeat until all outer table rows are processed</li>
</ol>
<p><strong>Performance Note:</strong> This algorithm is O(M*N) where M and N are the table sizes. Indexes on join columns can optimize this.</p>
</div>
<div class="controls">
<button id="startBtn">Start Animation</button>
<button id="resetBtn">Reset</button>
<button id="stepBtn">Step Through</button>
<div>
<label>Animation Speed: </label>
<input type="range" id="speedSlider" min="100" max="2000" value="500">
<span id="speedValue">500ms</span>
</div>
</div>
<div class="container">
<div class="table-container" id="outerTableContainer">
<h3>Outer Table (Driving Table) - Employees</h3>
<table id="outerTable">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>dept_id</th>
</tr>
</thead>
<tbody>
<tr data-id="1">
<td>1</td>
<td>Alice</td>
<td>101</td>
</tr>
<tr data-id="2">
<td>2</td>
<td>Bob</td>
<td>102</td>
</tr>
<tr data-id="3">
<td>3</td>
<td>Charlie</td>
<td>101</td>
</tr>
</tbody>
</table>
</div>
<div class="table-container" id="innerTableContainer">
<h3>Inner Table (Driven Table) - Departments</h3>
<table id="innerTable">
<thead>
<tr>
<th>dept_id</th>
<th>dept_name</th>
</tr>
</thead>
<tbody>
<tr data-id="101">
<td>101</td>
<td>Engineering</td>
</tr>
<tr data-id="102">
<td>102</td>
<td>Marketing</td>
</tr>
<tr data-id="103">
<td>103</td>
<td>Finance</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="result-container">
<h3>Join Result (employees JOIN departments ON dept_id)</h3>
<table id="resultTable">
<thead>
<tr>
<th>emp_id</th>
<th>emp_name</th>
<th>dept_id</th>
<th>dept_name</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div class="arrow" id="arrow"></div>
<script>
const outerTable = document.getElementById('outerTable');
const innerTable = document.getElementById('innerTable');
const resultTable = document.getElementById('resultTable').querySelector('tbody');
const startBtn = document.getElementById('startBtn');
const resetBtn = document.getElementById('resetBtn');
const stepBtn = document.getElementById('stepBtn');
const speedSlider = document.getElementById('speedSlider');
const speedValue = document.getElementById('speedValue');
const arrow = document.getElementById('arrow');
let outerRows = Array.from(outerTable.querySelectorAll('tbody tr'));
let innerRows = Array.from(innerTable.querySelectorAll('tbody tr'));
let currentOuterIndex = 0;
let currentInnerIndex = 0;
let isRunning = false;
let animationInterval;
let speed = 500;
speedSlider.addEventListener('input', function() {
speed = parseInt(this.value);
speedValue.textContent = speed + 'ms';
if (isRunning) {
clearInterval(animationInterval);
startAnimation();
}
});
startBtn.addEventListener('click', function() {
if (!isRunning) {
startAnimation();
} else {
pauseAnimation();
}
});
resetBtn.addEventListener('click', resetAnimation);
stepBtn.addEventListener('click', stepAnimation);
function startAnimation() {
isRunning = true;
startBtn.textContent = 'Pause';
animationInterval = setInterval(stepAnimation, speed);
}
function pauseAnimation() {
isRunning = false;
startBtn.textContent = 'Resume';
clearInterval(animationInterval);
}
function resetAnimation() {
pauseAnimation();
// Reset highlights
outerRows.forEach(row => row.classList.remove('highlight'));
innerRows.forEach(row => row.classList.remove('matched', 'highlight'));
// Clear result table
resultTable.innerHTML = '';
// Reset pointers
currentOuterIndex = 0;
currentInnerIndex = 0;
// Hide arrow
arrow.style.display = 'none';
}
function stepAnimation() {
// Clear previous highlights
outerRows.forEach(row => row.classList.remove('highlight'));
innerRows.forEach(row => row.classList.remove('matched', 'highlight'));
// If we've processed all outer rows, finish
if (currentOuterIndex >= outerRows.length) {
pauseAnimation();
return;
}
// Highlight current outer row
const currentOuterRow = outerRows[currentOuterIndex];
currentOuterRow.classList.add('highlight');
// Show arrow from outer to inner table
const outerRect = currentOuterRow.getBoundingClientRect();
const innerContainerRect = document.getElementById('innerTableContainer').getBoundingClientRect();
arrow.style.display = 'block';
arrow.style.left = (outerRect.right + 10) + 'px';
arrow.style.top = (outerRect.top + outerRect.height / 2) + 'px';
arrow.style.width = (innerContainerRect.left - outerRect.right - 20) + 'px';
// Get outer row's dept_id
const outerDeptId = currentOuterRow.querySelector('td:nth-child(3)').textContent;
// If we've scanned all inner rows, move to next outer row
if (currentInnerIndex >= innerRows.length) {
currentOuterIndex++;
currentInnerIndex = 0;
return;
}
// Highlight current inner row
const currentInnerRow = innerRows[currentInnerIndex];
currentInnerRow.classList.add('highlight');
// Get inner row's dept_id
const innerDeptId = currentInnerRow.querySelector('td:first-child').textContent;
// Check for match
if (outerDeptId === innerDeptId) {
currentInnerRow.classList.add('matched');
// Add to result table
const newRow = document.createElement('tr');
newRow.innerHTML = `
<td>${currentOuterRow.querySelector('td:nth-child(1)').textContent}</td>
<td>${currentOuterRow.querySelector('td:nth-child(2)').textContent}</td>
<td>${outerDeptId}</td>
<td>${currentInnerRow.querySelector('td:nth-child(2)').textContent}</td>
`;
resultTable.appendChild(newRow);
}
// Move to next inner row
currentInnerIndex++;
// If we've finished scanning inner table, move to next outer row
if (currentInnerIndex >= innerRows.length) {
setTimeout(() => {
currentOuterIndex++;
currentInnerIndex = 0;
}, speed / 2);
}
}
// Initialize
resetAnimation();
</script>
</body>
</html>