#!/usr/bin/env perl # Copyright (c) 2000, 2001, 2003, 2006 MySQL AB, 2009 Sun Microsystems, Inc. # Use is subject to license terms. # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Library General Public # License as published by the Free Software Foundation; version 2 # of the License. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Library General Public License for more details. # # You should have received a copy of the GNU Library General Public # License along with this library; if not, write to the Free # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, # MA 02110-1335 USA # # Test of selecting on keys that consist of many parts # ##################### Standard benchmark inits ##############################
use Cwd;
use DBI;
use Getopt::Long;
use Benchmark;
if ($opt_small_test)
{
$opt_loop_count/=10;
$opt_medium_loop_count/=10;
$opt_small_loop_count/=10;
$opt_groups/=10;
}
print "Testing the speed of selecting on keys that consist of many parts\n";
print "The test-table has $opt_loop_count rows and the test is done with $columns ranges.\n\n";
if ($opt_lock_tables)
{
do_query($dbh,"UNLOCK TABLES");
}
if ($opt_fast && defined($server->{vacuum}))
{
$server->vacuum(0,\$dbh,"bench1");
}
if ($opt_lock_tables)
{
do_query($dbh,"LOCK TABLES bench1 WRITE");
}
#### #### Do some selects on the table ####
select_test:
if ($limits->{'group_functions'})
{
my ($tmp); $tmp=1000;
print "Test if the database has a query cache\n";
# First ensure that the table is read into memory
fetch_all_rows($dbh,"select sum(idn+$tmp),sum(rev_idn-$tmp) from bench1");
$loop_time=new Benchmark; for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
{
fetch_all_rows($dbh,"select sum(idn+100),sum(rev_idn-100) from bench1");
}
$end_time=new Benchmark;
print "Time for select_cache ($opt_loop_count): " .
timestr(timediff($end_time, $loop_time),"all") . "\n\n";
# If the database has a query cache, the following loop should be much # slower than the previous loop
$loop_time=new Benchmark; for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
{
fetch_all_rows($dbh,"select sum(idn+$tests),sum(rev_idn-$tests) from bench1");
}
$end_time=new Benchmark;
print "Time for select_cache2 ($opt_loop_count): " .
timestr(timediff($end_time, $loop_time),"all") . "\n\n";
}
print "Testing big selects on the table\n";
$loop_time=new Benchmark;
$rows=0; for ($i=0 ; $i < $opt_small_loop_count ; $i++)
{
$grp=$i*11 % $opt_groups;
$region=chr(65+$i%($opt_regions+1)); # One larger to test misses
$rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region'");
$rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and idn=$i");
$rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and rev_idn=$i");
$rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and grp=$grp");
$rows+=fetch_all_rows($dbh,"select idn from bench1 where region>='B' and region<='C' and grp=$grp");
$rows+=fetch_all_rows($dbh,"select idn from bench1 where region>='B' and region<='E' and grp=$grp");
$rows+=fetch_all_rows($dbh,"select idn from bench1 where grp=$grp"); # This is hard
}
$count=$opt_small_loop_count*7;
for ($i=0 ; $i < $opt_small_loop_count ; $i++)
{
$region=chr(65+$i%($opt_regions+1)); # One larger to test out-of-regions
$query="select * from bench1 where ";
$or_part="grp = 1";
$or_part2="region='A' and grp=1";
for ($j=1 ; $j < $columns; $j++)
{
$tmpvar^= ((($tmpvar + 63) + $j)*3 % 100000);
$tmp=$tmpvar % $opt_groups;
$tmp_region=chr(65+$tmpvar%$opt_regions);
$or_part.=" or grp=$tmp"; if ($j < $max_and_conditions)
{
$or_part2.=" or region='$tmp_region' and grp=$tmp";
}
}
$or_part="region='$region' and ($or_part)";
# Same query, but use 'func_extra_in_num' instead. if ($limits->{'func_extra_in_num'})
{
$in_part=$or_part;
$in_part=~ s/ = / IN \(/;
$in_part=~ s/ or grp=/,/g;
$in_part.= ")";
defined($found=fetch_all_rows($dbh,$query . $in_part)) || die $DBI::errstr;
$rows+=$found;
$count++;
} for ($j=0; $j < 10 ; $j++)
{
$rows+=fetch_all_rows($dbh,$query . $or_part);
$rows+=fetch_all_rows($dbh,$query . $or_part2); # Do it a little harder by setting a extra range
$rows+=fetch_all_rows($dbh,"$query ($or_part) and idn < 50");
$rows+=fetch_all_rows($dbh,"$query (($or_part) or (region='A' and grp < 10)) and region <='B'")
}
$count+=$j*4;
$end_time=new Benchmark;
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
$opt_small_loop_count));
}
if ($limits->{'group_functions'} && $limits->{'order_by_unused'})
{
$loop_time=new Benchmark;
$count=0;
$estimated=0; for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
{
$count+=7;
$grp=$tests*3 % $opt_groups;
$region=chr(65+$tests % $opt_regions); if ($limits->{'group_func_sql_min_str'})
{
fetch_all_rows($dbh,"select min(region) from bench1");
fetch_all_rows($dbh,"select max(region) from bench1");
fetch_all_rows($dbh,"select min(region),max(region) from bench1");
}
fetch_all_rows($dbh,"select min(rev_idn) from bench1 where region='$region'");
fetch_all_rows($dbh,"select max(grp) from bench1 where region='$region'");
fetch_all_rows($dbh,"select max(idn) from bench1 where region='$region' and grp=$grp"); if ($limits->{'group_func_sql_min_str'})
{
fetch_all_rows($dbh,"select max(region) from bench1 where region<'$region'");
}
$end_time=new Benchmark;
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,
$tests+1, $opt_loop_count));
}
print_time($estimated);
print " for min_max_on_key ($count): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
$loop_time=new Benchmark;
$count=0;
$estimated=0; for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
{
$count+=5;
$grp=$tests*3 % $opt_groups;
$region=chr(65+$tests % $opt_regions);
fetch_all_rows($dbh,"select count(*) from bench1 where region='$region'");
fetch_all_rows($dbh,"select count(*) from bench1 where region='$region' and grp=$grp");
fetch_all_rows($dbh,"select count(*) from bench1 where region>'$region'");
fetch_all_rows($dbh,"select count(*) from bench1 where region<='$region'");
fetch_all_rows($dbh,"select count(*) from bench1 where region='$region' and grp>$grp");
$end_time=new Benchmark;
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,
$tests+1, $opt_loop_count));
}
print_time($estimated);
print " for count_on_key ($count): " .
timestr(timediff($end_time, $loop_time),"all") . "\n\n";
}
if ($limits->{'group_functions'})
{
$loop_time=new Benchmark;
$rows=0; for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$rows+=fetch_all_rows($dbh,"select grp,count(*) from bench1 group by grp");
}
$end_time=new Benchmark;
print "Time for count_group_on_key_parts ($i:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
$loop_time=new Benchmark;
$rows=0; for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$rows+=fetch_all_rows($dbh,"select grp_no_key,count(*) from bench1 group by grp_no_key");
}
$end_time=new Benchmark;
print "Time for count_group ($i:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
$loop_time=new Benchmark;
$rows=0; for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$rows+=fetch_all_rows($dbh,"select grp_no_key,count(*) from bench1 group by grp_no_key order by grp_no_key");
}
$end_time=new Benchmark;
if ($limits->{'order_by_null'})
{
$loop_time=new Benchmark;
$rows=0; for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$rows+=fetch_all_rows($dbh,"select grp_no_key,count(*) from bench1 group by grp_no_key order by null");
}
$end_time=new Benchmark;
print "Time for count_group_order_null ($i:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
}
$loop_time=new Benchmark;
$rows=0; for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$rows+=fetch_all_rows($dbh,"select grp_no_key,count(*) as cnt from bench1 group by grp_no_key order by cnt");
}
$end_time=new Benchmark;
print "Time for count_group_with_order ($i:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n\n";
}
if ($limits->{'group_distinct_functions'})
{
print "Testing count(distinct) on the table\n";
$loop_time=new Benchmark;
$rows=$estimated=$count=0; for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$count++;
$rows+=fetch_all_rows($dbh,"select count(distinct region) from bench1");
$end_time=new Benchmark;
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
$opt_medium_loop_count));
}
print_time($estimated);
print " for count_distinct_key_prefix ($count:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
$loop_time=new Benchmark;
$rows=$estimated=$count=0; for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$count++;
$rows+=fetch_all_rows($dbh,"select count(distinct grp) from bench1");
$end_time=new Benchmark;
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
$opt_medium_loop_count));
}
print_time($estimated);
print " for count_distinct ($count:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
# Workaround mimer's behavior if ($limits->{'multi_distinct'})
{
$loop_time=new Benchmark;
$rows=$estimated=$count=0; for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$count++;
$rows+=fetch_all_rows($dbh,"select count(distinct grp),count(distinct rev_idn) from bench1");
$end_time=new Benchmark;
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
$opt_medium_loop_count));
}
print_time($estimated);
print " for count_distinct_2 ($count:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
}
$loop_time=new Benchmark;
$rows=$estimated=$count=0; for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$count++;
$rows+=fetch_all_rows($dbh,"select region,count(distinct idn) from bench1 group by region");
$end_time=new Benchmark;
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
$opt_medium_loop_count));
}
print_time($estimated);
print " for count_distinct_group_on_key ($count:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
$loop_time=new Benchmark;
$rows=$estimated=$count=0; for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$count++;
$rows+=fetch_all_rows($dbh,"select grp,count(distinct idn) from bench1 group by grp");
$end_time=new Benchmark;
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
$opt_medium_loop_count));
}
print_time($estimated);
print " for count_distinct_group_on_key_parts ($count:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
$loop_time=new Benchmark;
$rows=$estimated=$count=0; for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
{
$count++;
$rows+=fetch_all_rows($dbh,"select grp,count(distinct rev_idn) from bench1 group by grp");
$end_time=new Benchmark;
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
$opt_medium_loop_count));
}
print_time($estimated);
print " for count_distinct_group ($count:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
$loop_time=new Benchmark;
$rows=$estimated=$count=0;
$test_count=$opt_medium_loop_count/10; for ($i=0 ; $i < $test_count ; $i++)
{
$count++;
$rows+=fetch_all_rows($dbh,"select idn,count(distinct region) from bench1 group by idn");
$end_time=new Benchmark;
last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
$test_count));
}
print_time($estimated);
print " for count_distinct_big ($count:$rows): " .
timestr(timediff($end_time, $loop_time),"all") . "\n";
}
#### #### End of benchmark ####
if ($opt_lock_tables)
{
do_query($dbh,"UNLOCK TABLES");
} if (!$opt_skip_delete)
{
do_query($dbh,"drop table bench1" . $server->{'drop_attr'});
}
if ($opt_fast && defined($server->{vacuum}))
{
$server->vacuum(0,\$dbh);
}
$dbh->disconnect; # close connection
end_benchmark($start_time);
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.20 Sekunden
(vorverarbeitet am 2026-10-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.