-
Notifications
You must be signed in to change notification settings - Fork 1
/
実機検証_mysql.txt
439 lines (317 loc) · 22.6 KB
/
実機検証_mysql.txt
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
------------------------------------------------------------
■環境構築
C:\Users\yyamasak>mysql -u root -p
Enter password: ****
mysql> create database dbstudy;
mysql> exit;
C:\Users\yyamasak>mysql -u root -f -D dbstudy -p < cretab_script_mysql.sql
Enter password: ****
------------------------------------------------------------
■333
use dbstudy;
show create table emp_huge\G
create index emp_huge_sal_idx on emp_huge(sal);
show create table emp_huge\G
select ename FROM emp_huge WHERE sal*1.1 < 1500;
select ename FROM emp_huge WHERE sal < 1500/1.1;
explain select ename FROM emp_huge WHERE sal*1.1 < 1500;
explain select ename FROM emp_huge WHERE sal < 1500/1.1;
mysql> explain select ename FROM emp_huge WHERE sal*1.1 < 1500;
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | emp_huge | ALL | NULL | NULL | NULL | NULL | 9849 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain select ename FROM emp_huge WHERE sal < 1500/1.1;
+----+-------------+----------+-------+------------------+------------------+---------+------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+------------------+------------------+---------+------+------+-----------------------+
| 1 | SIMPLE | emp_huge | range | emp_huge_sal_idx | emp_huge_sal_idx | 5 | NULL | 364 | Using index condition |
+----+-------------+----------+-------+------------------+------------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)
mysql>
------------------------------------------------------------
■334
use dbstudy;
show create table emp_huge\G
create index emp_huge_hiredate on emp_huge(hiredate);
show create table emp_huge\G
SELECT ename FROM emp_huge WHERE DATE_FORMAT(hiredate, '%y%m%d') = '801217';
SELECT ename FROM emp_huge WHERE hiredate = STR_TO_DATE(801217,'%y%m%d');
explain SELECT ename FROM emp_huge WHERE DATE_FORMAT(hiredate, '%y%m%d') = '801217';
explain SELECT ename FROM emp_huge WHERE hiredate = STR_TO_DATE(801217,'%y%m%d');
mysql> explain SELECT ename FROM emp_huge WHERE DATE_FORMAT(hiredate, '%y%m%d') = '801217';
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | emp_huge | ALL | NULL | NULL | NULL | NULL | 9849 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain SELECT ename FROM emp_huge WHERE hiredate = STR_TO_DATE(801217,'%y%m%d');
+----+-------------+----------+------+-------------------+-------------------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+-------------------+-------------------+---------+-------+------+-------+
| 1 | SIMPLE | emp_huge | ref | emp_huge_hiredate | emp_huge_hiredate | 4 | const | 1 | NULL |
+----+-------------+----------+------+-------------------+-------------------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql>
------------------------------------------------------------
■335
use dbstudy;
show create table emp_huge\G
create index emp_huge_mgr on emp_huge(mgr);
show create table emp_huge\G
SELECT ename FROM emp_huge WHERE mgr=1;
SELECT ename FROM emp_huge WHERE mgr='1';
explain SELECT ename FROM emp_huge WHERE mgr=1;
explain SELECT ename FROM emp_huge WHERE mgr='1';
mysql> explain SELECT ename FROM emp_huge WHERE mgr=1;
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | emp_huge | ALL | emp_huge_mgr | NULL | NULL | NULL | 9849 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql>
mysql> explain SELECT ename FROM emp_huge WHERE mgr='1';
+----+-------------+----------+------+---------------+--------------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+--------------+---------+-------+------+-----------------------+
| 1 | SIMPLE | emp_huge | ref | emp_huge_mgr | emp_huge_mgr | 13 | const | 1 | Using index condition |
+----+-------------+----------+------+---------------+--------------+---------+-------+------+-----------------------+
1 row in set (0.00 sec)
mysql>
------------------------------------------------------------
■336
use dbstudy;
show create table emp_huge\G
create index emp_huge_deptno on emp_huge(deptno);
show create table emp_huge\G
SELECT ename FROM emp_huge WHERE deptno <> 30;
SELECT ename FROM emp_huge WHERE deptno IN (SELECT deptno FROM dept WHERE deptno <> 30);
explain SELECT ename FROM emp_huge WHERE deptno <> 30;
explain SELECT ename FROM emp_huge WHERE deptno IN (SELECT deptno FROM dept WHERE deptno <> 30);
------------------------------------------------------------
■337
show create table emp_huge\G
create index emp_huge_ename on emp_huge(ename);
show create table emp_huge\G
SELECT ename FROM emp_huge WHERE ename = 'DAIZEN' OR empno = 0;
explain SELECT ename FROM emp_huge WHERE ename = 'DAIZEN' OR empno = 0;
mysql> explain SELECT ename FROM emp_huge WHERE ename = 'DAIZEN' OR empno = 0;
+----+-------------+----------+------+----------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+----------------+------+---------+------+------+-------------+
| 1 | SIMPLE | emp_huge | ALL | emp_huge_ename | NULL | NULL | NULL | 9849 | Using where |
+----+-------------+----------+------+----------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql>
show create table emp_huge\G
create index emp_huge_empno on emp_huge(empno);
show create table emp_huge\G
SELECT ename FROM emp_huge WHERE ename = 'DAIZEN' OR empno = 0;
mysql> explain SELECT ename FROM emp_huge WHERE ename = 'DAIZEN' OR empno = 0;
+----+-------------+----------+-------------+-------------------------------+-------------------------------+---------+------+------+---------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------------+-------------------------------+-------------------------------+---------+------+------+---------------------------------------------------------+
| 1 | SIMPLE | emp_huge | index_merge | emp_huge_ename,emp_huge_empno | emp_huge_ename,emp_huge_empno | 766,3 | NULL | 2 | Using union(emp_huge_ename,emp_huge_empno); Using where |
+----+-------------+----------+-------------+-------------------------------+-------------------------------+---------+------+------+---------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
------------------------------------------------------------
■338
show create table emp_huge\G
create index emp_huge_deptno on emp_huge(deptno);
show create table emp_huge\G
SELECT ename FROM emp_huge WHERE deptno NOT IN (20,30);
SELECT ename FROM emp_huge WHERE deptno IN (SELECT deptno FROM dept WHERE deptno not in (20,30));
explain SELECT ename FROM emp_huge WHERE deptno NOT IN (20,30);
explain SELECT ename FROM emp_huge WHERE deptno IN (SELECT deptno FROM dept WHERE deptno not in (20,30));
mysql> explain SELECT ename FROM emp_huge WHERE deptno NOT IN (20,30);
+----+-------------+----------+-------+-----------------+-----------------+---------+------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+-----------------+-----------------+---------+------+------+-----------------------+
| 1 | SIMPLE | emp_huge | range | emp_huge_deptno | emp_huge_deptno | 2 | NULL | 3 | Using index condition |
+----+-------------+----------+-------+-----------------+-----------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)
mysql>
mysql> explain SELECT ename FROM emp_huge WHERE deptno IN (SELECT deptno FROM dept WHERE deptno not in (20,30));
+----+-------------+----------+-------+-----------------+-----------------+---------+------+------+--------------------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+-----------------+-----------------+---------+------+------+--------------------------------------------------------------------------+
| 1 | SIMPLE | emp_huge | range | emp_huge_deptno | emp_huge_deptno | 2 | NULL | 3 | Using index condition |
| 1 | SIMPLE | dept | ALL | NULL | NULL | NULL | NULL | 4 | Using where; FirstMatch(emp_huge); Using join buffer (Block Nested Loop) |
+----+-------------+----------+-------+-----------------+-----------------+---------+------+------+--------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
------------------------------------------------------------
■339
show create table emp_huge\G
create index emp_huge_job_deptno on emp_huge(job,deptno);
show create table emp_huge\G
SELECT empno FROM emp_huge WHERE job = 'AUTHOR' AND deptno = 10;
SELECT empno FROM emp_huge WHERE job = 'AUTHOR';
SELECT empno FROM emp_huge WHERE deptno = 10;
explain SELECT empno FROM emp_huge WHERE job = 'AUTHOR' AND deptno = 10;
explain SELECT empno FROM emp_huge WHERE job = 'AUTHOR';
explain SELECT empno FROM emp_huge WHERE deptno = 10;
mysql> explain SELECT empno FROM emp_huge WHERE job = 'AUTHOR' AND deptno = 10;
+----+-------------+----------+------+---------------------+---------------------+---------+-------------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------------+---------------------+---------+-------------+------+-----------------------+
| 1 | SIMPLE | emp_huge | ref | emp_huge_job_deptno | emp_huge_job_deptno | 32 | const,const | 1 | Using index condition |
+----+-------------+----------+------+---------------------+---------------------+---------+-------------+------+-----------------------+
1 row in set (0.00 sec)
mysql> explain SELECT empno FROM emp_huge WHERE job = 'AUTHOR';
+----+-------------+----------+------+---------------------+---------------------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------------+---------------------+---------+-------+------+-----------------------+
| 1 | SIMPLE | emp_huge | ref | emp_huge_job_deptno | emp_huge_job_deptno | 30 | const | 1 | Using index condition |
+----+-------------+----------+------+---------------------+---------------------+---------+-------+------+-----------------------+
1 row in set (0.00 sec)
mysql> explain SELECT empno FROM emp_huge WHERE deptno = 10;
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | emp_huge | ALL | NULL | NULL | NULL | NULL | 9849 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql>
------------------------------------------------------------
■340 ※例題の例が、SQL文の使い方として不適切
show create table emp_huge\G
create index emp_huge_deptno on emp_huge(deptno);
show create table emp_huge\G
SELECT deptno, COUNT(empno) FROM emp_huge GROUP BY deptno HAVING deptno = 10;
SELECT deptno, COUNT(empno) FROM emp_huge WHERE deptno = 10 GROUP BY deptno;
explain SELECT deptno, COUNT(empno) FROM emp_huge GROUP BY deptno HAVING deptno = 10;
explain SELECT deptno, COUNT(empno) FROM emp_huge WHERE deptno = 10 GROUP BY deptno;
mysql> explain SELECT deptno, COUNT(empno) FROM emp_huge GROUP BY deptno HAVING deptno = 10;
+----+-------------+----------+-------+-------------------------------------+-----------------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+-------------------------------------+-----------------+---------+------+------+-------+
| 1 | SIMPLE | emp_huge | index | emp_huge_job_deptno,emp_huge_deptno | emp_huge_deptno | 2 | NULL | 9849 | NULL |
+----+-------------+----------+-------+-------------------------------------+-----------------+---------+------+------+-------+
1 row in set (0.00 sec)
mysql>
mysql> explain SELECT deptno, COUNT(empno) FROM emp_huge WHERE deptno = 10 GROUP BY deptno;
+----+-------------+----------+------+-------------------------------------+-----------------+---------+-------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+-------------------------------------+-----------------+---------+-------+------+----------------------------------------------+
| 1 | SIMPLE | emp_huge | ref | emp_huge_job_deptno,emp_huge_deptno | emp_huge_deptno | 2 | const | 1 | Using where; Using temporary; Using filesort |
+----+-------------+----------+------+-------------------------------------+-----------------+---------+-------+------+----------------------------------------------+
1 row in set (0.00 sec)
mysql>
------------------------------------------------------------
■341
show create table emp_huge\G
create index emp_huge_job_hiredate on emp_huge(job,hiredate);
show create table emp_huge\G
SELECT hiredate FROM emp_huge WHERE job='AUTHOR';
explain SELECT hiredate FROM emp_huge WHERE job='AUTHOR';
mysql> explain SELECT hiredate FROM emp_huge WHERE job='AUTHOR';
+----+-------------+----------+------+-------------------------------------------+---------------------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+-------------------------------------------+---------------------+---------+-------+------+-----------------------+
| 1 | SIMPLE | emp_huge | ref | emp_huge_job_deptno,emp_huge_job_hiredate | emp_huge_job_deptno | 30 | const | 1 | Using index condition |
+----+-------------+----------+------+-------------------------------------------+---------------------+---------+-------+------+-----------------------+
1 row in set (0.00 sec)
mysql>
------------------------------------------------------------
■342
show create table emp_huge\G
create index emp_huge_empno on emp_huge(empno);
show create table emp_huge\G
SELECT ename FROM emp_huge WHERE empno > 9000 ORDER BY empno;
explain SELECT ename FROM emp_huge WHERE empno > 9000 ORDER BY empno;
mysql> explain SELECT ename FROM emp_huge WHERE empno > 9000 ORDER BY empno;
+----+-------------+----------+-------+----------------+----------------+---------+------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+----------------+----------------+---------+------+------+-----------------------+
| 1 | SIMPLE | emp_huge | range | emp_huge_empno | emp_huge_empno | 3 | NULL | 999 | Using index condition |
+----+-------------+----------+-------+----------------+----------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)
mysql>
※索引が無い場合
mysql> explain SELECT ename FROM emp_huge WHERE empno > 9000 ORDER BY empno;
+----+-------------+----------+------+---------------+------+---------+------+-------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+-------+-----------------------------+
| 1 | SIMPLE | emp_huge | ALL | NULL | NULL | NULL | NULL | 10001 | Using where; Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+-------+-----------------------------+
1 row in set (0.00 sec)
mysql>
------------------------------------------------------------
■343
show create table emp_huge\G
create index emp_huge_sal on emp_huge(sal);
show create table emp_huge\G
SELECT DISTINCT sal FROM emp_huge;
explain SELECT DISTINCT sal FROM emp_huge;
mysql> explain SELECT DISTINCT sal FROM emp_huge;
+----+-------------+----------+-------+---------------+--------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+---------------+--------------+---------+------+------+-------------+
| 1 | SIMPLE | emp_huge | index | emp_huge_sal | emp_huge_sal | 5 | NULL | 9849 | Using index |
+----+-------------+----------+-------+---------------+--------------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql>
※索引が無い場合
mysql> explain SELECT DISTINCT sal FROM emp_huge;
+----+-------------+----------+------+---------------+------+---------+------+-------+-----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+-------+-----------------+
| 1 | SIMPLE | emp_huge | ALL | NULL | NULL | NULL | NULL | 10001 | Using temporary |
+----+-------------+----------+------+---------------+------+---------+------+-------+-----------------+
1 row in set (0.00 sec)
mysql>
------------------------------------------------------------
■344
show create table emp_huge\G
create index emp_huge_deptno on emp_huge(deptno);
show create table emp_huge\G
SELECT deptno,max(sal) FROM emp_huge WHERE deptno <> 10 GROUP BY deptno;
explain SELECT deptno,max(sal) FROM emp_huge WHERE deptno <> 10 GROUP BY deptno;
mysql> explain SELECT deptno,max(sal) FROM emp_huge WHERE deptno <> 10 GROUP BY deptno;
+----+-------------+----------+-------+-----------------+-----------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+-----------------+-----------------+---------+------+------+-------------+
| 1 | SIMPLE | emp_huge | index | emp_huge_deptno | emp_huge_deptno | 2 | NULL | 9849 | Using where |
+----+-------------+----------+-------+-----------------+-----------------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql>
※索引が無い場合
mysql> explain SELECT deptno,max(sal) FROM emp_huge WHERE deptno <> 10 GROUP BY deptno;
+----+-------------+----------+------+---------------+------+---------+------+-------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+-------+----------------------------------------------+
| 1 | SIMPLE | emp_huge | ALL | NULL | NULL | NULL | NULL | 10001 | Using where; Using temporary; Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+-------+----------------------------------------------+
1 row in set (0.02 sec)
mysql>
------------------------------------------------------------
■345
show create table emp_huge\G
create index emp_huge_sal on emp_huge(sal);
show create table emp_huge\G
SELECT MAX(sal) FROM emp_huge;
explain SELECT MAX(sal) FROM emp_huge;
mysql> explain SELECT MAX(sal) FROM emp_huge;
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
1 row in set (0.00 sec)
mysql>
※索引が無い場合
mysql> explain SELECT MAX(sal) FROM emp_huge;
+----+-------------+----------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+------+-------+
| 1 | SIMPLE | emp_huge | ALL | NULL | NULL | NULL | NULL | 9849 | NULL |
+----+-------------+----------+------+---------------+------+---------+------+------+-------+
1 row in set (0.00 sec)
mysql>
------------------------------------------------------------