Skip to content

Commit d281219

Browse files
Merge pull request #1 from kaushik-baeldung/kaushik-baeldung-patch-1
SQL Eval Article - kaushik@baeldung.com
2 parents 24191bf + bb259a3 commit d281219

File tree

1 file changed

+233
-0
lines changed

1 file changed

+233
-0
lines changed

composite_index_eval_article.sql

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
USE University;
2+
3+
-- Difference between Single and Composite Index
4+
5+
-- Explain Single Index
6+
SELECT *
7+
FROM University.Student;
8+
9+
DESCRIBE University.Student;
10+
11+
SELECT *
12+
FROM University.Student
13+
WHERE national_id = '123345566';
14+
15+
EXPLAIN SELECT *
16+
FROM University.Student
17+
WHERE national_id = '123345566';
18+
19+
CREATE INDEX idx_national_id
20+
ON University.Student (national_id);
21+
22+
EXPLAIN SELECT *
23+
FROM University.Student
24+
WHERE national_id = '123345566';
25+
26+
-- Difference between Single and Composite Index
27+
28+
SELECT *
29+
FROM University.Registration;
30+
31+
SELECT student_id, course_id, semester, reg_datetime
32+
FROM University.Registration
33+
WHERE semester = 'Fall'
34+
AND student_id = 1001
35+
ORDER BY 2
36+
;
37+
38+
EXPLAIN SELECT student_id, course_id, semester, reg_datetime
39+
FROM University.Registration
40+
WHERE semester = 'Fall'
41+
AND student_id = 1001
42+
ORDER BY 2
43+
;
44+
45+
CREATE INDEX idx_student_id
46+
ON University.Registration (student_id);
47+
48+
CREATE INDEX idx_student_id_semester_course
49+
ON University.Registration (student_id, semester, course_id);
50+
51+
52+
-- Multi-Column Searches
53+
54+
SELECT *
55+
FROM University.Course;
56+
57+
SELECT *
58+
FROM University.Faculty;
59+
60+
SELECT *
61+
FROM University.Department;
62+
63+
SHOW INDEXES IN University.Course;
64+
65+
SELECT id
66+
FROM University.Department
67+
WHERE name = 'Computer Science';
68+
69+
SELECT id, name, department_id, position
70+
FROM University.Faculty
71+
WHERE department_id = (
72+
SELECT id
73+
FROM University.Department
74+
WHERE name = 'Computer Science'
75+
)
76+
ORDER BY 4, 2
77+
;
78+
79+
EXPLAIN SELECT id, name, department_id, position
80+
FROM University.Faculty
81+
WHERE department_id = (
82+
SELECT id
83+
FROM University.Department
84+
WHERE name = 'Computer Science'
85+
)
86+
ORDER BY 4, 2
87+
;
88+
CREATE INDEX idx_department_position_name
89+
ON University.Faculty (department_id, position, name);
90+
91+
SHOW INDEXES IN University.Faculty;
92+
93+
94+
CREATE INDEX idx_department_position
95+
ON University.Faculty (department_id, position);
96+
97+
-- Sorting and Grouping
98+
99+
-- Registration and Course Tables - Sorting and Grouping by semester and Year
100+
-- Find the number of students registered in each courses for each semester, ordered by year and semester
101+
102+
SELECT *
103+
FROM University.Registration;
104+
105+
SELECT *
106+
FROM University.Course;
107+
108+
SELECT c.name AS course_name,
109+
r.semester AS semester,
110+
YEAR(r.reg_datetime) AS year,
111+
COUNT(r.student_id) AS student_count
112+
FROM University.Registration r
113+
JOIN University.Course c
114+
ON r.course_id = c.id
115+
WHERE YEAR(r.reg_datetime) = 2023
116+
GROUP BY 1, 2, 3
117+
ORDER BY 3, 2
118+
;
119+
120+
EXPLAIN SELECT c.name AS course_name,
121+
r.semester AS semester,
122+
YEAR(r.reg_datetime) AS year,
123+
COUNT(r.student_id) AS student_count
124+
FROM University.Registration r
125+
JOIN University.Course c
126+
ON r.course_id = c.id
127+
WHERE YEAR(r.reg_datetime) = 2023
128+
GROUP BY 1, 2, 3
129+
ORDER BY 3, 2
130+
;
131+
132+
CREATE INDEX idx_reg_course_date
133+
ON University.Registration (course_id, semester, reg_datetime);
134+
135+
-- Coverign Index
136+
-- Find the grades of all students for a specific course, ordered by exam date
137+
138+
SELECT *
139+
FROM University.Exam;
140+
141+
SELECT student_id,
142+
grade,
143+
exam_date
144+
FROM University.Exam e
145+
WHERE course_id = 'CS211'
146+
ORDER BY exam_date;
147+
148+
EXPLAIN SELECT
149+
student_id,
150+
grade,
151+
exam_date
152+
FROM University.Exam e
153+
WHERE course_id = 'CS211'
154+
ORDER BY exam_date;
155+
156+
CREATE INDEX idx_course_exam_date_grade_student
157+
ON University.Exam (course_id, exam_date, grade, student_id);
158+
159+
160+
161+
-- Stored Procedure
162+
-- a stored procedure to find the grades for students in a specific course for a given year, ordered by exam date
163+
SHOW INDEXES IN University.Exam;
164+
165+
SELECT *
166+
FROM University.Exam;
167+
168+
DROP PROCEDURE IF EXISTS grades_course_year
169+
170+
DELIMITER $$
171+
CREATE PROCEDURE grades_course_year(
172+
p_course_id VARCHAR (10),
173+
p_year INT
174+
)
175+
BEGIN
176+
SELECT student_id,
177+
grade,
178+
exam_date
179+
FROM Exam
180+
WHERE course_id = p_course_id
181+
AND YEAR(exam_date) = p_year
182+
ORDER BY exam_date;
183+
END $$
184+
DELIMITER ;
185+
186+
CALL grades_course_year ('CS411', 2023);
187+
188+
-- Creating Composite index covering the stored procedure
189+
CREATE INDEX idx_course_exam
190+
ON University.Exam (course_id, exam_date);
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+

0 commit comments

Comments
 (0)