Debian Codegraph @

mysql-test/suite/heap/heap_hash.test

raw
1 #
2 # Test of heap tables.
3 #
4
5 create table t1 (a int not null,b int not null, primary key using HASH (a)) engine=heap comment="testing heaps" avg_row_length=100 min_rows=1 max_rows=100;
6 insert into t1 values(1,1),(2,2),(3,3),(4,4);
7 delete from t1 where a=1 or a=0;
8 #show table status like "t1";
9 show keys from t1;
10 select * from t1;
11 select * from t1 where a=4;
12 update t1 set b=5 where a=4;
13 update t1 set b=b+1 where a>=3;
14 replace t1 values (3,3);
15 select * from t1;
16 alter table t1 add c int not null, add key using HASH (c,a);
17 drop table t1;
18
19 create table t1 (a int not null,b int not null, primary key using HASH (a)) engine=heap comment="testing heaps";
20 insert into t1 values(1,1),(2,2),(3,3),(4,4);
21 delete from t1 where a > 0;
22 select * from t1;
23 drop table t1;
24
25 create table t1 (a int not null,b int not null, primary key using HASH (a)) engine=heap comment="testing heaps";
26 insert into t1 values(1,1),(2,2),(3,3),(4,4);
27 alter table t1 modify a int not null auto_increment, engine=myisam, comment="new myisam table";
28 #show table status like "t1";
29 select * from t1;
30 drop table t1;
31
32 create table t1 (a int not null) engine=heap;
33 insert into t1 values (869751),(736494),(226312),(802616),(728912);
34 select * from t1 where a > 736494;
35 alter table t1 add unique uniq_id using HASH (a);
36 select * from t1 where a > 736494;
37 select * from t1 where a = 736494;
38 select * from t1 where a=869751 or a=736494;
39 select * from t1 where a in (869751,736494,226312,802616);
40 alter table t1 engine=myisam;
41 explain select * from t1 where a in (869751,736494,226312,802616);
42 drop table t1;
43
44 create table t1 (x int not null, y int not null, key x using HASH (x), unique y using HASH (y))
45 engine=heap;
46 insert into t1 values (1,1),(2,2),(1,3),(2,4),(2,5),(2,6);
47 select * from t1 where x=1;
48 select * from t1,t1 as t2 where t1.x=t2.y;
49 explain select * from t1,t1 as t2 where t1.x=t2.y;
50 drop table t1;
51
52 create table t1 (a int) engine=heap;
53 insert into t1 values(1);
54 select max(a) from t1;
55 drop table t1;
56
57 CREATE TABLE t1 ( a int not null default 0, b int not null default 0, key using HASH (a), key using HASH (b) ) ENGINE=HEAP;
58 insert into t1 values(1,1),(1,2),(2,3),(1,3),(1,4),(1,5),(1,6);
59 select * from t1 where a=1;
60 insert into t1 values(1,1),(1,2),(2,3),(1,3),(1,4),(1,5),(1,6);
61 select * from t1 where a=1;
62 drop table t1;
63
64 create table t1 (id int unsigned not null, primary key using HASH (id)) engine=HEAP;
65 insert into t1 values(1);
66 select max(id) from t1;
67 insert into t1 values(2);
68 select max(id) from t1;
69 replace into t1 values(1);
70 drop table t1;
71
72 create table t1 (n int) engine=heap;
73 drop table t1;
74
75 create table t1 (n int) engine=heap;
76 drop table if exists t1;
77
78 # Test of non unique index
79
80 CREATE table t1(f1 int not null,f2 char(20) not
81 null,index(f2)) engine=heap;
82 INSERT into t1 set f1=12,f2="bill";
83 INSERT into t1 set f1=13,f2="bill";
84 INSERT into t1 set f1=14,f2="bill";
85 INSERT into t1 set f1=15,f2="bill";
86 INSERT into t1 set f1=16,f2="ted";
87 INSERT into t1 set f1=12,f2="ted";
88 INSERT into t1 set f1=12,f2="ted";
89 INSERT into t1 set f1=12,f2="ted";
90 INSERT into t1 set f1=12,f2="ted";
91 delete from t1 where f2="bill";
92 select * from t1;
93 drop table t1;
94
95 #
96 # Test when using part key searches
97 #
98
99 create table t1 (btn char(10) not null, key using HASH (btn)) engine=heap;
100 insert into t1 values ("hello"),("hello"),("hello"),("hello"),("hello"),("a"),("b"),("c"),("d"),("e"),("f"),("g"),("h"),("i");
101 explain select * from t1 where btn like "q%";
102 select * from t1 where btn like "q%";
103 alter table t1 add column new_col char(1) not null, add key using HASH (btn,new_col), drop key btn;
104 update t1 set new_col=left(btn,1);
105 explain select * from t1 where btn="a";
106 explain select * from t1 where btn="a" and new_col="a";
107 drop table t1;
108
109 #
110 # Test of NULL keys
111 #
112
113 CREATE TABLE t1 (
114 a int default NULL,
115 b int default NULL,
116 KEY a using HASH (a),
117 UNIQUE b using HASH (b)
118 ) engine=heap;
119 INSERT INTO t1 VALUES (NULL,99),(99,NULL),(1,1),(2,2),(1,3);
120 SELECT * FROM t1 WHERE a=NULL;
121 explain SELECT * FROM t1 WHERE a IS NULL;
122 SELECT * FROM t1 WHERE a<=>NULL;
123 SELECT * FROM t1 WHERE b=NULL;
124 explain SELECT * FROM t1 WHERE b IS NULL;
125 SELECT * FROM t1 WHERE b<=>NULL;
126
127 --error ER_DUP_ENTRY
128 INSERT INTO t1 VALUES (1,3);
129 DROP TABLE t1;
130
131 #
132 # Test when deleting all rows
133 #
134
135 CREATE TABLE t1 (a int not null, primary key using HASH (a)) engine=heap;
136 INSERT into t1 values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11);
137 DELETE from t1 where a < 100;
138 SELECT * from t1;
139 DROP TABLE t1;
140
141
142 #
143 # Hash index # records estimate test
144 #
145 create table t1
146 (
147 a char(8) not null,
148 b char(20) not null,
149 c int not null,
150 key (a)
151 ) engine=heap;
152
153 insert into t1 values ('aaaa', 'prefill-hash=5',0);
154 insert into t1 values ('aaab', 'prefill-hash=0',0);
155 insert into t1 values ('aaac', 'prefill-hash=7',0);
156 insert into t1 values ('aaad', 'prefill-hash=2',0);
157 insert into t1 values ('aaae', 'prefill-hash=1',0);
158 insert into t1 values ('aaaf', 'prefill-hash=4',0);
159 insert into t1 values ('aaag', 'prefill-hash=3',0);
160 insert into t1 values ('aaah', 'prefill-hash=6',0);
161
162 explain select * from t1 where a='aaaa';
163 explain select * from t1 where a='aaab';
164 explain select * from t1 where a='aaac';
165 explain select * from t1 where a='aaad';
166 insert into t1 select * from t1;
167
168 # avoid statistics differences between normal and ps-protocol tests
169 flush tables;
170 explain select * from t1 where a='aaaa';
171 explain select * from t1 where a='aaab';
172 explain select * from t1 where a='aaac';
173 explain select * from t1 where a='aaad';
174
175 # a known effect: table reload causes statistics to be updated:
176 flush tables;
177 explain select * from t1 where a='aaaa';
178 explain select * from t1 where a='aaab';
179 explain select * from t1 where a='aaac';
180 explain select * from t1 where a='aaad';
181
182 # Check if delete_all_rows() updates #hash_buckets
183 create table t2 as select * from t1;
184 delete from t1;
185 insert into t1 select * from t2;
186 explain select * from t1 where a='aaaa';
187 explain select * from t1 where a='aaab';
188 explain select * from t1 where a='aaac';
189 explain select * from t1 where a='aaad';
190 drop table t1, t2;
191
192
193 # Btree and hash index use costs.
194 create table t1 (
195 id int unsigned not null primary key auto_increment,
196 name varchar(20) not null,
197 index heap_idx(name),
198 index btree_idx using btree(name)
199 ) engine=heap;
200
201 create table t2 (
202 id int unsigned not null primary key auto_increment,
203 name varchar(20) not null,
204 index btree_idx using btree(name),
205 index heap_idx(name)
206 ) engine=heap;
207
208 insert into t1 (name) values ('Matt'), ('Lilu'), ('Corbin'), ('Carly'),
209 ('Suzy'), ('Hoppy'), ('Burrito'), ('Mimi'), ('Sherry'), ('Ben'), ('Phil'),
210 ('Emily'), ('Mike');
211 insert into t2 select * from t1;
212 explain select * from t1 where name='matt';
213 explain select * from t2 where name='matt';
214
215 explain select * from t1 where name='Lilu';
216 explain select * from t2 where name='Lilu';
217
218 explain select * from t1 where name='Phil';
219 explain select * from t2 where name='Phil';
220
221 explain select * from t1 where name='Lilu';
222 explain select * from t2 where name='Lilu';
223
224 insert into t1 (name) select name from t2;
225 insert into t1 (name) select name from t2;
226 insert into t1 (name) select name from t2;
227 insert into t1 (name) select name from t2;
228 insert into t1 (name) select name from t2;
229 insert into t1 (name) select name from t2;
230 flush tables;
231 select count(*) from t1 where name='Matt';
232 explain select * from t1 ignore index (btree_idx) where name='matt';
233 show index from t1;
234
235 show index from t1;
236
237 create table t3
238 (
239 a varchar(20) not null,
240 b varchar(20) not null,
241 key (a,b)
242 ) engine=heap;
243 insert into t3 select name, name from t1;
244 show index from t3;
245 show index from t3;
246
247 # test rec_per_key use for joins.
248 explain select * from t1 ignore key(btree_idx), t3 where t1.name='matt' and t3.a = concat('',t1.name) and t3.b=t1.name;
249
250 drop table t1, t2, t3;
251
252 # Fix for BUG#8371: wrong rec_per_key value for hash index on temporary table
253 create temporary table t1 ( a int, index (a) ) engine=memory;
254 insert into t1 values (1),(2),(3),(4),(5);
255 select a from t1 where a in (1,3);
256 explain select a from t1 where a in (1,3);
257 drop table t1;
258
259 --echo End of 4.1 tests
260
261 #
262 # Bug #27643: query failed : 1114 (The table '' is full)
263 #
264 # Check that HASH indexes disregard trailing spaces when comparing
265 # strings with binary collations
266
267 CREATE TABLE t1(col1 VARCHAR(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
268 col2 VARCHAR(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
269 UNIQUE KEY key1 USING HASH (col1, col2)) ENGINE=MEMORY;
270 INSERT INTO t1 VALUES('A', 'A');
271 --error ER_DUP_ENTRY
272 INSERT INTO t1 VALUES('A ', 'A ');
273 DROP TABLE t1;
274 CREATE TABLE t1(col1 VARCHAR(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
275 col2 VARCHAR(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
276 UNIQUE KEY key1 USING HASH (col1, col2)) ENGINE=MEMORY;
277 INSERT INTO t1 VALUES('A', 'A');
278 --error ER_DUP_ENTRY
279 INSERT INTO t1 VALUES('A ', 'A ');
280 DROP TABLE t1;
281
282 --echo End of 5.0 tests
283
284 --echo #
285 --echo # MDEV-568 (AKA LP BUG#1007981, AKA MySQL bug#44771)
286 --echo # Wrong result for a hash index look-up if the index is unique and
287 --echo # the key is NULL
288 --echo #
289 CREATE TABLE t1 ( pk INT PRIMARY KEY, val INT, UNIQUE KEY USING HASH(val)) ENGINE=MEMORY;
290
291 INSERT INTO t1 VALUES (1, NULL);
292 INSERT INTO t1 VALUES (2, NULL);
293 INSERT INTO t1 VALUES (3, 1);
294 INSERT INTO t1 VALUES (4, NULL);
295 EXPLAIN SELECT * FROM t1 WHERE val IS NULL;
296 SELECT * FROM t1 WHERE val IS NULL;
297 drop table t1;
298
299 --echo End of 5.2 tests
300
301 -- echo # bit index in heap tables
302
303 create table t1 (a bit(63) not null) engine=heap;
304 insert into t1 values (869751),(736494),(226312),(802616),(728912);
305 alter table t1 add unique uniq_id using HASH (a);
306 select 0+a from t1 where a > 736494;
307 explain select 0+a from t1 where a > 736494;
308 select 0+a from t1 where a = 736494;
309 explain select 0+a from t1 where a = 736494;
310 select 0+a from t1 where a=869751 or a=736494;
311 explain select 0+a from t1 where a=869751 or a=736494;
312 select 0+a from t1 where a in (869751,736494,226312,802616,728912);
313 explain select 0+a from t1 where a in (869751,736494,226312,802616,728912);
314 drop table t1;
315
316 --echo End of 5.3 tests
317
318 --echo #
319 --echo # Bug #55472: Assertion failed in heap_rfirst function of hp_rfirst.c
320 --echo # on DELETE statement
321 --echo #
322
323 CREATE TABLE t1 (col_int_nokey INT,
324 col_int_key INT,
325 INDEX(col_int_key) USING HASH) ENGINE = HEAP;
326 INSERT INTO t1 (col_int_nokey, col_int_key) VALUES (3, 0), (4, 0), (3, 1);
327
328 DELETE FROM t1 WHERE col_int_nokey = 5 ORDER BY col_int_key LIMIT 2;
329
330 DROP TABLE t1;
331
332 --echo #
333 --echo # Bug #1002564: Wrong result for a lookup query from a heap table
334 --echo #
335
336 CREATE TABLE t1 (c1 VARCHAR(10) NOT NULL, KEY i1 (c1(3))) ENGINE=MEMORY DEFAULT CHARSET=latin1;
337 INSERT INTO t1 VALUES ('foo1'), ('bar2'), ('baz3');
338 explain SELECT * FROM t1 WHERE c1='bar2';
339 SELECT * FROM t1 WHERE c1='bar2';
340 ALTER TABLE t1 DROP KEY i1, ADD KEY il (c1(3)) using btree;
341 explain SELECT * FROM t1 WHERE c1='bar2';
342 SELECT * FROM t1 WHERE c1='bar2';
343 DROP TABLE t1;
344
345 --echo #
346 --echo # End of 5.5 tests
347 --echo #
348
349 --echo #
350 --echo # MDEV-27406 Index on a HEAP table retains DESC attribute despite being hash
351 --echo #
352 create table t1 (a int, key(a desc)) engine=memory;
353 show index from t1;
354 show create table t1;
355 drop table t1;
356
357 --echo #
358 --echo # End of 10.8 tests
359 --echo #