1
# Test Routine for Indexing in BlitzDB
4
drop table if exists t1, t2;
10
create table t1 (id int primary key, a int, b int) engine = blitzdb;
12
insert into t1 values (1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4);
13
insert into t1 values (5, 5, 5), (6, 6, 6), (7, 7, 7), (8, 8, 8);
14
insert into t1 values (9, 9, 9), (10, 10, 10), (11, 11, 11), (12, 12, 12);
15
insert into t1 values (13, 13, 13), (14, 14, 14), (15, 15, 15), (16, 16, 16);
17
# this statement should be 'optimized away'.
18
explain select count (*) from t1;
21
--error ER_WARN_DATA_OUT_OF_RANGE
22
insert into t1 values (2147483648, 1, 1);
26
insert into t1 values (1, 0, 0);
28
insert into t1 values (2, 0, 0);
30
insert into t1 values (3, 0, 0);
32
insert into t1 values (4, 0, 0);
34
# needle in a haystack
35
select * from t1 where id = 2;
36
select * from t1 where id = 4;
37
select * from t1 where id = 6;
40
update t1 set a = 70 where id = 7;
41
update t1 set a = 80 where id = 8;
42
select * from t1 order by id;
46
update t1 set id = 2 where id = 1;
48
update t1 set id = 17 where id = 1;
49
update t1 set id = 18 where id = 2;
50
update t1 set id = 19 where id = 3;
51
update t1 set id = 20 where id = 4;
52
update t1 set id = 21 where id = 5;
53
update t1 set id = 22 where id = 6;
54
update t1 set id = 23 where id = 7;
57
update t1 set id = 20 where id = 8;
59
# TODO: This is using filesort. Investigate why.
60
select * from t1 order by id;
63
select count (*) from t1;
64
delete from t1 where id = 8;
65
select count (*) from t1;
66
delete from t1 where id = 9;
67
select count (*) from t1;
68
delete from t1 where id = 18;
69
select count (*) from t1;
70
delete from t1 where id = 19;
71
select count (*) from t1;
72
select count (id) from t1;
73
select count (a) from t1;
74
select count (b) from t1;
75
select * from t1 order by id;
78
delete from t1 where id > 10 and id < 14;
79
select * from t1 order by id;
80
select count (*) from t1;
81
delete from t1 where id >= 20 and id <= 23;
82
select * from t1 order by id;
83
select count (*) from t1;
86
delete from t1 where a > 20;
87
select count(*) from t1;
91
# +---------------------+
92
# | PRIMARY KEY: BIGINT |
93
# +---------------------+
94
create table t1 (id bigint primary key, a int) engine = blitzdb;
97
insert into t1 values (2147483647, 1);
100
insert into t1 values (1.8e+18, 2);
103
--error ER_WARN_DATA_OUT_OF_RANGE
104
insert into t1 values (1.8e+19, 3);
106
insert into t1 values (1,1), (2,2), (3,3), (4,4);
109
update t1 set id = 4 where id = 1;
111
update t1 set id = 10 where id = 1;
112
update t1 set id = 20 where id = 2;
113
update t1 set id = 30 where id = 3;
114
update t1 set id = 40 where id = 4;
116
select count(*) from t1 where id < 10;
117
explain select id from t1 where id in (10, 20, 30, 40);
118
select id from t1 where id in (10, 20, 30, 40);
121
delete from t1 where id = 10;
122
delete from t1 where id = 20;
123
delete from t1 where id in (30, 40);
127
# +--------------------------------------+
128
# | PRIMARY KEY: INT with Auto Increment |
129
# +--------------------------------------+
130
create table t1 (id int primary key auto_increment, num int) engine = blitzdb;
131
insert into t1 (num) values (1);
133
insert into t1 (num) values (1);
134
insert into t1 (num) values (1);
135
insert into t1 (num) values (1);
136
select count(*) from t1;
138
select count(*) from t1;
139
insert into t1 (num) values (1);
140
insert into t1 (num) values (1);
141
insert into t1 (num) values (1);
142
insert into t1 (num) values (1);
143
select count(*) from t1;
144
select count(num) from t1;
148
create table t1 (id int primary key auto_increment) engine = blitzdb;
149
insert into t1 values (1), (2), (3), (4);
150
insert into t1 values (8), (9), (10), (11);
151
insert into t1 values (5), (7);
153
insert into t1 values (), (), (); # 12, 13, 14
157
# COUNT on a single auto column table
158
create table t1 (id int primary key auto_increment) engine = blitzdb;
159
insert into t1 values ();
160
insert into t1 values ();
161
insert into t1 values ();
162
insert into t1 values ();
163
select count(*) from t1;
164
delete from t1 where id = 1;
165
delete from t1 where id = 2;
166
select count(*) from t1;
167
insert into t1 values ();
168
insert into t1 values ();
169
select count(*) from t1;
172
# +---------------------+
173
# | PRIMARY KEY: DOUBLE |
174
# +---------------------+
175
create table t1 (id double primary key, a int) engine = blitzdb;
176
insert into t1 values (1.1, 1);
177
insert into t1 values (1.11, 2);
178
insert into t1 values (1.111, 3);
179
insert into t1 values (1.1111, 4);
180
insert into t1 values (2.2, 5);
181
insert into t1 values (2.22, 6);
182
insert into t1 values (2.222, 7);
183
insert into t1 values (2.2222, 8);
187
update t1 set id = 1.11 where id = 1.1;
189
update t1 set id = 2.22 where id = 2.2;
191
update t1 set id = 3.3 where id = 1.1;
192
update t1 set id = 3.33 where id = 1.11;
193
update t1 set id = 3.333 where id = 1.111;
194
update t1 set id = 3.3333 where id = 1.1111;
195
select * from t1 order by id;
198
update t1 set id = 2.2 where id = 3.3;
200
delete from t1 where id = 3.3;
201
delete from t1 where id = 3.33;
202
select * from t1 order by id;
205
# +----------------------+
206
# | PRIMARY KEY: VARCHAR |
207
# +----------------------+
208
create table t1 (id varchar(64) primary key, country varchar(64)) engine = blitzdb;
209
insert into t1 values ('amsterdam', 'netherlands');
210
insert into t1 values ('budapest', 'hungary');
211
insert into t1 values ('copenhagen', 'denmark');
212
insert into t1 values ('dublin', 'ireland');
213
insert into t1 values ('edinburgh', 'scotland');
214
insert into t1 values ('fukuoka', 'japan');
215
insert into t1 values ('geneva', 'switzerland');
217
select * from t1 order by id;
218
select country from t1 where id = 'dublin';
219
select country from t1 where id = 'geneva';
220
select country from t1 where id = 'amsterdam';
221
select country from t1 where id = 'non existent key';
222
select country from t1 where id = 'edinburgh';
223
select country from t1 where id = 'copenhagen';
224
select country from t1 where id = 'fukuoka';
225
select country from t1 where id = 'budapest';
226
select count (id) from t1;
229
update t1 set id = 'dublin' where id = 'geneva';
230
update t1 set id = 'berlin', country = 'germany' where id = 'budapest';
231
update t1 set id = 'london', country = 'england' where id = 'copenhagen';
232
update t1 set id = 'paris', country = 'france' where id = 'dublin';
234
explain select * from t1 where id = 'berlin';
235
select * from t1 where id = 'berlin';
236
select * from t1 where id = 'london';
237
select * from t1 where id = 'paris';
239
delete from t1 where id = 'geneva';
240
delete from t1 where id = 'fukuoka';
241
select count (id) from t1;
242
select count (*) from t1;
243
select * from t1 order by id;
246
delete from t1 where id < 'london';
247
select count (*) from t1;
248
select * from t1 order by id;
251
# Test HA_KEYTYPE_VARTEXT1 as a PRIMARY KEY
252
create table t1 (a varchar(10) primary key, b int) engine = blitzdb;
253
insert into t1 values ('aaa', 1), ('bbb', 2), ('ccc', 3), ('ddd', 4);
254
insert into t1 values ('eee', 5), ('fff', 6), ('ggg', 7), ('hhh', 8);
256
select * from t1 where a = 'aaa';
257
select * from t1 where a = 'bbb';
258
select * from t1 where a = 'ccc';
259
select * from t1 where a = 'ddd';
262
delete from t1 where a = 'ggg';
263
delete from t1 where a = 'hhh';
267
update t1 set a = 'ddd' where a = 'aaa';
269
update t1 set a = 'ccc' where a = 'bbb';
271
update t1 set a = 'zzz' where a = 'fff';
272
select count(*) from t1 where a = 'fff';
273
select * from t1 where a = 'zzz';
277
# +------------------+
278
# |PRIMARY KEY: DATE |
279
# +------------------+
280
create table t1 (a date primary key, b int, c varchar(32)) engine = blitzdb;
281
insert into t1 values ('1984-09-22', 22, 'twenty two');
282
insert into t1 values ('1984-09-23', 23, 'twenty three');
283
insert into t1 values ('1984-09-24', 24, 'twenty four');
284
insert into t1 values ('1984-09-25', 23, 'twenty five');
286
explain select * from t1 where a = '1984-09-22';
287
select * from t1 where a = '1984-09-22';
288
select * from t1 where a = '1984-09-23';
289
select * from t1 where a = '1984-09-24';
290
select * from t1 where a = '1984-09-25';
293
update t1 set a = '1984-09-22' where a = '1984-09-25';
295
update t1 set a = '19840922' where a = '1984-09-25';
297
update t1 set a = '2010-03-10' where a = '1984-09-22';
298
update t1 set a = '2010-03-11', b = 777, c = 'triple seven' where a = '1984-09-23';
299
select * from t1 order by a;
302
# Test basic index scan on INT primary keys.
303
create table t1 (a int primary key) engine = blitzdb;
304
insert into t1 values (1), (2), (3), (4), (5), (6);
307
explain select * from t1 order by a desc;
311
explain select * from t1 order by a desc;
312
select * from t1 order by a desc;
314
# write in random order
316
insert into t1 values (6), (1), (5), (3), (2), (4);
319
explain select * from t1;
324
explain select * from t1 order by a desc;
325
select * from t1 order by a desc;
327
# test aggregate functions
328
explain select max(a) from t1;
329
select max(a) from t1;
330
explain select min(a) from t1;
331
select min(a) from t1;
332
explain select sum(a) from t1;
333
select sum(a) from t1;
337
# Test basic forward index scan on BIGINT primary keys.
338
create table t1 (a bigint primary key) engine = blitzdb;
339
insert into t1 values (10), (20), (30), (40), (50), (60);
341
explain select * from t1;
343
select * from t1 order by a desc;
346
insert into t1 values (60), (10), (50), (30), (20), (40);
348
select * from t1 order by a desc;
351
# Test basic index scan on VARTEXT1 primary keys.
352
create table t1 (a varchar(10) primary key) engine = blitzdb;
353
insert into t1 values ('a'), ('b'), ('c'), ('d'), ('e');
355
explain select * from t1 order by a desc;
356
select * from t1 order by a desc;
358
insert into t1 values ('c'), ('a'), ('e'), ('b'), ('d');
362
# Test it with multi byte characters (Japanese).
363
insert into t1 values ('う'), ('お'), ('い'), ('あ'), ('え');
364
insert into t1 values ('こ'), ('け'), ('か'), ('く'), ('き');
365
explain select * from t1;
367
explain select * from t1 order by a desc;
368
select * from t1 order by a desc;
371
# Test basic index scan on VARTEXT2 primary keys.
372
create table t1 (a varchar(255) primary key) engine = blitzdb;
373
insert into t1 values ('a'), ('b'), ('c'), ('d'), ('e');
374
explain select * from t1;
376
select * from t1 order by a desc;
378
insert into t1 values ('c'), ('a'), ('e'), ('b'), ('d');
383
# Test UNIQUE index insertion
384
create table t1 (a int, unique index(a)) engine = blitzdb;
385
insert into t1 values (1), (2);
388
insert into t1 values (1);
390
insert into t1 values (2);
392
insert into t1 values (NULL);
393
insert into t1 values (3);
394
insert into t1 values (4);
397
select * from t1 where a = 1;
398
select * from t1 where a = 2;
399
select * from t1 where a = 3;
400
select * from t1 where a = 4;
401
select * from t1 where a is NULL;
404
create table t1 (a varchar(32), unique index(a)) engine = blitzdb;
405
insert into t1 values ('a'), ('b'), ('c');
408
insert into t1 values ('a');
410
insert into t1 values ('b');
412
insert into t1 values ('c');
414
insert into t1 values ('f'), ('e'), ('d');
417
select count(*) from t1;
418
explain select * from t1 where a = 'a';
419
select * from t1 where a = 'a';
420
select * from t1 where a = 'b';
421
select * from t1 where a = 'c';
422
select * from t1 where a = 'd';
423
select * from t1 where a = 'e';
424
select * from t1 where a = 'f';
427
delete from t1 where a = 'a';
428
delete from t1 where a = 'b';
429
select count(*) from t1;
431
delete from t1 where a = 'c';
432
delete from t1 where a = 'd';
433
delete from t1 where a = 'e';
434
delete from t1 where a = 'f';
435
select count(*) from t1;
442
create table t1 (a int, index(a)) engine = blitzdb;
443
insert into t1 values (1), (2), (3), (4);
444
insert into t1 values (1), (2), (3), (4);
449
insert into t1 values (NULL), (NULL);
450
insert into t1 values (NULL), (NULL);
453
select count(*) from t1 where a is NULL;
454
select * from t1 where a is NULL;
455
select * from t1 where a is not NULL;
457
# indexed needle in a haystack query
458
explain select * from t1 where a = 1;
459
select * from t1 where a = 1;
460
select * from t1 where a = 2;
461
select * from t1 where a = 3;
462
select * from t1 where a = 4;
464
# index based deletion
465
delete from t1 where a = 3;
466
select * from t1 where a = 3;
467
delete from t1 where a = 1;
468
select * from t1 where a = 1;
470
delete from t1 where a is NULL;
471
select * from t1 where a is NULL;
472
delete from t1 where a = 2;
473
select * from t1 where a = 2;
474
delete from t1 where a = 4;
475
select * from t1 where a = 4;
476
select count(*) from t1;
479
# +-----------------+
480
# | INDEX: VARTEXT1 |
481
# +-----------------+
482
create table t1 (a varchar(32), index(a)) engine = blitzdb;
483
insert into t1 values ('ccc'), ('bbb'), ('aaa');
484
insert into t1 values ('eee'), ('ddd'), ('fff');
487
select * from t1 where a = 'aaa';
488
select * from t1 where a = 'bbb';
489
select * from t1 where a = 'ccc';
490
select * from t1 where a = 'ddd';
491
select * from t1 where a = 'eee';
492
delete from t1 where a = 'ddd';
493
delete from t1 where a = 'eee';
495
select count(*) from t1;
498
# +------------------------------+
499
# | UNIQUE INDEX: DUPLICATE NULL |
500
# +------------------------------+
501
create table t1 (a int, b int, unique index(a)) engine = blitzdb;
502
insert into t1 values (NULL, 1), (NULL, 2), (NULL, 3), (NULL, 4);
504
select * from t1 where a is NULL;
505
insert into t1 values (1, 5), (2, 6);
507
insert into t1 values (1, 7), (1, 8);
508
select * from t1 where a is not NULL;
509
delete from t1 where a is NULL;
511
select count(*) from t1;
514
# +-----------------+
515
# | INDEX: GROUP BY |
516
# +-----------------+
519
lastname varchar(64),
520
description varchar(255),
525
insert into t1 values (1, "Schwartz", "Flight", 1500);
526
insert into t1 values (2, "Hayes", "Computer Equipment", 400);
527
insert into t1 values (3, "Lawrence", "Text Books", 220);
528
insert into t1 values (4, "Smith", "Weaponry", 45500);
529
insert into t1 values (5, "Yamada", "Dinner", 120);
530
insert into t1 values (6, "Smith", "Lunch", 30);
531
insert into t1 values (7, "Hayes", "Lunch", 30);
532
insert into t1 values (8, "Kinoshita", "Computer Equipment", 3740);
535
select sum(price) from t1;
536
select lastname, sum(price) from t1 group by lastname;
540
# +-----------------------+
541
# | COMPOSITE INDEX CHECK |
542
# +-----------------------+
544
--error ER_CANT_CREATE_TABLE
545
create table t1 (a int, b int, c int, d int, primary key(a, b)) engine = blitzdb;
546
--error ER_CANT_CREATE_TABLE
547
create table t1 (a int, b int, c int, d int, primary key(a, b, c)) engine = blitzdb;
548
--error ER_CANT_CREATE_TABLE
549
create table t1 (a int, b int, c int, d int, index(a, b)) engine = blitzdb;
550
--error ER_CANT_CREATE_TABLE
551
create table t1 (a int, b int, c int, d int, index(a, b, c)) engine = blitzdb;
552
--error ER_CANT_CREATE_TABLE
553
create table t1 (a int, b int, c int, d int, unique(a, b)) engine = blitzdb;
554
--error ER_CANT_CREATE_TABLE
555
create table t1 (a int, b int, c int, d int, unique(a, b, c)) engine = blitzdb;
557
# +------------------------------------------+
558
# | LARGE KEYS : Testcase from Patrick Crews |
559
# +------------------------------------------+
562
`col_varchar_10_key` varchar(10),
564
`col_varchar_1024_key` varchar(1024),
565
pk integer auto_increment,
566
`col_varchar_10` varchar(10),
568
`col_varchar_1024` varchar(1024),
569
key (`col_varchar_10_key`),
571
key (`col_varchar_1024_key`),
575
insert /*! IGNORE */ into t1 values ('x', NULL, 'keyone', NULL, 'could', 1322188800, 'I\'m');
576
insert /*! IGNORE */ into t1 values ('y', NULL, 'keytwo', NULL, 'could', 1322188800, 'I\'m');
577
insert /*! IGNORE */ into t1 values ('z', NULL, 'keythree', NULL, 'could', 1322188800, 'I\'m');