home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win.exe / DATA1.CAB / Benchmark_Files / bench / test-select < prev    next >
Text File  |  2000-11-22  |  12KB  |  376 lines

  1. #!/my/gnu/bin/perl5
  2. # Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  3. #
  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; either
  7. # version 2 of the License, or (at your option) any later version.
  8. #
  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.
  13. #
  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,
  17. # MA 02111-1307, USA
  18. #
  19. # Test of selecting on keys that consist of many parts
  20. #
  21. ##################### Standard benchmark inits ##############################
  22.  
  23. use DBI;
  24. use Getopt::Long;
  25. use Benchmark;
  26.  
  27. $opt_loop_count=10000;
  28. $opt_medium_loop_count=1000;
  29. $opt_small_loop_count=10;
  30. $opt_regions=6;
  31. $opt_groups=100;
  32.  
  33. chomp($pwd = `pwd`); $pwd = "." if ($pwd eq '');
  34. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  35.  
  36. $columns=min($limits->{'max_columns'},500,($limits->{'query_size'}-50)/24,
  37.          $limits->{'max_conditions'}/2-3);
  38.  
  39. if ($opt_small_test)
  40. {
  41.   $opt_loop_count/=10;
  42.   $opt_medium_loop_count/=10;
  43.   $opt_small_loop_count/=10;
  44.   $opt_groups/=10;
  45. }
  46.  
  47. print "Testing the speed of selecting on keys that consist of many parts\n";
  48. print "The test-table has $opt_loop_count rows and the test is done with $columns ranges.\n\n";
  49.  
  50. ####
  51. ####  Connect and start timeing
  52. ####
  53.  
  54. $dbh = $server->connect();
  55. $start_time=new Benchmark;
  56.  
  57. ####
  58. #### Create needed tables
  59. ####
  60.  
  61. goto select_test if ($opt_skip_create);
  62.  
  63. print "Creating table\n";
  64. $dbh->do("drop table bench1");
  65.  
  66. do_many($dbh,$server->create("bench1",
  67.                  ["region char(1) NOT NULL",
  68.                   "idn integer(6) NOT NULL",
  69.                   "rev_idn integer(6) NOT NULL",
  70.                   "grp integer(6) NOT NULL"],
  71.                  ["primary key (region,idn)",
  72.                   "unique (region,rev_idn)",
  73.                   "unique (region,grp,idn)"]));
  74. if ($opt_lock_tables)
  75. {
  76.   do_query($dbh,"LOCK TABLES bench1 WRITE");
  77. }
  78.  
  79. if ($opt_fast && defined($server->{vacuum}))
  80. {
  81.   $server->vacuum(1,\$dbh);
  82. }
  83.  
  84. ####
  85. #### Insert $opt_loop_count records with
  86. #### region:    "A" -> "E"
  87. #### idn:     0 -> count
  88. #### rev_idn:    count -> 0,
  89. #### grp:    distributed values 0 - > count/100
  90. ####
  91.  
  92. print "Inserting $opt_loop_count rows\n";
  93.  
  94. $loop_time=new Benchmark;
  95. $query="insert into bench1 values (";
  96. $half_done=$opt_loop_count/2;
  97. for ($id=0,$rev_id=$opt_loop_count-1 ; $id < $opt_loop_count ; $id++,$rev_id--)
  98. {
  99.   $grp=$id*3 % $opt_groups;
  100.   $region=chr(65+$id%$opt_regions);
  101.   do_query($dbh,"$query'$region',$id,$rev_id,$grp)");
  102.   if ($id == $half_done)
  103.   {                # Test with different insert
  104.     $query="insert into bench1 (region,idn,rev_idn,grp) values (";
  105.   }
  106. }
  107.  
  108. $end_time=new Benchmark;
  109. print "Time to insert ($opt_loop_count): " .
  110.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  111.  
  112. if ($opt_lock_tables)
  113. {
  114.   do_query($dbh,"UNLOCK TABLES");
  115. }
  116.  
  117. if ($opt_fast && defined($server->{vacuum}))
  118. {
  119.   $server->vacuum(0,\$dbh,"bench1");
  120. }
  121.  
  122. if ($opt_lock_tables)
  123. {
  124.   do_query($dbh,"LOCK TABLES bench1 WRITE");
  125. }
  126.  
  127. ####
  128. #### Do some selects on the table
  129. ####
  130.  
  131. select_test:
  132.  
  133. print "Testing big selects on the table\n";
  134. $loop_time=new Benchmark;
  135. $rows=0;
  136. for ($i=0 ; $i < $opt_small_loop_count ; $i++)
  137. {
  138.   $grp=$i*11 % $opt_groups;
  139.   $region=chr(65+$i%($opt_regions+1));    # One larger to test misses
  140.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region'");
  141.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and idn=$i");
  142.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and rev_idn=$i");
  143.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and grp=$grp");
  144.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region>='B' and region<='C' and grp=$grp");
  145.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region>='B' and region<='E' and grp=$grp");
  146.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where grp=$grp"); # This is hard
  147. }
  148. $count=$opt_small_loop_count*7;
  149.  
  150. $end_time=new Benchmark;
  151. print "Time for select_big ($count:$rows): " .
  152.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  153.  
  154. # Test select with many OR's
  155.  
  156. $loop_time=new Benchmark;
  157. $tmpvar=0;
  158. $count=0;
  159. $estimated=0;
  160. $max_and_conditions=$limits->{'max_conditions'}/2;
  161. $rows=0;
  162.  
  163. for ($i=0 ; $i < $opt_small_loop_count ; $i++)
  164. {
  165.   $region=chr(65+$i%($opt_regions+1));    # One larger to test out-of-regions
  166.   $query="select * from bench1 where ";
  167.   $or_part="grp = 1";
  168.   $or_part2="region='A' and grp=1";
  169.  
  170.   for ($j=1 ; $j < $columns; $j++)
  171.   {
  172.     $tmpvar^= ((($tmpvar + 63) + $j)*3 % 100000);
  173.     $tmp=$tmpvar % $opt_groups;
  174.     $tmp_region=chr(65+$tmpvar%$opt_regions);
  175.     $or_part.=" or grp=$tmp";
  176.     if ($j < $max_and_conditions)
  177.     {
  178.       $or_part2.=" or region='$tmp_region' and grp=$tmp";
  179.     }
  180.   }
  181.   $or_part="region='$region' and ($or_part)";
  182.  
  183. # Same query, but use 'func_extra_in_num' instead.
  184.   if ($limits->{'func_extra_in_num'})
  185.   {
  186.     $in_part=$or_part;
  187.     $in_part=~ s/ = / IN \(/;
  188.     $in_part=~ s/ or grp=/,/g;
  189.     $in_part.= ")";
  190.     defined($found=fetch_all_rows($dbh,$query . $in_part)) || die $DBI::errstr;
  191.     $rows+=$found;
  192.     $count++;
  193.   }
  194.   for ($j=0; $j < 10 ; $j++)
  195.   {
  196.     $rows+=fetch_all_rows($dbh,$query . $or_part);
  197.     $rows+=fetch_all_rows($dbh,$query . $or_part2);
  198. # Do it a little harder by setting a extra range
  199.     $rows+=fetch_all_rows($dbh,"$query ($or_part) and idn < 50");
  200.     $rows+=fetch_all_rows($dbh,"$query (($or_part) or (region='A' and grp < 10)) and region <='B'")
  201.   }
  202.   $count+=$j*4;
  203.   $end_time=new Benchmark;
  204.   last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  205.                      $opt_small_loop_count));
  206. }
  207.  
  208. print_time($estimated);
  209. print " for select_range ($count:$rows): " .
  210.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  211.  
  212. #
  213. # Testing MIN() and MAX() on keys
  214. #
  215.  
  216. if ($limits->{'group_functions'})
  217. {
  218.   $loop_time=new Benchmark;
  219.   $count=0;
  220.   $estimated=0;
  221.   for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
  222.   {
  223.     $count+=7;
  224.     $grp=$tests*3 % $opt_groups;
  225.     $region=chr(65+$tests % $opt_regions);
  226.     if ($limits->{'group_func_sql_min_str'})
  227.     {
  228.       fetch_all_rows($dbh,"select min(region) from bench1");
  229.       fetch_all_rows($dbh,"select max(region) from bench1");
  230.       fetch_all_rows($dbh,"select min(region),max(region) from bench1");
  231.     }
  232.     fetch_all_rows($dbh,"select min(rev_idn) from bench1 where region='$region'");
  233.     fetch_all_rows($dbh,"select max(grp) from bench1 where region='$region'");
  234.     fetch_all_rows($dbh,"select max(idn) from bench1 where region='$region' and grp=$grp");
  235.     if ($limits->{'group_func_sql_min_str'})
  236.     {
  237.       fetch_all_rows($dbh,"select max(region) from bench1 where region<'$region'");
  238.     }
  239.     $end_time=new Benchmark;
  240.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,
  241.                        $tests+1, $opt_loop_count));
  242.   }
  243.   print_time($estimated);
  244.   print " for min_max_on_key ($count): " .
  245.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  246.  
  247.   $loop_time=new Benchmark;
  248.   $count=0;
  249.   $estimated=0;
  250.   for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
  251.   {
  252.     $count+=5;
  253.     $grp=$tests*3 % $opt_groups;
  254.     $region=chr(65+$tests % $opt_regions);
  255.     fetch_all_rows($dbh,"select count(*) from bench1 where region='$region'");
  256.     fetch_all_rows($dbh,"select count(*) from bench1 where region='$region' and grp=$grp");
  257.     fetch_all_rows($dbh,"select count(*) from bench1 where region>'$region'");
  258.     fetch_all_rows($dbh,"select count(*) from bench1 where region<='$region'");
  259.     fetch_all_rows($dbh,"select count(*) from bench1 where region='$region' and grp>$grp");
  260.     $end_time=new Benchmark;
  261.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,
  262.                        $tests+1, $opt_loop_count));
  263.   }
  264.   print_time($estimated);
  265.   print " for count_on_key ($count): " .
  266.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  267.   
  268.   $loop_time=new Benchmark;
  269.   $rows=0;
  270.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  271.   {
  272.     $rows+=fetch_all_rows($dbh,"select grp,count(*) from bench1 group by grp");
  273.   }
  274.   $end_time=new Benchmark;
  275.   print "Time for count_group_on_key_parts ($i:$rows): " .
  276.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  277. }
  278.  
  279. if ($limits->{'group_functions'})
  280. {
  281.   print "Testing count(distinct) on the table\n";
  282.   $loop_time=new Benchmark;
  283.   $rows=$estimated=$count=0;
  284.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  285.   {
  286.     $count+=2;
  287.     $rows+=fetch_all_rows($dbh,"select count(distinct region) from bench1");
  288.     $rows+=fetch_all_rows($dbh,"select count(distinct grp) from bench1");
  289.     $end_time=new Benchmark;
  290.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  291.                        $opt_medium_loop_count));
  292.   }
  293.   print_time($estimated);
  294.   print " for count_distinct ($count:$rows): " .
  295.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  296.  
  297.   $loop_time=new Benchmark;
  298.   $rows=$estimated=$count=0;
  299.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  300.   {
  301.     $count++;
  302.     $rows+=fetch_all_rows($dbh,"select region,count(distinct idn) from bench1 group by region");
  303.     $end_time=new Benchmark;
  304.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  305.                        $opt_medium_loop_count));
  306.   }
  307.   print_time($estimated);
  308.   print " for count_distinct_group_on_key ($count:$rows): " .
  309.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  310.  
  311.   $loop_time=new Benchmark;
  312.   $rows=$estimated=$count=0;
  313.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  314.   {
  315.     $count++;
  316.     $rows+=fetch_all_rows($dbh,"select grp,count(distinct idn) from bench1 group by grp");
  317.     $end_time=new Benchmark;
  318.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  319.                        $opt_medium_loop_count));
  320.   }
  321.   print_time($estimated);
  322.   print " for count_distinct_group_on_key_parts ($count:$rows): " .
  323.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  324.  
  325.   $loop_time=new Benchmark;
  326.   $rows=$estimated=$count=0;
  327.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  328.   {
  329.     $count++;
  330.     $rows+=fetch_all_rows($dbh,"select grp,count(distinct rev_idn) from bench1 group by grp");
  331.     $end_time=new Benchmark;
  332.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  333.                        $opt_medium_loop_count));
  334.   }
  335.   print_time($estimated);
  336.   print " for count_distinct_group ($count:$rows): " .
  337.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  338.  
  339.   $loop_time=new Benchmark;
  340.   $rows=$estimated=$count=0;
  341.   $test_count=$opt_medium_loop_count/10;
  342.   for ($i=0 ; $i < $test_count ; $i++)
  343.   {
  344.     $count++;
  345.     $rows+=fetch_all_rows($dbh,"select idn,count(distinct region) from bench1 group by idn");
  346.     $end_time=new Benchmark;
  347.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  348.                        $test_count));
  349.   }
  350.   print_time($estimated);
  351.   print " for count_distinct_big ($count:$rows): " .
  352.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  353. }
  354.  
  355. ####
  356. #### End of benchmark
  357. ####
  358.  
  359. if ($opt_lock_tables)
  360. {
  361.   do_query($dbh,"UNLOCK TABLES");
  362. }
  363. if (!$opt_skip_delete)
  364. {
  365.   do_query($dbh,"drop table bench1");
  366. }
  367.  
  368. if ($opt_fast && defined($server->{vacuum}))
  369. {
  370.   $server->vacuum(0,\$dbh);
  371. }
  372.  
  373. $dbh->disconnect;                # close connection
  374.  
  375. end_benchmark($start_time);
  376.