2
# Copyright (C) 2000-2001, 2003 MySQL AB
4
# This library is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU Library General Public
6
# License as published by the Free Software Foundation; version 2
9
# This library is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
# Library General Public License for more details.
14
# You should have received a copy of the GNU Library General Public
15
# License along with this library; if not, write to the Free
16
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19
# Test of selecting on keys that consist of many parts
21
##################### Standard benchmark inits ##############################
28
$opt_loop_count=10000;
29
$opt_medium_loop_count=1000;
30
$opt_small_loop_count=10;
34
$pwd = cwd(); $pwd = "." if ($pwd eq '');
35
require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
37
$columns=min($limits->{'max_columns'},500,($limits->{'query_size'}-50)/24,
38
$limits->{'max_conditions'}/2-3);
43
$opt_medium_loop_count/=10;
44
$opt_small_loop_count/=10;
48
print "Testing the speed of selecting on keys that consist of many parts\n";
49
print "The test-table has $opt_loop_count rows and the test is done with $columns ranges.\n\n";
52
#### Connect and start timeing
55
$dbh = $server->connect();
56
$start_time=new Benchmark;
59
#### Create needed tables
62
goto select_test if ($opt_skip_create);
64
print "Creating table\n";
65
$dbh->do("drop table bench1" . $server->{'drop_attr'});
67
do_many($dbh,$server->create("bench1",
68
["region char(1) NOT NULL",
69
"idn integer NOT NULL",
70
"rev_idn integer NOT NULL",
71
"grp integer NOT NULL"],
72
["primary key (region,idn)",
73
"unique (region,rev_idn)",
74
"unique (region,grp,idn)"]));
77
do_query($dbh,"LOCK TABLES bench1 WRITE");
80
if ($opt_fast && defined($server->{vacuum}))
82
$server->vacuum(1,\$dbh);
86
#### Insert $opt_loop_count records with
87
#### region: "A" -> "E"
89
#### rev_idn: count -> 0,
90
#### grp: distributed values 0 - > count/100
93
print "Inserting $opt_loop_count rows\n";
95
$loop_time=new Benchmark;
97
if ($opt_fast && $server->{transactions})
99
$dbh->{AutoCommit} = 0;
102
$query="insert into bench1 values (";
103
$half_done=$opt_loop_count/2;
104
for ($id=0,$rev_id=$opt_loop_count-1 ; $id < $opt_loop_count ; $id++,$rev_id--)
106
$grp=$id*3 % $opt_groups;
107
$region=chr(65+$id%$opt_regions);
108
do_query($dbh,"$query'$region',$id,$rev_id,$grp)");
109
if ($id == $half_done)
110
{ # Test with different insert
111
$query="insert into bench1 (region,idn,rev_idn,grp) values (";
115
if ($opt_fast && $server->{transactions})
118
$dbh->{AutoCommit} = 1;
121
$end_time=new Benchmark;
122
print "Time to insert ($opt_loop_count): " .
123
timestr(timediff($end_time, $loop_time),"all") . "\n\n";
125
if ($opt_lock_tables)
127
do_query($dbh,"UNLOCK TABLES");
130
if ($opt_fast && defined($server->{vacuum}))
132
$server->vacuum(0,\$dbh,"bench1");
135
if ($opt_lock_tables)
137
do_query($dbh,"LOCK TABLES bench1 WRITE");
141
#### Do some selects on the table
146
if ($limits->{'group_functions'})
148
my ($tmp); $tmp=1000;
149
print "Test if the database has a query cache\n";
151
# First ensure that the table is read into memory
152
fetch_all_rows($dbh,"select sum(idn+$tmp),sum(rev_idn-$tmp) from bench1");
154
$loop_time=new Benchmark;
155
for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
157
fetch_all_rows($dbh,"select sum(idn+100),sum(rev_idn-100) from bench1");
159
$end_time=new Benchmark;
160
print "Time for select_cache ($opt_loop_count): " .
161
timestr(timediff($end_time, $loop_time),"all") . "\n\n";
163
# If the database has a query cache, the following loop should be much
164
# slower than the previous loop
166
$loop_time=new Benchmark;
167
for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
169
fetch_all_rows($dbh,"select sum(idn+$tests),sum(rev_idn-$tests) from bench1");
171
$end_time=new Benchmark;
172
print "Time for select_cache2 ($opt_loop_count): " .
173
timestr(timediff($end_time, $loop_time),"all") . "\n\n";
177
print "Testing big selects on the table\n";
178
$loop_time=new Benchmark;
180
for ($i=0 ; $i < $opt_small_loop_count ; $i++)
182
$grp=$i*11 % $opt_groups;
183
$region=chr(65+$i%($opt_regions+1)); # One larger to test misses
184
$rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region'");
185
$rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and idn=$i");
186
$rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and rev_idn=$i");
187
$rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and grp=$grp");
188
$rows+=fetch_all_rows($dbh,"select idn from bench1 where region>='B' and region<='C' and grp=$grp");
189
$rows+=fetch_all_rows($dbh,"select idn from bench1 where region>='B' and region<='E' and grp=$grp");
190
$rows+=fetch_all_rows($dbh,"select idn from bench1 where grp=$grp"); # This is hard
192
$count=$opt_small_loop_count*7;
194
$end_time=new Benchmark;
195
print "Time for select_big ($count:$rows): " .
196
timestr(timediff($end_time, $loop_time),"all") . "\n";
198
# Test select with many OR's
200
$loop_time=new Benchmark;
204
$max_and_conditions=$limits->{'max_conditions'}/2;
207
for ($i=0 ; $i < $opt_small_loop_count ; $i++)
209
$region=chr(65+$i%($opt_regions+1)); # One larger to test out-of-regions
210
$query="select * from bench1 where ";
212
$or_part2="region='A' and grp=1";
214
for ($j=1 ; $j < $columns; $j++)
216
$tmpvar^= ((($tmpvar + 63) + $j)*3 % 100000);
217
$tmp=$tmpvar % $opt_groups;
218
$tmp_region=chr(65+$tmpvar%$opt_regions);
219
$or_part.=" or grp=$tmp";
220
if ($j < $max_and_conditions)
222
$or_part2.=" or region='$tmp_region' and grp=$tmp";
225
$or_part="region='$region' and ($or_part)";
227
# Same query, but use 'func_extra_in_num' instead.
228
if ($limits->{'func_extra_in_num'})
231
$in_part=~ s/ = / IN \(/;
232
$in_part=~ s/ or grp=/,/g;
234
defined($found=fetch_all_rows($dbh,$query . $in_part)) || die $DBI::errstr;
238
for ($j=0; $j < 10 ; $j++)
240
$rows+=fetch_all_rows($dbh,$query . $or_part);
241
$rows+=fetch_all_rows($dbh,$query . $or_part2);
242
# Do it a little harder by setting a extra range
243
$rows+=fetch_all_rows($dbh,"$query ($or_part) and idn < 50");
244
$rows+=fetch_all_rows($dbh,"$query (($or_part) or (region='A' and grp < 10)) and region <='B'")
247
$end_time=new Benchmark;
248
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
249
$opt_small_loop_count));
252
print_time($estimated);
253
print " for select_range ($count:$rows): " .
254
timestr(timediff($end_time, $loop_time),"all") . "\n";
257
# Testing MIN() and MAX() on keys
260
if ($limits->{'group_functions'} && $limits->{'order_by_unused'})
262
$loop_time=new Benchmark;
265
for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
268
$grp=$tests*3 % $opt_groups;
269
$region=chr(65+$tests % $opt_regions);
270
if ($limits->{'group_func_sql_min_str'})
272
fetch_all_rows($dbh,"select min(region) from bench1");
273
fetch_all_rows($dbh,"select max(region) from bench1");
274
fetch_all_rows($dbh,"select min(region),max(region) from bench1");
276
fetch_all_rows($dbh,"select min(rev_idn) from bench1 where region='$region'");
278
fetch_all_rows($dbh,"select max(grp) from bench1 where region='$region'");
279
fetch_all_rows($dbh,"select max(idn) from bench1 where region='$region' and grp=$grp");
280
if ($limits->{'group_func_sql_min_str'})
282
fetch_all_rows($dbh,"select max(region) from bench1 where region<'$region'");
284
$end_time=new Benchmark;
285
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,
286
$tests+1, $opt_loop_count));
288
print_time($estimated);
289
print " for min_max_on_key ($count): " .
290
timestr(timediff($end_time, $loop_time),"all") . "\n";
292
$loop_time=new Benchmark;
295
for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
298
$grp=$tests*3 % $opt_groups;
299
$region=chr(65+$tests % $opt_regions);
300
fetch_all_rows($dbh,"select count(*) from bench1 where region='$region'");
301
fetch_all_rows($dbh,"select count(*) from bench1 where region='$region' and grp=$grp");
302
fetch_all_rows($dbh,"select count(*) from bench1 where region>'$region'");
303
fetch_all_rows($dbh,"select count(*) from bench1 where region<='$region'");
304
fetch_all_rows($dbh,"select count(*) from bench1 where region='$region' and grp>$grp");
305
$end_time=new Benchmark;
306
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,
307
$tests+1, $opt_loop_count));
309
print_time($estimated);
310
print " for count_on_key ($count): " .
311
timestr(timediff($end_time, $loop_time),"all") . "\n\n";
315
if ($limits->{'group_functions'})
317
$loop_time=new Benchmark;
319
for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
321
$rows+=fetch_all_rows($dbh,"select grp,count(*) from bench1 group by grp");
323
$end_time=new Benchmark;
324
print "Time for count_group_on_key_parts ($i:$rows): " .
325
timestr(timediff($end_time, $loop_time),"all") . "\n";
328
if ($limits->{'group_distinct_functions'})
330
print "Testing count(distinct) on the table\n";
331
$loop_time=new Benchmark;
332
$rows=$estimated=$count=0;
333
for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
336
$rows+=fetch_all_rows($dbh,"select count(distinct region) from bench1");
337
$end_time=new Benchmark;
338
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
339
$opt_medium_loop_count));
341
print_time($estimated);
342
print " for count_distinct_key_prefix ($count:$rows): " .
343
timestr(timediff($end_time, $loop_time),"all") . "\n";
345
$loop_time=new Benchmark;
346
$rows=$estimated=$count=0;
347
for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
350
$rows+=fetch_all_rows($dbh,"select count(distinct grp) from bench1");
351
$end_time=new Benchmark;
352
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
353
$opt_medium_loop_count));
355
print_time($estimated);
356
print " for count_distinct ($count:$rows): " .
357
timestr(timediff($end_time, $loop_time),"all") . "\n";
359
# Workaround mimer's behavior
360
if ($limits->{'multi_distinct'})
362
$loop_time=new Benchmark;
363
$rows=$estimated=$count=0;
364
for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
367
$rows+=fetch_all_rows($dbh,"select count(distinct grp),count(distinct rev_idn) from bench1");
368
$end_time=new Benchmark;
369
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
370
$opt_medium_loop_count));
372
print_time($estimated);
373
print " for count_distinct_2 ($count:$rows): " .
374
timestr(timediff($end_time, $loop_time),"all") . "\n";
377
$loop_time=new Benchmark;
378
$rows=$estimated=$count=0;
379
for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
382
$rows+=fetch_all_rows($dbh,"select region,count(distinct idn) from bench1 group by region");
383
$end_time=new Benchmark;
384
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
385
$opt_medium_loop_count));
387
print_time($estimated);
388
print " for count_distinct_group_on_key ($count:$rows): " .
389
timestr(timediff($end_time, $loop_time),"all") . "\n";
391
$loop_time=new Benchmark;
392
$rows=$estimated=$count=0;
393
for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
396
$rows+=fetch_all_rows($dbh,"select grp,count(distinct idn) from bench1 group by grp");
397
$end_time=new Benchmark;
398
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
399
$opt_medium_loop_count));
401
print_time($estimated);
402
print " for count_distinct_group_on_key_parts ($count:$rows): " .
403
timestr(timediff($end_time, $loop_time),"all") . "\n";
405
$loop_time=new Benchmark;
406
$rows=$estimated=$count=0;
407
for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
410
$rows+=fetch_all_rows($dbh,"select grp,count(distinct rev_idn) from bench1 group by grp");
411
$end_time=new Benchmark;
412
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
413
$opt_medium_loop_count));
415
print_time($estimated);
416
print " for count_distinct_group ($count:$rows): " .
417
timestr(timediff($end_time, $loop_time),"all") . "\n";
419
$loop_time=new Benchmark;
420
$rows=$estimated=$count=0;
421
$test_count=$opt_medium_loop_count/10;
422
for ($i=0 ; $i < $test_count ; $i++)
425
$rows+=fetch_all_rows($dbh,"select idn,count(distinct region) from bench1 group by idn");
426
$end_time=new Benchmark;
427
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
430
print_time($estimated);
431
print " for count_distinct_big ($count:$rows): " .
432
timestr(timediff($end_time, $loop_time),"all") . "\n";
436
#### End of benchmark
439
if ($opt_lock_tables)
441
do_query($dbh,"UNLOCK TABLES");
443
if (!$opt_skip_delete)
445
do_query($dbh,"drop table bench1" . $server->{'drop_attr'});
448
if ($opt_fast && defined($server->{vacuum}))
450
$server->vacuum(0,\$dbh);
453
$dbh->disconnect; # close connection
455
end_benchmark($start_time);