home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 July & August / PCWorld_2003-07-08_cd.bin / Software / Vyzkuste / mysql / data1.cab / Development / bench / test-select < prev    next >
Text File  |  2003-05-17  |  14KB  |  454 lines

  1. #!/usr/bin/perl
  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" . $server->{'drop_attr'});
  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.  
  96. if ($opt_fast && $server->{transactions})
  97. {
  98.   $dbh->{AutoCommit} = 0;
  99. }
  100.  
  101. $query="insert into bench1 values (";
  102. $half_done=$opt_loop_count/2;
  103. for ($id=0,$rev_id=$opt_loop_count-1 ; $id < $opt_loop_count ; $id++,$rev_id--)
  104. {
  105.   $grp=$id*3 % $opt_groups;
  106.   $region=chr(65+$id%$opt_regions);
  107.   do_query($dbh,"$query'$region',$id,$rev_id,$grp)");
  108.   if ($id == $half_done)
  109.   {                # Test with different insert
  110.     $query="insert into bench1 (region,idn,rev_idn,grp) values (";
  111.   }
  112. }
  113.  
  114. if ($opt_fast && $server->{transactions})
  115. {
  116.   $dbh->commit;
  117.   $dbh->{AutoCommit} = 1;
  118. }
  119.  
  120. $end_time=new Benchmark;
  121. print "Time to insert ($opt_loop_count): " .
  122.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  123.  
  124. if ($opt_lock_tables)
  125. {
  126.   do_query($dbh,"UNLOCK TABLES");
  127. }
  128.  
  129. if ($opt_fast && defined($server->{vacuum}))
  130. {
  131.   $server->vacuum(0,\$dbh,"bench1");
  132. }
  133.  
  134. if ($opt_lock_tables)
  135. {
  136.   do_query($dbh,"LOCK TABLES bench1 WRITE");
  137. }
  138.  
  139. ####
  140. #### Do some selects on the table
  141. ####
  142.  
  143. select_test:
  144.  
  145. if ($limits->{'group_functions'})
  146. {
  147.   my ($tmp); $tmp=1000;
  148.   print "Test if the database has a query cache\n";
  149.  
  150.   # First ensure that the table is read into memory
  151.   fetch_all_rows($dbh,"select sum(idn+$tmp),sum(rev_idn-$tmp) from bench1");
  152.  
  153.   $loop_time=new Benchmark;
  154.   for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
  155.   {
  156.     fetch_all_rows($dbh,"select sum(idn+100),sum(rev_idn-100) from bench1");
  157.   }
  158.   $end_time=new Benchmark;
  159.   print "Time for select_cache ($opt_loop_count): " .
  160.      timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  161.  
  162.   # If the database has a query cache, the following loop should be much
  163.   # slower than the previous loop
  164.  
  165.   $loop_time=new Benchmark;
  166.   for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
  167.   {
  168.     fetch_all_rows($dbh,"select sum(idn+$tests),sum(rev_idn-$tests) from bench1");
  169.   }
  170.   $end_time=new Benchmark;
  171.   print "Time for select_cache2 ($opt_loop_count): " .
  172.      timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  173. }
  174.  
  175.  
  176. print "Testing big selects on the table\n";
  177. $loop_time=new Benchmark;
  178. $rows=0;
  179. for ($i=0 ; $i < $opt_small_loop_count ; $i++)
  180. {
  181.   $grp=$i*11 % $opt_groups;
  182.   $region=chr(65+$i%($opt_regions+1));    # One larger to test misses
  183.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region'");
  184.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and idn=$i");
  185.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and rev_idn=$i");
  186.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region='$region' and grp=$grp");
  187.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region>='B' and region<='C' and grp=$grp");
  188.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where region>='B' and region<='E' and grp=$grp");
  189.   $rows+=fetch_all_rows($dbh,"select idn from bench1 where grp=$grp"); # This is hard
  190. }
  191. $count=$opt_small_loop_count*7;
  192.  
  193. $end_time=new Benchmark;
  194. print "Time for select_big ($count:$rows): " .
  195.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  196.  
  197. # Test select with many OR's
  198.  
  199. $loop_time=new Benchmark;
  200. $tmpvar=0;
  201. $count=0;
  202. $estimated=0;
  203. $max_and_conditions=$limits->{'max_conditions'}/2;
  204. $rows=0;
  205.  
  206. for ($i=0 ; $i < $opt_small_loop_count ; $i++)
  207. {
  208.   $region=chr(65+$i%($opt_regions+1));    # One larger to test out-of-regions
  209.   $query="select * from bench1 where ";
  210.   $or_part="grp = 1";
  211.   $or_part2="region='A' and grp=1";
  212.  
  213.   for ($j=1 ; $j < $columns; $j++)
  214.   {
  215.     $tmpvar^= ((($tmpvar + 63) + $j)*3 % 100000);
  216.     $tmp=$tmpvar % $opt_groups;
  217.     $tmp_region=chr(65+$tmpvar%$opt_regions);
  218.     $or_part.=" or grp=$tmp";
  219.     if ($j < $max_and_conditions)
  220.     {
  221.       $or_part2.=" or region='$tmp_region' and grp=$tmp";
  222.     }
  223.   }
  224.   $or_part="region='$region' and ($or_part)";
  225.  
  226. # Same query, but use 'func_extra_in_num' instead.
  227.   if ($limits->{'func_extra_in_num'})
  228.   {
  229.     $in_part=$or_part;
  230.     $in_part=~ s/ = / IN \(/;
  231.     $in_part=~ s/ or grp=/,/g;
  232.     $in_part.= ")";
  233.     defined($found=fetch_all_rows($dbh,$query . $in_part)) || die $DBI::errstr;
  234.     $rows+=$found;
  235.     $count++;
  236.   }
  237.   for ($j=0; $j < 10 ; $j++)
  238.   {
  239.     $rows+=fetch_all_rows($dbh,$query . $or_part);
  240.     $rows+=fetch_all_rows($dbh,$query . $or_part2);
  241. # Do it a little harder by setting a extra range
  242.     $rows+=fetch_all_rows($dbh,"$query ($or_part) and idn < 50");
  243.     $rows+=fetch_all_rows($dbh,"$query (($or_part) or (region='A' and grp < 10)) and region <='B'")
  244.   }
  245.   $count+=$j*4;
  246.   $end_time=new Benchmark;
  247.   last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  248.                      $opt_small_loop_count));
  249. }
  250.  
  251. print_time($estimated);
  252. print " for select_range ($count:$rows): " .
  253.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  254.  
  255. #
  256. # Testing MIN() and MAX() on keys
  257. #
  258.  
  259. if ($limits->{'group_functions'} && $limits->{'order_by_unused'})
  260. {
  261.   $loop_time=new Benchmark;
  262.   $count=0;
  263.   $estimated=0;
  264.   for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
  265.   {
  266.     $count+=7;
  267.     $grp=$tests*3 % $opt_groups;
  268.     $region=chr(65+$tests % $opt_regions);
  269.     if ($limits->{'group_func_sql_min_str'})
  270.     {
  271.       fetch_all_rows($dbh,"select min(region) from bench1");
  272.       fetch_all_rows($dbh,"select max(region) from bench1");
  273.       fetch_all_rows($dbh,"select min(region),max(region) from bench1");
  274.     }
  275.     fetch_all_rows($dbh,"select min(rev_idn) from bench1 where region='$region'");
  276.  
  277.     fetch_all_rows($dbh,"select max(grp) from bench1 where region='$region'");
  278.     fetch_all_rows($dbh,"select max(idn) from bench1 where region='$region' and grp=$grp");
  279.     if ($limits->{'group_func_sql_min_str'})
  280.     {
  281.       fetch_all_rows($dbh,"select max(region) from bench1 where region<'$region'");
  282.     }
  283.     $end_time=new Benchmark;
  284.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,
  285.                        $tests+1, $opt_loop_count));
  286.   }
  287.   print_time($estimated);
  288.   print " for min_max_on_key ($count): " .
  289.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  290.  
  291.   $loop_time=new Benchmark;
  292.   $count=0;
  293.   $estimated=0;
  294.   for ($tests=0 ; $tests < $opt_loop_count ; $tests++)
  295.   {
  296.     $count+=5;
  297.     $grp=$tests*3 % $opt_groups;
  298.     $region=chr(65+$tests % $opt_regions);
  299.     fetch_all_rows($dbh,"select count(*) from bench1 where region='$region'");
  300.     fetch_all_rows($dbh,"select count(*) from bench1 where region='$region' and grp=$grp");
  301.     fetch_all_rows($dbh,"select count(*) from bench1 where region>'$region'");
  302.     fetch_all_rows($dbh,"select count(*) from bench1 where region<='$region'");
  303.     fetch_all_rows($dbh,"select count(*) from bench1 where region='$region' and grp>$grp");
  304.     $end_time=new Benchmark;
  305.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,
  306.                        $tests+1, $opt_loop_count));
  307.   }
  308.   print_time($estimated);
  309.   print " for count_on_key ($count): " .
  310.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  311.   
  312. }
  313.  
  314. if ($limits->{'group_functions'})
  315. {
  316.   $loop_time=new Benchmark;
  317.   $rows=0;
  318.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  319.   {
  320.     $rows+=fetch_all_rows($dbh,"select grp,count(*) from bench1 group by grp");
  321.   }
  322.   $end_time=new Benchmark;
  323.   print "Time for count_group_on_key_parts ($i:$rows): " .
  324.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  325. }
  326.  
  327. if ($limits->{'group_distinct_functions'})
  328. {
  329.   print "Testing count(distinct) on the table\n";
  330.   $loop_time=new Benchmark;
  331.   $rows=$estimated=$count=0;
  332.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  333.   {
  334.     $count++;
  335.     $rows+=fetch_all_rows($dbh,"select count(distinct region) from bench1");
  336.     $end_time=new Benchmark;
  337.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  338.                        $opt_medium_loop_count));
  339.   }
  340.   print_time($estimated);
  341.   print " for count_distinct_key_prefix ($count:$rows): " .
  342.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  343.  
  344.   $loop_time=new Benchmark;
  345.   $rows=$estimated=$count=0;
  346.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  347.   {
  348.     $count++;
  349.     $rows+=fetch_all_rows($dbh,"select count(distinct grp) from bench1");
  350.     $end_time=new Benchmark;
  351.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  352.                        $opt_medium_loop_count));
  353.   }
  354.   print_time($estimated);
  355.   print " for count_distinct ($count:$rows): " .
  356.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  357.  
  358. #  Workaround mimer's behavior
  359.   if (limits->{'multi_distinct'} == 1 ) {
  360.     $loop_time=new Benchmark;
  361.     $rows=$estimated=$count=0;
  362.     for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  363.     {
  364.       $count++;
  365.       $rows+=fetch_all_rows($dbh,"select count(distinct grp),count(distinct rev_idn) from bench1");
  366.       $end_time=new Benchmark;
  367.       last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  368.                        $opt_medium_loop_count));
  369.     } 
  370.     print_time($estimated);
  371.     print " for count_distinct_2 ($count:$rows): " .
  372.       timestr(timediff($end_time, $loop_time),"all") . "\n";
  373.   }    
  374.  
  375.   $loop_time=new Benchmark;
  376.   $rows=$estimated=$count=0;
  377.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  378.   {
  379.     $count++;
  380.     $rows+=fetch_all_rows($dbh,"select region,count(distinct idn) from bench1 group by region");
  381.     $end_time=new Benchmark;
  382.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  383.                        $opt_medium_loop_count));
  384.   }
  385.   print_time($estimated);
  386.   print " for count_distinct_group_on_key ($count:$rows): " .
  387.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  388.  
  389.   $loop_time=new Benchmark;
  390.   $rows=$estimated=$count=0;
  391.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  392.   {
  393.     $count++;
  394.     $rows+=fetch_all_rows($dbh,"select grp,count(distinct idn) from bench1 group by grp");
  395.     $end_time=new Benchmark;
  396.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  397.                        $opt_medium_loop_count));
  398.   }
  399.   print_time($estimated);
  400.   print " for count_distinct_group_on_key_parts ($count:$rows): " .
  401.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  402.  
  403.   $loop_time=new Benchmark;
  404.   $rows=$estimated=$count=0;
  405.   for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  406.   {
  407.     $count++;
  408.     $rows+=fetch_all_rows($dbh,"select grp,count(distinct rev_idn) from bench1 group by grp");
  409.     $end_time=new Benchmark;
  410.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  411.                        $opt_medium_loop_count));
  412.   }
  413.   print_time($estimated);
  414.   print " for count_distinct_group ($count:$rows): " .
  415.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  416.  
  417.   $loop_time=new Benchmark;
  418.   $rows=$estimated=$count=0;
  419.   $test_count=$opt_medium_loop_count/10;
  420.   for ($i=0 ; $i < $test_count ; $i++)
  421.   {
  422.     $count++;
  423.     $rows+=fetch_all_rows($dbh,"select idn,count(distinct region) from bench1 group by idn");
  424.     $end_time=new Benchmark;
  425.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i+1,
  426.                        $test_count));
  427.   }
  428.   print_time($estimated);
  429.   print " for count_distinct_big ($count:$rows): " .
  430.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  431. }
  432.  
  433. ####
  434. #### End of benchmark
  435. ####
  436.  
  437. if ($opt_lock_tables)
  438. {
  439.   do_query($dbh,"UNLOCK TABLES");
  440. }
  441. if (!$opt_skip_delete)
  442. {
  443.   do_query($dbh,"drop table bench1" . $server->{'drop_attr'});
  444. }
  445.  
  446. if ($opt_fast && defined($server->{vacuum}))
  447. {
  448.   $server->vacuum(0,\$dbh);
  449. }
  450.  
  451. $dbh->disconnect;                # close connection
  452.  
  453. end_benchmark($start_time);
  454.