-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLab6.tex
464 lines (417 loc) · 28.5 KB
/
Lab6.tex
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T2A]{fontenc}
\usepackage[russian, english]{babel}
\usepackage{graphicx}
\usepackage{listings}
\begin{document}
\section{Постановка задачи}
Цели лабораторной работы:
\begin{enumerate}
\item Оптимизация запросов. Индексы. Типы индексов. B-Tree.
\item Оптимизация запросов. Explain. Profiling.
\end{enumerate}
\section{Типы индексов}
\begin{enumerate}
\item Primary, Unique, Index (B-Tree)
\item Fulltext (InvertedList)
\item Clustered/Non clustered
\end{enumerate}
\section{Инструменты}
\subsection{Explain}
\begin{lstlisting}[language=SQL]
EXPLAIN SELECT * FROM title;
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------+
| 1 | SIMPLE | title | NULL | ALL | NULL | NULL | NULL | NULL | 5713325 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------+
1 row in set, 2 warnings (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
EXPLAIN SELECT * FROM title WHERE titleID = 'tt0468569';
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | title | NULL | const | PRIMARY | PRIMARY | 40 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
EXPLAIN
SELECT * FROM title
INNER JOIN title_rating ON title.titleID = title_rating.titleID
ORDER BY numVotes DESC
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+--------------+------------+--------+---------------+---------+---------+--------------------------------+--
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | r
+----+-------------+--------------+------------+--------+---------------+---------+---------+--------------------------------+--
| 1 | SIMPLE | title_rating | NULL | ALL | PRIMARY | NULL | NULL | NULL | 9
| 1 | SIMPLE | title | NULL | eq_ref | PRIMARY | PRIMARY | 40 | imdb_full.title_rating.titleID |
+----+-------------+--------------+------------+--------+---------------+---------+---------+--------------------------------+--
2 rows in set, 1 warning (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
EXPLAIN
SELECT * FROM title
WHERE titleID IN (
SELECT titleID FROM title_genre
WHERE title_genre.genre IN ("Comedy", "Action")
)
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+-------------+------------+--------+---------------+---------+---------+-------------------------------+----
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | row
+----+-------------+-------------+------------+--------+---------------+---------+---------+-------------------------------+----
| 1 | SIMPLE | title_genre | NULL | index | PRIMARY | PRIMARY | 41 | NULL | 911
| 1 | SIMPLE | title | NULL | eq_ref | PRIMARY | PRIMARY | 40 | imdb_full.title_genre.titleID |
+----+-------------+-------------+------------+--------+---------------+---------+---------+-------------------------------+----
2 rows in set, 1 warning (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
EXPLAIN
SELECT * FROM title
WHERE EXISTS(
SELECT 1 FROM title_genre
WHERE title_genre.genre IN ("Comedy", "Action")
AND title.titleID = title_genre.titleID
)
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+--------------------+-------------+------------+------+---------------+---------+---------+-------------------------+-----
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows
+----+--------------------+-------------+------------+------+---------------+---------+---------+-------------------------+-----
| 1 | PRIMARY | title | NULL | ALL | NULL | NULL | NULL | NULL | 5713
| 2 | DEPENDENT SUBQUERY | title_genre | NULL | ref | PRIMARY | PRIMARY | 40 | imdb_full.title.titleID |
+----+--------------------+-------------+------------+------+---------------+---------+---------+-------------------------+-----
2 rows in set, 2 warnings (0.00 sec)
\end{lstlisting}
\subsection{Profile}
\begin{lstlisting}[language=SQL]
SET PROFILING=1;
SET profiling_history_size = 1;
\end{lstlisting}
\begin{lstlisting}[language=SQL]
SHOW PROFILES;
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----------+-------------+---------------------+
| Query_ID | Duration | Query |
+----------+-------------+---------------------+
| 1 | 10.16626100 | SELECT * FROM title |
+----------+-------------+---------------------+
1 row in set, 1 warning (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
SHOW PROFILE FOR QUERY 1;
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----------------------+-----------+
| Status | Duration |
+----------------------+-----------+
| starting | 0.000857 |
| checking permissions | 0.000005 |
| Opening tables | 0.000012 |
| init | 0.000012 |
| System lock | 0.000005 |
| optimizing | 0.000003 |
| statistics | 0.000007 |
| preparing | 0.000006 |
| executing | 0.000002 |
| Sending data | 10.165251 |
| end | 0.000011 |
| query end | 0.000007 |
| closing tables | 0.000008 |
| freeing items | 0.000059 |
| cleaning up | 0.000016 |
+----------------------+-----------+
15 rows in set, 1 warning (0.00 sec)
\end{lstlisting}
\section{Использование индексов}
\subsection{Scan}
Общее количество фильмов:
\begin{lstlisting}[language=SQL]
SELECT COUNT(*) FROM title;
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----------+
| COUNT(*) |
+----------+
| 6243651 |
+----------+
1 row in set (1.82 sec)
\end{lstlisting}
\subsection{Key lookup}
Без индекса по полю startYear:
\begin{lstlisting}[language=SQL]
SELECT SQL_NO_CACHE * FROM title WHERE startYear = 2019 LIMIT 1000;
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----------+------------+-------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------------------+
| 6 | 0.72669600 | SELECT * FROM title WHERE startYear = 2019 LIMIT 1000 |
+----------+------------+-------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
EXPLAIN SELECT SQL_NO_CACHE * FROM title WHERE startYear = 2019 LIMIT 1000;
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| 1 | SIMPLE | title | NULL | ALL | NULL | NULL | NULL | NULL | 5713325 | 10.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
\end{lstlisting}
Добавление индекса:
\begin{lstlisting}[language=SQL]
ALTER TABLE `title` ADD INDEX `startYearIndex` (`startYear` ASC);
\end{lstlisting}
С индексом по полю startYear:
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----------+------------+-------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------------------+
| 14 | 0.00276400 | SELECT * FROM title WHERE startYear = 2019 LIMIT 1000 |
+----------+------------+-------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+-------+------------+------+----------------+----------------+---------+-------+--------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+----------------+----------------+---------+-------+--------+----------+-------+
| 1 | SIMPLE | title | NULL | ref | startYearIndex | startYearIndex | 5 | const | 439292 | 100.00 | NULL |
+----+-------------+-------+------------+------+----------------+----------------+---------+-------+--------+----------+-------+
1 row in set, 1 warning (0.00 sec)
\end{lstlisting}
\subsection{Index range scan}
\begin{lstlisting}[language=SQL]
EXPLAIN SELECT SQL_NO_CACHE * FROM title_rating
WHERE averageRating BETWEEN 9 AND 10
\end{lstlisting}
До добавления индеса по полю averageRating:
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+--------------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | title_rating | NULL | ALL | NULL | NULL | NULL | NULL | 981120 | 11.11 | Using where |
+----+-------------+--------------+------------+------+---------------+------+---------+------+--------+----------+-------------+
1 row in set, 2 warnings (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
ALTER TABLE `imdb_full`.`title_rating`
ADD INDEX `averageRating` (`averageRating` ASC);
\end{lstlisting}
После добавления индекса по полю averageRating:
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+--------------+------------+-------+---------------+---------------+---------+------+-------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------+------------+-------+---------------+---------------+---------+------+-------+----------+-----------------------+
| 1 | SIMPLE | title_rating | NULL | range | averageRating | averageRating | 8 | NULL | 72116 | 100.00 | Using index condition |
+----+-------------+--------------+------------+-------+---------------+---------------+---------+------+-------+----------+-----------------------+
1 row in set, 2 warnings (0.00 sec)
\end{lstlisting}
\subsection{Group by}
До добавления индекса по полю genre:
\begin{lstlisting}[language=SQL]
SELECT SQL_NO_CACHE genre, COUNT(titleID) FROM imdb_full.title_genre GROUP BY genre
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----------+------------+-------------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------------------------------------------------+
| 8 | 5.94673000 | SELECT SQL_NO_CACHE genre, COUNT(titleID) FROM imdb_full.title_genre GROUP BY genre |
+----------+------------+-------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
EXPLAIN SELECT SQL_NO_CACHE genre, COUNT(titleID) FROM imdb_full.title_genre GROUP BY genre;
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+-------------+------------+-------+---------------+---------+---------+------+---------+----------+----------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------------+------------+-------+---------------+---------+---------+------+---------+----------+----------------------------------------------+
| 1 | SIMPLE | title_genre | NULL | index | PRIMARY | PRIMARY | 41 | NULL | 9118175 | 100.00 | Using index; Using temporary; Using filesort |
+----+-------------+-------------+------------+-------+---------------+---------+---------+------+---------+----------+----------------------------------------------+
1 row in set, 2 warnings (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
ALTER TABLE `imdb_full`.`title_genre` ADD INDEX `genreIndex` (`genre` ASC);
\end{lstlisting}
После добавления индекса по полю genre:
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----------+------------+-------------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------------------------------------------------+
| 9 | 1.78642400 | SELECT SQL_NO_CACHE genre, COUNT(titleID) FROM imdb_full.title_genre GROUP BY genre |
+----------+------------+-------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+-------------+------------+-------+--------------------+------------+---------+------+---------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------------+------------+-------+--------------------+------------+---------+------+---------+----------+-------------+
| 1 | SIMPLE | title_genre | NULL | index | PRIMARY,genreIndex | genreIndex | 1 | NULL | 9118175 | 100.00 | Using index |
+----+-------------+-------------+------------+-------+--------------------+------------+---------+------+---------+----------+-------------+
1 row in set, 2 warnings (0.00 sec)
\end{lstlisting}
\subsection{Order by}
До добавления индекса по полю numVotes.
\begin{lstlisting}[language=SQL]
SELECT SQL_NO_CACHE titleID, numVotes FROM title_rating ORDER BY numVotes DESC LIMIT 1000;
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----------+------------+-------------------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------------------------------------------------------+
| 21 | 0.30404600 | SELECT SQL_NO_CACHE titleID, numVotes FROM title_rating ORDER BY numVotes DESC LIMIT 1000 |
+----------+------------+-------------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
EXPLAIN SELECT SQL_NO_CACHE titleID, numVotes FROM title_rating ORDER BY numVotes DESC LIMIT 1000;
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+--------------+------------+------+---------------+------+---------+------+--------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------+------------+------+---------------+------+---------+------+--------+----------+----------------+
| 1 | SIMPLE | title_rating | NULL | ALL | NULL | NULL | NULL | NULL | 981120 | 100.00 | Using filesort |
+----+-------------+--------------+------------+------+---------------+------+---------+------+--------+----------+----------------+
1 row in set, 2 warnings (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
ALTER TABLE `title_rating` ADD INDEX `numVotesIndex` (`numVotes` ASC);
\end{lstlisting}
После добавления индекса по полю numVotes.
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----------+------------+-------------------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------------------------------------------------------+
| 22 | 0.00075700 | SELECT SQL_NO_CACHE titleID, numVotes FROM title_rating ORDER BY numVotes DESC LIMIT 1000 |
+----------+------------+-------------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+--------------+------------+-------+---------------+---------------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------+------------+-------+---------------+---------------+---------+------+------+----------+-------------+
| 1 | SIMPLE | title_rating | NULL | index | NULL | numVotesIndex | 4 | NULL | 1000 | 100.00 | Using index |
+----+-------------+--------------+------------+-------+---------------+---------------+---------+------+------+----------+-------------+
1 row in set, 2 warnings (0.00 sec)
\end{lstlisting}
\subsection{Join}
\begin{lstlisting}[language=SQL]
EXPLAIN SELECT * FROM title
INNER JOIN title_rating ON title.titleID = title_rating.titleID
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+--------------+------------+--------+---------------+---------+---------+--------------------------------+--------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------------+------------+--------+---------------+---------+---------+--------------------------------+--------+----------+-------+
| 1 | SIMPLE | title_rating | NULL | ALL | PRIMARY | NULL | NULL | NULL | 981120 | 100.00 | NULL |
| 1 | SIMPLE | title | NULL | eq_ref | PRIMARY | PRIMARY | 40 | imdb_full.title_rating.titleID | 1 | 100.00 | NULL |
+----+-------------+--------------+------------+--------+---------------+---------+---------+--------------------------------+--------+----------+-------+
2 rows in set, 1 warning (0.00 sec)
\end{lstlisting}
\subsection{Subqueries}
\begin{lstlisting}[language=SQL]
EXPLAIN
SELECT * FROM title
WHERE titleID IN (
SELECT titleID FROM title_genre
WHERE title_genre.genre IN ("Comedy", "Action")
)
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+-------------+------------+--------+---------------+---------+---------+-------------------------------+---------+----------+------------------------------------
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra
+----+-------------+-------------+------------+--------+---------------+---------+---------+-------------------------------+---------+----------+------------------------------------
| 1 | SIMPLE | title_genre | NULL | index | PRIMARY | PRIMARY | 41 | NULL | 9118175 | 12.50 | Using where; Using index; LooseScan
| 1 | SIMPLE | title | NULL | eq_ref | PRIMARY | PRIMARY | 40 | imdb_full.title_genre.titleID | 1 | 100.00 | NULL
+----+-------------+-------------+------------+--------+---------------+---------+---------+-------------------------------+---------+----------+------------------------------------
2 rows in set, 1 warning (0.00 sec)
\end{lstlisting}
\section{Примеры оптимизации запросов}
Вывести список людей и количество людей с такой же датой рождения.
\begin{lstlisting}[language=SQL]
EXPLAIN
SELECT F.primaryName, COUNT(F.nameID) FROM name F, name S
WHERE F.nameID != S.nameID AND F.birthYear = S.birthYear
GROUP BY F.nameID
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+----------------------------------------------------+
| 1 | SIMPLE | F | NULL | ALL | PRIMARY | NULL | NULL | NULL | 9371346 | 100.00 | Using temporary; Using filesort |
| 1 | SIMPLE | S | NULL | ALL | NULL | NULL | NULL | NULL | 9371346 | 9.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+----------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
ALTER TABLE `name` ADD INDEX `birthYearIndex` (`birthYear` ASC);
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----+-------------+-------+------------+-------+-------------------+-----------+---------+-----------------------+---------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+-------------------+-----------+---------+-----------------------+---------+----------+--------------------------+
| 1 | SIMPLE | F | NULL | index | PRIMARY,birthYear | PRIMARY | 40 | NULL | 9371346 | 100.00 | Using where |
| 1 | SIMPLE | S | NULL | ref | birthYear | birthYear | 5 | imdb_full.F.birthYear | 65533 | 90.00 | Using where; Using index |
+----+-------------+-------+------------+-------+-------------------+-----------+---------+-----------------------+---------+----------+--------------------------+
2 rows in set, 1 warning (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----------+------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| 38 | 2.14287300 | SELECT F.primaryName, COUNT(F.nameID) FROM name F, name S WHERE F.nameID != S.nameID AND F.birthYear = S.birthYear GROUP BY F.nameID LIMIT 1000 |
+----------+------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
\end{lstlisting}
Получить список фильмов отсортированных по рейтингу:
\begin{lstlisting}[language=SQL]
EXPLAIN SELECT SQL_NO_CACHE * FROM title
JOIN title_rating ON title.titleID = title_rating.titleID
ORDER BY title_rating.numVotes DESC
LIMIT 1000
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------
| Query_ID | Duration | Query
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------
| 43 | 0.57302600 | SELECT title.titleID, title.primaryTitle, numVotes FROM title INNER JOIN title_rating ON title.titleID = title_rating.titleID ORDER BY numVotes DESC LIMIT
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set, 1 warning (0.00 sec)
\end{lstlisting}
\begin{lstlisting}[language=SQL]
ALTER TABLE `title_rating` ADD INDEX `numVotes` (`numVotes` DESC);
\end{lstlisting}
\begin{lstlisting}[language=SQL]
EXPLAIN SELECT SQL_NO_CACHE * FROM title
JOIN title_rating ON title.titleID = title_rating.titleID
ORDER BY title_rating.numVotes DESC
LIMIT 1000
\end{lstlisting}
\begin{lstlisting}[basicstyle = \tiny\ttfamily, columns = fixed]
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------
| Query_ID | Duration | Query
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------
| 44 | 0.00314300 | SELECT title.titleID, title.primaryTitle, numVotes FROM title INNER JOIN title_rating ON title.titleID = title_rating.titleID ORDER BY numVotes DESC LIMIT
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set, 1 warning (0.00 sec)
\end{lstlisting}
\section{Используемые источники}
\begin{enumerate}
\item https://dev.mysql.com/doc/refman/8.0/en/execution-plan-information.html
\item https://dev.mysql.com/doc/refman/8.0/en/explain-output.htmlv
\item https://dev.mysql.com/doc/refman/8.0/en/optimization.html
\item https://dev.mysql.com/doc/refman/8.0/en/optimization-indexes.html
\end{enumerate}
\end{document}