MySQL Index Cookbook Deep & Wide Index Tutorial
Rick James
Feb., 2015
MySQL Index Cookbook Deep & Wide Index Tutorial Rick James - - PowerPoint PPT Presentation
MySQL Index Cookbook Deep & Wide Index Tutorial Rick James Feb., 2015 TOC Preface Case Study PRIMARY KEY Use Cases EXPLAIN Work-Arounds Datatypes Tools PARTITIONing MyISAM
Feb., 2015
2
3
4
5
6
http://en.wikipedia.org/wiki/B-tree http:// upload.wikimedia.org/wikipedia/commons/thumb/6/65/B-tree.svg/500px-B-tree.svg.png http://upload.wikimedia.org/wikipedia/commons/thumb/6/65/B-tree.svg/500px-B- tree.svg.png
7
8
9
10
11
12
13
14
15
16
17
18
+-----+------------+-----------+-----------+ | seq | last | first | term | +-----+------------+-----------+-----------+ | 1 | Washington | George | 1789-1797 | | 2 | Adams | John | 1797-1801 | ... | 7 | Jackson | Andrew | 1829-1837 | ... | 17 | Johnson | Andrew | 1865-1869 | ... | 36 | Johnson | Lyndon B. | 1963-1969 | ...
19
20
21
type: ALL <-- Implies table scan key: NULL <-- Implies that no index is useful, hence table scan rows: 44 <-- That's about how many rows in the table, so table scan
22
23
24
type: index_merge possible_keys: first, last key: first, last key_len: 92,92 rows: 1 Extra: Using intersect(first,last); Using where
25
key_len: 184 ← length of both fields ref: const, const ← WHERE had constants rows: 1 ← Goodie
26
key_len: 184 ← length of both fields ref: const, const ← WHERE had constants rows: 1 ← Goodie Extra: Using where; Using index ← Note
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
id INT UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(255), PRIMARY KEY (id), UNIQUE (name)
In MyISAM add these to “cover”: INDEX(id,name), INDEX(name,id)
45
id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, md5 BINARY(16/22/32) NOT NULL, stuff TEXT/BLOB NOT NULL,
INSERT INTO tbl (md5, stuff) VALUES($m,$s)
ON DUPLICATE KEY UPDATE id=LAST_INDERT_ID(id);
$id = SELECT LAST_INSERT_ID();
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
http://dev.mysql.com/doc/refman/5.5/en/explain-output.html
63
<#>
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
WHERE dt >= ‘2009-02-27’ AND dt < ‘2009-02-27’ + INTERVAL 4 DAY
WHERE dt >= ‘2009-01-01’ AND dt < ‘2009-01-01’ + INTERVAL 1 YEAR
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
http://mysql.rjweb.org/doc.php/myisam2innodb
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