home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / sharewar / mysql / data1.cab / Development / bench / test-insert < prev    next >
Encoding:
Text File  |  2001-11-02  |  45.2 KB  |  1,616 lines

  1. #!/my/gnu/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 creating a simple table and inserting $record_count records in it,
  20. # $opt_loop_count rows in order, $opt_loop_count rows in reverse order and
  21. # $opt_loop_count rows in random order
  22. #
  23. # changes made for Oracle compatibility
  24. # - $limits{'func_odbc_mod'} is OK from crash-me, but it fails here so set we
  25. #   set it to 0 in server-cfg
  26. # - the default server config runs out of rollback segments, so I added a couple
  27. #   of disconnect/connects to reset
  28. ##################### Standard benchmark inits ##############################
  29.  
  30. use DBI;
  31. use Benchmark;
  32.  
  33. $opt_loop_count=100000;        # number of rows/3
  34. $small_loop_count=10;        # Loop for full table retrieval
  35. $range_loop_count=$small_loop_count*50;
  36. $many_keys_loop_count=$opt_loop_count;
  37. $opt_read_key_loop_count=$opt_loop_count;
  38.  
  39. chomp($pwd = `pwd`); $pwd = "." if ($pwd eq '');
  40. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  41.  
  42. if ($opt_small_test)
  43. {
  44.   $opt_loop_count/=100;
  45.   $many_keys_loop_count=$opt_loop_count/10;
  46.   $range_loop_count=10;
  47.   $opt_read_key_loop_count=10;
  48. }
  49. elsif ($opt_small_tables)
  50. {
  51.   $opt_loop_count=10000;        # number of rows/3
  52.   $many_keys_loop_count=$opt_loop_count;
  53.   $opt_read_key_loop_count=10;
  54. }
  55. elsif ($opt_small_key_tables)
  56. {
  57.   $many_keys_loop_count/=10;
  58. }
  59.  
  60. if ($opt_loop_count < 100)
  61. {
  62.   $opt_loop_count=100;        # Some tests must have some data to work!
  63. }
  64. $range_loop_count=min($opt_loop_count,$range_loop_count);
  65.  
  66.  
  67. print "Testing the speed of inserting data into 1 table and do some selects on it.\n";
  68. print "The tests are done with a table that has $opt_loop_count rows.\n\n";
  69.  
  70. ####
  71. #### Generating random keys
  72. ####
  73.  
  74. print "Generating random keys\n";
  75. $random[$opt_loop_count]=0;
  76. for ($i=0 ; $i < $opt_loop_count ; $i++)
  77. {
  78.   $random[$i]=$i+$opt_loop_count;
  79. }
  80.  
  81. my $tmpvar=1;
  82. for ($i=0 ; $i < $opt_loop_count ; $i++)
  83. {
  84.   $tmpvar^= ((($tmpvar + 63) + $i)*3 % $opt_loop_count);
  85.   $swap=$tmpvar % $opt_loop_count;
  86.   $tmp=$random[$i]; $random[$i]=$random[$swap]; $random[$swap]=$tmp;
  87. }
  88.  
  89. $total_rows=$opt_loop_count*3;
  90.  
  91. ####
  92. ####  Connect and start timeing
  93. ####
  94. $start_time=new Benchmark;
  95. $dbh = $server->connect();
  96. ####
  97. #### Create needed tables
  98. ####
  99.  
  100. goto keys_test if ($opt_stage == 2);
  101. goto select_test if ($opt_skip_create);
  102.  
  103. print "Creating tables\n";
  104. $dbh->do("drop table bench1" . $server->{'drop_attr'});
  105. $dbh->do("drop table bench2" . $server->{'drop_attr'});
  106. $dbh->do("drop table bench3" . $server->{'drop_attr'});
  107. do_many($dbh,$server->create("bench1",
  108.                  ["id int NOT NULL",
  109.                   "id2 int NOT NULL",
  110.                   "id3 int NOT NULL",
  111.                   "dummy1 char(30)"],
  112.                  ["primary key (id,id2)",
  113.                  "index index_id3 (id3)"]));
  114.  
  115. if ($opt_lock_tables)
  116. {
  117.   $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  118. }
  119.  
  120. ####
  121. #### Insert $total_rows records in order, in reverse order and random.
  122. ####
  123.  
  124. $loop_time=new Benchmark;
  125.  
  126. if ($opt_fast_insert)
  127. {
  128.   $query="insert into bench1 values ";
  129. }
  130. else
  131. {
  132.   $query="insert into bench1 (id,id2,id3,dummy1) values ";
  133. }
  134.  
  135. if (($opt_fast || $opt_fast_insert) && $server->{'limits'}->{'insert_multi_value'})
  136. {
  137.   $query_size=$server->{'limits'}->{'query_size'};
  138.  
  139.   print "Inserting $opt_loop_count multiple-value rows in order\n";
  140.   $res=$query;
  141.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  142.   {
  143.     $tmp= "($i,$i,$i,'ABCDEFGHIJ'),";
  144.     if (length($tmp)+length($res) < $query_size)
  145.     {
  146.       $res.= $tmp;
  147.     }
  148.     else
  149.     {
  150.       $sth = $dbh->do(substr($res,0,length($res)-1)) or die $DBI::errstr;
  151.       $res=$query . $tmp;
  152.     }
  153.   }
  154.   print "Inserting $opt_loop_count multiple-value rows in reverse order\n";
  155.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  156.   {
  157.     $tmp= "(" . ($total_rows-1-$i) . "," .($total_rows-1-$i) .
  158.       "," .($total_rows-1-$i) . ",'BCDEFGHIJK'),";
  159.     if (length($tmp)+length($res) < $query_size)
  160.     {
  161.       $res.= $tmp;
  162.     }
  163.     else
  164.     {
  165.       $sth = $dbh->do(substr($res,0,length($res)-1)) or die $DBI::errstr;
  166.       $res=$query . $tmp;
  167.     }
  168.   }
  169.   print "Inserting $opt_loop_count multiple-value rows in random order\n";
  170.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  171.   {
  172.     $tmp= "(" . $random[$i] . "," . $random[$i] . "," . $random[$i] .
  173.       ",'CDEFGHIJKL')," or die $DBI::errstr;
  174.     if (length($tmp)+length($res) < $query_size)
  175.     {
  176.       $res.= $tmp;
  177.     }
  178.     else
  179.     {
  180.       $sth = $dbh->do(substr($res,0,length($res)-1)) or die $DBI::errstr;
  181.       $res=$query . $tmp;
  182.     }
  183.   }
  184.   $sth = $dbh->do(substr($res,0,length($res)-1)) or die $DBI::errstr;
  185. }
  186. else
  187. {
  188.   print "Inserting $opt_loop_count rows in order\n";
  189.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  190.   {
  191.     $sth = $dbh->do($query . "($i,$i,$i,'ABCDEFGHIJ')") or die $DBI::errstr;
  192.   }
  193.  
  194.   print "Inserting $opt_loop_count rows in reverse order\n";
  195.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  196.   {
  197.     $sth = $dbh->do($query . "(" . ($total_rows-1-$i) . "," .
  198.             ($total_rows-1-$i) . "," .
  199.             ($total_rows-1-$i) . ",'BCDEFGHIJK')")
  200.       or die $DBI::errstr;
  201.   }
  202.  
  203.   print "Inserting $opt_loop_count rows in random order\n";
  204.  
  205.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  206.   {
  207.     $sth = $dbh->do($query . "(". $random[$i] . "," . $random[$i] .
  208.             "," . $random[$i] . ",'CDEFGHIJKL')") or die $DBI::errstr;
  209.   }
  210. }
  211.  
  212. $end_time=new Benchmark;
  213. print "Time for insert (" . ($total_rows) . "): " .
  214.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  215.  
  216. if ($opt_lock_tables)
  217. {
  218.   $sth = $dbh->do("UNLOCK TABLES") || die $DBI::errstr;
  219. }
  220. if ($opt_fast && defined($server->{vacuum}))
  221. {
  222.   $server->vacuum(1,\$dbh,"bench1");
  223. }
  224. if ($opt_lock_tables)
  225. {
  226.   $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  227. }
  228.  
  229. ####
  230. #### insert $opt_loop_count records with duplicate id
  231. ####
  232.  
  233. if ($limits->{'unique_index'})
  234. {
  235.   print "Testing insert of duplicates\n";
  236.   $loop_time=new Benchmark;
  237.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  238.   {
  239.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $opt_loop_count);
  240.     $tmp=$tmpvar % ($total_rows);
  241.     $tmpquery = "$query ($tmp,$tmp,2,'D')";
  242.     if ($dbh->do($tmpquery))
  243.     {
  244.       die "Didn't get an error when inserting duplicate record $tmp\n";
  245.     }
  246.   }
  247.  
  248.   $end_time=new Benchmark;
  249.   print "Time for insert_duplicates (" . ($opt_loop_count) . "): " .
  250.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  251. }
  252.  
  253. #if ($opt_fast && defined($server->{vacuum}))
  254. #{
  255. #  $server->vacuum(1,\$dbh);
  256. #}
  257.  
  258. ####
  259. #### Do some selects on the table
  260. ####
  261.  
  262. select_test:
  263.  
  264. print "Retrieving data from the table\n";
  265. $loop_time=new Benchmark;
  266. $error=0;
  267.  
  268. # It's really a small table, so we can try a select on everything
  269.  
  270. $count=0;
  271. for ($i=1 ; $i <= $small_loop_count ; $i++)
  272. {
  273.   if (($found_rows=fetch_all_rows($dbh,"select id from bench1")) !=
  274.       $total_rows)
  275.   {
  276.     if (!$error++)
  277.     {
  278.       print "Warning: Got $found_rows rows when selecting a whole table of " . ($total_rows) . " rows\nContact the database or DBD author!\n";
  279.     }
  280.   }
  281.   $count+=$found_rows;
  282. }
  283.  
  284. $end_time=new Benchmark;
  285. print "Time for select_big ($small_loop_count:$count): " .
  286.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  287.  
  288. #
  289. # Do a lot of different ORDER BY queries
  290. #
  291.  
  292. $loop_time=new Benchmark;
  293. $estimated=$rows=0;
  294. for ($i=1 ; $i <= $small_loop_count ; $i++)
  295. {
  296.   $rows+=fetch_all_rows($dbh,"select id,id2 from bench1 order by id,id2",1);
  297.   $end_time=new Benchmark;
  298.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  299.                      $small_loop_count));
  300. }
  301. if ($estimated)
  302. { print "Estimated time"; }
  303. else
  304. { print "Time"; }
  305. print " for order_by_big_key ($small_loop_count:$rows): " .
  306.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  307.  
  308.  
  309. $loop_time=new Benchmark;
  310. $estimated=$rows=0;
  311. for ($i=1 ; $i <= $small_loop_count ; $i++)
  312. {
  313.   $rows+=fetch_all_rows($dbh,"select id,id2 from bench1 order by id desc, id2 desc",1);
  314.   $end_time=new Benchmark;
  315.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  316.                      $small_loop_count));
  317. }
  318. if ($estimated)
  319. { print "Estimated time"; }
  320. else
  321. { print "Time"; }
  322. print " for order_by_big_key_desc ($small_loop_count:$rows): " .
  323.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  324.  
  325.  
  326. $loop_time=new Benchmark;
  327. $estimated=$rows=0;
  328. for ($i=1 ; $i <= $small_loop_count ; $i++)
  329. {
  330.   $rows+=fetch_all_rows($dbh,"select id from bench1 order by id desc",1);
  331.   $end_time=new Benchmark;
  332.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  333.                      $small_loop_count));
  334. }
  335. if ($estimated)
  336. { print "Estimated time"; }
  337. else
  338. { print "Time"; }
  339. print " for order_by_big_key_prefix ($small_loop_count:$rows): " .
  340.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  341.  
  342.  
  343. $loop_time=new Benchmark;
  344. $estimated=$rows=0;
  345. for ($i=1 ; $i <= $small_loop_count ; $i++)
  346. {
  347.   $rows+=fetch_all_rows($dbh,"select id3 from bench1 order by id3",1);
  348.   $end_time=new Benchmark;
  349.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  350.                      $small_loop_count));
  351. }
  352. if ($estimated)
  353. { print "Estimated time"; }
  354. else
  355. { print "Time"; }
  356. print " for order_by_big_key2 ($small_loop_count:$rows): " .
  357.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  358.  
  359.  
  360. $sel=$limits->{'order_by_unused'} ? "id2" : "*";
  361. $loop_time=new Benchmark;
  362. $estimated=$rows=0;
  363. for ($i=1 ; $i <= $small_loop_count ; $i++)
  364. {
  365.   $rows+=fetch_all_rows($dbh,"select $sel from bench1 order by id3",1);
  366.   $end_time=new Benchmark;
  367.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  368.                      $small_loop_count));
  369. }
  370. if ($estimated)
  371. { print "Estimated time"; }
  372. else
  373. { print "Time"; }
  374. print " for order_by_big_key_diff ($small_loop_count:$rows): " .
  375.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  376.  
  377.  
  378. $sel=$limits->{'order_by_unused'} ? "id" : "*";
  379. $loop_time=new Benchmark;
  380. $estimated=$rows=0;
  381. for ($i=1 ; $i <= $small_loop_count ; $i++)
  382. {
  383.   $rows+=fetch_all_rows($dbh,"select $sel from bench1 order by id2,id3",1);
  384.   $end_time=new Benchmark;
  385.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  386.                      $small_loop_count));
  387. }
  388. if ($estimated)
  389. { print "Estimated time"; }
  390. else
  391. { print "Time"; }
  392. print " for order_by_big ($small_loop_count:$rows): " .
  393.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  394.  
  395.  
  396. $sel=$limits->{'order_by_unused'} ? "dummy1" : "dummy1,id3";
  397. $loop_time=new Benchmark;
  398. $estimated=$rows=0;
  399. for ($i=1 ; $i <= $range_loop_count ; $i++)
  400. {
  401.   $start=$opt_loop_count/$range_loop_count*$i;
  402.   $end=$start+$i;
  403.   $rows+=fetch_all_rows($dbh,"select $sel from bench1 where id>=$start and id <= $end order by id3",1);
  404.   $end_time=new Benchmark;
  405.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  406.                      $range_loop_count));
  407. }
  408. if ($estimated)
  409. { print "Estimated time"; }
  410. else
  411. { print "Time"; }
  412. print " for order_by_range ($range_loop_count:$rows): " .
  413.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  414.  
  415. $sel=$limits->{'order_by_unused'} ? "dummy1" : "dummy1,id";
  416. $loop_time=new Benchmark;
  417. $estimated=$rows=0;
  418. for ($i=1 ; $i <= $range_loop_count ; $i++)
  419. {
  420.   $start=$opt_loop_count/$range_loop_count*$i;
  421.   $end=$start+$i;
  422.   $rows+=fetch_all_rows($dbh,"select $sel from bench1 where id>=$start and id <= $end order by id",1);
  423.   $end_time=new Benchmark;
  424.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  425.                      $range_loop_count));
  426. }
  427. if ($estimated)
  428. { print "Estimated time"; }
  429. else
  430. { print "Time"; }
  431. print " for order_by_key_prefix ($range_loop_count:$rows): " .
  432.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  433.  
  434. $sel=$limits->{'order_by_unused'} ? "id2" : "id2,id3";
  435. $loop_time=new Benchmark;
  436. $estimated=$rows=0;
  437. for ($i=1 ; $i <= $range_loop_count ; $i++)
  438. {
  439.   $start=$opt_loop_count/$range_loop_count*$i;
  440.   $end=$start+$range_loop_count;
  441.   $rows+=fetch_all_rows($dbh,"select $sel from bench1 where id3>=$start and id3 <= $end order by id3",1);
  442.   $end_time=new Benchmark;
  443.   last if ($estimated=predict_query_time($loop_time,$end_time,\$i,$i,
  444.                      $range_loop_count));
  445. }
  446. if ($estimated)
  447. { print "Estimated time"; }
  448. else
  449. { print "Time"; }
  450. print " for order_by_key2_diff ($range_loop_count:$rows): " .
  451.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  452.  
  453. #
  454. # Test of select on 2 different keys with or
  455. # (In this case database can only use keys if they do an automatic union).
  456. #
  457.  
  458. $loop_time=new Benchmark;
  459. $estimated=0;
  460. $rows=0;
  461. $count=0;
  462. for ($i=1 ; $i <= $range_loop_count ; $i++)
  463. {
  464.   my $rnd=$i;
  465.   my $rnd2=$random[$i];
  466.   $rows+=fetch_all_rows($dbh,"select id2 from bench1 where id=$rnd or id3=$rnd2",1);
  467.   $count++;
  468.   $end_time=new Benchmark;
  469.   last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$count,
  470.                      $range_loop_count));
  471. }
  472. if ($estimated)
  473. { print "Estimated time"; }
  474. else
  475. { print "Time"; }
  476. print " for select_diff_key ($count:$rows): " .
  477.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  478.  
  479.  
  480. # Test select that is very popular when using ODBC
  481.  
  482. check_or_range("id","select_range_prefix");
  483. check_or_range("id3","select_range_key2");
  484.  
  485. # Check reading on direct key on id and id3
  486.  
  487. check_select_key("*","id","select_key_prefix");
  488. check_select_key2("*","id","id2","select_key");
  489. check_select_key2("id,id2","id","id2","select_key_return_key");
  490. check_select_key("*","id3","select_key2");
  491. check_select_key("id3","id3","select_key2_return_key");
  492. check_select_key("id,id2","id3","select_key2_return_prim");
  493.  
  494. ####
  495. #### A lot of simple selects on ranges
  496. ####
  497.  
  498. @Q=("select * from bench1 where !id!=3 or !id!=2 or !id!=1 or !id!=4 or !id!=16 or !id!=10",
  499.     6,
  500.     "select * from bench1 where !id!>=" . ($total_rows-1) ." or !id!<1",
  501.     2,
  502.     "select * from bench1 where !id!>=1 and !id!<=2",
  503.     2,
  504.     "select * from bench1 where (!id!>=1 and !id!<=2) or (!id!>=1 and !id!<=2)",
  505.     2,
  506.     "select * from bench1 where !id!>=1 and !id!<=10 and !id!<=5",
  507.     5,
  508.     "select * from bench1 where (!id!>0 and !id!<2) or !id!>=" . ($total_rows-1),
  509.     2,
  510.     "select * from bench1 where (!id!>0 and !id!<2) or (!id!>= " . ($opt_loop_count/2) . " and !id! <= " . ($opt_loop_count/2+2) . ") or !id! = " . ($opt_loop_count/2-1),
  511.     5,
  512.     "select * from bench1 where (!id!>=5 and !id!<=10) or (!id!>=1 and !id!<=4)",
  513.     10,
  514.     "select * from bench1 where (!id!=1 or !id!=2) and (!id!=3 or !id!=4)",
  515.     0,
  516.     "select * from bench1 where (!id!=1 or !id!=2) and (!id!=2 or !id!=3)",
  517.     1,
  518.     "select * from bench1 where (!id!=1 or !id!=5 or !id!=20 or !id!=40) and (!id!=1 or !id!>=20 or !id!=4)",
  519.     3,
  520.     "select * from bench1 where ((!id!=1 or !id!=3) or (!id!>1 and !id!<3)) and !id!<=2",
  521.     2,
  522.     "select * from bench1 where (!id! >= 0 and !id! < 4) or (!id! >=4 and !id! < 6)",
  523.     6,
  524.     "select * from bench1 where !id! <= -1 or (!id! >= 0 and !id! <= 5) or (!id! >=4 and !id! < 6) or (!id! >=6 and !id! <=7) or (!id!>7 and !id! <= 8)",
  525.     9,
  526.     "select * from bench1 where (!id!>=1 and !id!<=2 or !id!>=4 and !id!<=5) or (!id!>=0 and !id! <=10)",
  527.     11,
  528.     "select * from bench1 where (!id!>=1 and !id!<=2 or !id!>=4 and !id!<=5) or (!id!>2 and !id! <=10)",
  529.     10,
  530.     "select * from bench1 where (!id!>1 or !id! <1) and !id!<=2",
  531.     2,
  532.     "select * from bench1 where !id! <= 2 and (!id!>1 or !id! <=1)",
  533.     3,
  534.     "select * from bench1 where (!id!>=1 or !id! <1) and !id!<=2",
  535.     3,
  536.     "select * from bench1 where (!id!>=1 or !id! <=2) and !id!<=2",
  537.     3
  538.     );
  539.  
  540. print "\nTest of compares with simple ranges\n";
  541. check_select_range("id","select_range_prefix");
  542. check_select_range("id3","select_range_key2");
  543.  
  544. ####
  545. #### Some group queries
  546. ####
  547.  
  548. if ($limits->{'group_functions'})
  549. {
  550.   $loop_time=new Benchmark;
  551.   $count=1;
  552.  
  553.   $estimated=0;
  554.   for ($tests=0 ; $tests < $small_loop_count ; $tests++)
  555.   {
  556.     $sth=$dbh->prepare($query="select count(*) from bench1") or die $DBI::errstr;
  557.     $sth->execute or die $sth->errstr;
  558.     if (($sth->fetchrow_array)[0] != $total_rows)
  559.     {
  560.       print "Warning: '$query' returned wrong result\n";
  561.     }
  562.     $sth->finish;
  563.  
  564.     # min, max in keys are very normal
  565.     $count+=7;
  566.     fetch_all_rows($dbh,"select min(id) from bench1");
  567.     fetch_all_rows($dbh,"select max(id) from bench1");
  568.     fetch_all_rows($dbh,"select sum(id+0.0) from bench1");
  569.     fetch_all_rows($dbh,"select min(id3),max(id3),sum(id3 +0.0) from bench1");
  570.     if ($limits->{'group_func_sql_min_str'})
  571.     {
  572.       fetch_all_rows($dbh,"select min(dummy1),max(dummy1) from bench1");
  573.     }
  574.     $count++;
  575.     $sth=$dbh->prepare($query="select count(*) from bench1 where id >= " .
  576.                ($opt_loop_count*2)) or die $DBI::errstr;
  577.     $sth->execute or die $DBI::errstr;
  578.     if (($sth->fetchrow_array)[0] != $opt_loop_count)
  579.     {
  580.       print "Warning: '$query' returned wrong result\n";
  581.     }
  582.     $sth->finish;
  583.  
  584.  
  585.     $count++;
  586.     $sth=$dbh->prepare($query="select count(*),sum(id+0.0),min(id),max(id),avg(id+0.0) from bench1") or die $DBI::errstr;
  587.     $sth->execute or die $DBI::errstr;
  588.     @row=$sth->fetchrow_array;
  589.     if ($row[0] != $total_rows ||
  590.     int($row[1]+0.5) != int((($total_rows-1)/2*$total_rows)+0.5) ||
  591.     $row[2] != 0 ||
  592.     $row[3] != $total_rows-1 ||
  593.     1-$row[4]/(($total_rows-1)/2) > 0.001)
  594.     {
  595.       # PostgreSQL 6.3 fails here
  596.       print "Warning: '$query' returned wrong result: @row\n";
  597.     }
  598.     $sth->finish;
  599.  
  600.     if ($limits->{'func_odbc_mod'})
  601.     {
  602.       $tmp="mod(id,10)";
  603.       if ($limits->{'func_extra_%'})
  604.       {
  605.     $tmp="id % 10";        # For postgreSQL
  606.       }
  607.       $count++;
  608.       if ($limits->{'group_by_alias'}) {
  609.     if (fetch_all_rows($dbh,$query=$server->query("select $tmp as last_digit,count(*) from bench1 group by last_digit")) != 10)
  610.     {
  611.       print "Warning: '$query' returned wrong number of rows\n";
  612.     }
  613.       } elsif ($limits->{'group_by_position'}) {
  614.     if (fetch_all_rows($dbh,$query=$server->query("select $tmp,count(*) from bench1 group by 1")) != 10)
  615.     {
  616.       print "Warning: '$query' returned wrong number of rows\n";
  617.     }
  618.       }
  619.     }
  620.  
  621.     if ($limits->{'order_by_position'} && $limits->{'group_by_position'})
  622.     {
  623.       $count++;
  624.       if (fetch_all_rows($dbh, $query="select id,id3,dummy1 from bench1 where id < 100+$count-$count group by id,id3,dummy1 order by id desc,id3,dummy1") != 100)
  625.       {
  626.     print "Warning: '$query' returned wrong number of rows\n";
  627.       }
  628.     }
  629.     $end_time=new Benchmark;
  630.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$tests,
  631.                        $small_loop_count));
  632.   }
  633.   print_time($estimated);
  634.   print " for select_group ($count): " .
  635.       timestr(timediff($end_time, $loop_time),"all") . "\n";
  636.  
  637.   $loop_time=new Benchmark;
  638.   $count=$estimated=0;
  639.   for ($tests=1 ; $tests <= $range_loop_count*5 ; $tests++)
  640.   {
  641.     $count+=6;
  642.     fetch_all_rows($dbh,"select min(id) from bench1");
  643.     fetch_all_rows($dbh,"select max(id) from bench1");
  644.     fetch_all_rows($dbh,"select min(id2) from bench1 where id=$tests");
  645.     fetch_all_rows($dbh,"select max(id2) from bench1 where id=$tests");
  646.     if ($limits->{'group_func_sql_min_str'})
  647.     {
  648.       fetch_all_rows($dbh,"select min(dummy1) from bench1 where id=$tests");
  649.       fetch_all_rows($dbh,"select max(dummy1) from bench1 where id=$tests");
  650.     }
  651.     $end_time=new Benchmark;
  652.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$tests,
  653.                        $range_loop_count*5));
  654.   }
  655.   if ($estimated)
  656.   { print "Estimated time"; }
  657.   else
  658.   { print "Time"; }
  659.   print " for min_max_on_key ($count): " .
  660.       timestr(timediff($end_time, $loop_time),"all") . "\n";
  661.  
  662.   $loop_time=new Benchmark;
  663.   $count=$estimated=0;
  664.   for ($tests=1 ; $tests <= $small_loop_count ; $tests++)
  665.   {
  666.     $count+=6;
  667.     fetch_all_rows($dbh,"select min(id2) from bench1");
  668.     fetch_all_rows($dbh,"select max(id2) from bench1");
  669.     fetch_all_rows($dbh,"select min(id3) from bench1 where id2=$tests");
  670.     fetch_all_rows($dbh,"select max(id3) from bench1 where id2=$tests");
  671.     if ($limits->{'group_func_sql_min_str'})
  672.     {
  673.       fetch_all_rows($dbh,"select min(dummy1) from bench1 where id2=$tests");
  674.       fetch_all_rows($dbh,"select max(dummy1) from bench1 where id2=$tests");
  675.     }
  676.     $end_time=new Benchmark;
  677.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$tests,
  678.                        $range_loop_count));
  679.   }
  680.   if ($estimated)
  681.   { print "Estimated time"; }
  682.   else
  683.   { print "Time"; }
  684.   print " for min_max ($count): " .
  685.       timestr(timediff($end_time, $loop_time),"all") . "\n";
  686.  
  687.   $loop_time=new Benchmark;
  688.   $count=0;
  689.   $total=$opt_loop_count*3;
  690.   for ($tests=0 ; $tests < $total ; $tests+=$total/100)
  691.   {
  692.     $count+=1;
  693.     fetch_all_rows($dbh,"select count(id) from bench1 where id < $tests");
  694.   }
  695.   $end_time=new Benchmark;
  696.   print "Time for count_on_key ($count): " .
  697.       timestr(timediff($end_time, $loop_time),"all") . "\n";
  698.  
  699.   $loop_time=new Benchmark;
  700.   $count=0;
  701.   for ($tests=0 ; $tests < $total ; $tests+=$total/100)
  702.   {
  703.     $count+=1;
  704.     fetch_all_rows($dbh,"select count(dummy1) from bench1 where id2 < $tests");
  705.   }
  706.   $end_time=new Benchmark;
  707.   print "Time for count ($count): " .
  708.       timestr(timediff($end_time, $loop_time),"all") . "\n";
  709.  
  710.   if ($limits->{'group_distinct_functions'})
  711.   {
  712.     $loop_time=new Benchmark;
  713.     $count=$estimated=0;
  714.     for ($tests=1 ; $tests <= $small_loop_count ; $tests++)
  715.     {
  716.       $count+=2;
  717.       fetch_all_rows($dbh,"select count(distinct dummy1) from bench1");
  718.       fetch_all_rows($dbh,"select dummy1,count(distinct id) from bench1 group by dummy1");
  719.       $end_time=new Benchmark;
  720.       last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$tests,
  721.                          $small_loop_count));
  722.     }
  723.     if ($estimated)
  724.     { print "Estimated time"; }
  725.     else
  726.     { print "Time"; }
  727.     print " for count_distinct_big ($count): " .
  728.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  729.   }
  730. }
  731.  
  732.  
  733. if ($server->small_rollback_segment())
  734. {
  735.   $dbh->disconnect;                # close connection
  736.   $dbh = $server->connect();
  737. }
  738.  
  739. ####
  740. #### Some updates on the table
  741. ####
  742.  
  743. $loop_time=new Benchmark;
  744.  
  745. if ($limits->{'functions'})
  746. {
  747.   print "\nTesting update of keys with functions\n";
  748.   my $update_loop_count=$opt_loop_count/2;
  749.   for ($i=0 ; $i < $update_loop_count ; $i++)
  750.   {
  751.     my $tmp=$opt_loop_count+$random[$i]; # $opt_loop_count*2 <= $tmp < $total_rows
  752.     $sth = $dbh->do("update bench1 set id3=-$tmp where id3=$tmp") or die $DBI::errstr;
  753.   }
  754.  
  755.   $end_time=new Benchmark;
  756.   print "Time for update_of_key ($update_loop_count):  " .
  757.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  758.  
  759.   if ($opt_lock_tables)
  760.   {
  761.     do_query($dbh,"UNLOCK TABLES");
  762.   }
  763.   if ($opt_fast && defined($server->{vacuum}))
  764.   {
  765.     $server->vacuum(1,\$dbh,"bench1");
  766.   }
  767.   if ($opt_lock_tables)
  768.   {
  769.     $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  770.   }
  771.  
  772.   if ($server->small_rollback_segment())
  773.   {
  774.     $dbh->disconnect;                # close connection
  775.     $dbh = $server->connect();
  776.   }
  777.  
  778.   $loop_time=new Benchmark;
  779.   $count=0;
  780.   $step=int($opt_loop_count/$range_loop_count+1);
  781.   for ($i= 0 ; $i < $opt_loop_count ; $i+= $step)
  782.   {
  783.     $count++;
  784.     $sth=$dbh->do("update bench1 set id3= 0-id3 where id3 >= 0 and id3 <= $i") or die $DBI::errstr;
  785.   }
  786.  
  787.   if ($server->small_rollback_segment())
  788.   {
  789.     $dbh->disconnect;                # close connection
  790.     $dbh = $server->connect();
  791.   }
  792.   $count++;
  793.   $sth=$dbh->do("update bench1 set id3= 0-id3 where id3 >= 0 and id3 < $opt_loop_count") or die $DBI::errstr;
  794.  
  795.   if ($server->small_rollback_segment())
  796.   {
  797.     $dbh->disconnect;                # close connection
  798.     $dbh = $server->connect();
  799.   }
  800.   $count++;
  801.   $sth=$dbh->do("update bench1 set id3= 0-id3 where id3 >= $opt_loop_count and id3 < ". ($opt_loop_count*2)) or die $DBI::errstr;
  802.  
  803.   #
  804.   # Check that everything was updated
  805.   # In principle we shouldn't time this in the update loop..
  806.   #
  807.  
  808.   if ($server->small_rollback_segment())
  809.   {
  810.     $dbh->disconnect;                # close connection
  811.     $dbh = $server->connect();
  812.   }
  813.   $row_count=0;
  814.   if (($sth=$dbh->prepare("select count(*) from bench1 where id3>=0"))
  815.       && $sth->execute)
  816.   {
  817.     ($row_count)=$sth->fetchrow;
  818.   }
  819.   $result=1 + $opt_loop_count-$update_loop_count;
  820.   if ($row_count != $result)
  821.   {
  822.     print "Warning: Update check returned $row_count instead of $result\n";
  823.   }
  824.  
  825.   $sth->finish;
  826.   if ($server->small_rollback_segment())
  827.   {
  828.     $dbh->disconnect;                # close connection
  829.     $dbh = $server->connect();
  830.   }
  831.   #restore id3 to 0 <= id3 < $total_rows/10 or 0<= id3 < $total_rows
  832.  
  833.   my $func=($limits->{'func_odbc_floor'}) ? "floor((0-id3)/20)" : "0-id3";
  834.   $count++;
  835.   $sth=$dbh->do($query="update bench1 set id3=$func where id3<0") or die $DBI::errstr;
  836.  
  837.   $end_time=new Benchmark;
  838.   print "Time for update_of_key_big ($count): " .
  839.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  840. }
  841. else
  842. {
  843.   print "\nTesting update of keys in loops\n";
  844.   #
  845.   # This is for mSQL that doesn't have functions. Do we really need this ????
  846.   #
  847.  
  848.   $sth=$dbh->prepare("select id3 from bench1 where id3 >= 0") or die $DBI::errstr;
  849.   $sth->execute or die $DBI::errstr;
  850.   $count=0;
  851.   while (@tmp = $sth->fetchrow_array)
  852.   {
  853.     my $tmp1 = "-$tmp[0]";
  854.     my $sth1 = $dbh->do("update bench1 set id3 = $tmp1 where id3 = $tmp[0]");
  855.     $count++;
  856.     $end_time=new Benchmark;
  857.     if (($end_time->[0] - $loop_time->[0]) > $opt_time_limit)
  858.     {
  859.       print "note: Aborting update loop because of timeout\n";
  860.       last;
  861.     }
  862.   }
  863.   $sth->finish;
  864.   # Check that everything except id3=0 was updated
  865.   # In principle we shouldn't time this in the update loop..
  866.   #
  867.   if (fetch_all_rows($dbh,$query="select * from bench1 where id3>=0") != 1)
  868.   {
  869.     if ($count == $total_rows)
  870.     {
  871.       print "Warning: Wrong information after update: Found '$row_count' rows, but should have been: 1\n";
  872.     }
  873.   }
  874.   #restore id3 to 0 <= id3 < $total_rows
  875.   $sth=$dbh->prepare("select id3 from bench1 where id3 < 0") or die $DBI::errstr;
  876.   $sth->execute or die $DBI::errstr;
  877.   while (@tmp = $sth->fetchrow_array)
  878.   {
  879.     $count++;
  880.     my $tmp1 = floor((0-$tmp[0])/10);
  881.     my $sth1 = $dbh->do("update bench1 set id3 = $tmp1 where id3 = $tmp[0]");
  882.   }
  883.   $sth->finish;
  884.   $end_time=new Benchmark;
  885.   $estimated=predict_query_time($loop_time,$end_time,\$count,$count,
  886.                 $opt_loop_count*6);
  887.   if ($estimated)
  888.   { print "Estimated time"; }
  889.   else
  890.   { print "Time"; }
  891.   print " for update_of_key ($count): " .
  892.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  893. }
  894.  
  895. if ($opt_fast && defined($server->{vacuum}))
  896. {
  897.   if ($opt_lock_tables)
  898.   {
  899.     do_query($dbh,"UNLOCK TABLES");
  900.   }
  901.   $server->vacuum(1,\$dbh,"bench1");
  902.   if ($opt_lock_tables)
  903.   {
  904.     $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  905.   }
  906. }
  907.  
  908. #
  909. # Testing some simple updates
  910. #
  911.  
  912. print "Testing update with key\n";
  913. $loop_time=new Benchmark;
  914. for ($i=0 ; $i < $opt_loop_count*3 ; $i++)
  915. {
  916.   $sth = $dbh->do("update bench1 set dummy1='updated' where id=$i and id2=$i") or die $DBI::errstr;
  917. }
  918.  
  919. $end_time=new Benchmark;
  920. print "Time for update_with_key (" . ($opt_loop_count*3) . "):  " .
  921.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  922.  
  923. $loop_time=new Benchmark;
  924. for ($i=0 ; $i < $opt_loop_count*3 ; $i+=3)
  925. {
  926.   $sth = $dbh->do("update bench1 set dummy1='updated' where id=$i") or die $DBI::errstr;
  927. }
  928.  
  929. $end_time=new Benchmark;
  930. print "Time for update_with_key_prefix (" . ($opt_loop_count) . "):  " .
  931.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  932.  
  933. print "\nTesting update of all rows\n";
  934. $loop_time=new Benchmark;
  935. for ($i=0 ; $i < $small_loop_count ; $i++)
  936. {
  937.   $sth = $dbh->do("update bench1 set dummy1='updated $i'") or die $DBI::errstr;
  938. }
  939. $end_time=new Benchmark;
  940. print "Time for update_big ($small_loop_count):  " .
  941.   timestr(timediff($end_time, $loop_time),"all") . "\n";
  942.  
  943.  
  944. #
  945. # Testing left outer join
  946. #
  947.  
  948. if ($limits->{'func_odbc_floor'} && $limits->{'left_outer_join'})
  949. {
  950.   if ($opt_lock_tables)
  951.   {
  952.     $sth = $dbh->do("LOCK TABLES bench1 a READ, bench1 b READ") || die $DBI::errstr;
  953.   }
  954.   print "\nTesting left outer join\n";
  955.   $loop_time=new Benchmark;
  956.   $count=0;
  957.   for ($i=0 ; $i < $small_loop_count ; $i++)
  958.   {
  959.     $count+=fetch_all_rows($dbh,"select count(*) from bench1 as a left outer join bench1 as b on (a.id2=b.id3)");
  960.   }
  961.   $end_time=new Benchmark;
  962.   print "Time for outer_join_on_key ($small_loop_count:$count):  " .
  963.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  964.  
  965.   $loop_time=new Benchmark;
  966.   $count=0;
  967.   for ($i=0 ; $i < $small_loop_count ; $i++)
  968.   {
  969.     $count+=fetch_all_rows($dbh,"select count(a.dummy1),count(b.dummy1) from bench1 as a left outer join bench1 as b on (a.id2=b.id3)");
  970.   }
  971.   $end_time=new Benchmark;
  972.   print "Time for outer_join ($small_loop_count:$count):  " .
  973.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  974.  
  975.   $count=0;
  976.   $loop_time=new Benchmark;
  977.   for ($i=0 ; $i < $small_loop_count ; $i++)
  978.   {
  979.     $count+=fetch_all_rows($dbh,"select count(a.dummy1),count(b.dummy1) from bench1 as a left outer join bench1 as b on (a.id2=b.id3) where b.id3 is not null");
  980.   }
  981.   $end_time=new Benchmark;
  982.   print "Time for outer_join_found ($small_loop_count:$count):  " .
  983.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  984.  
  985.   $count=$estimated=0;
  986.   $loop_time=new Benchmark;
  987.   for ($i=1 ; $i <= $small_loop_count ; $i++)
  988.   {
  989.     $count+=fetch_all_rows($dbh,"select count(a.dummy1),count(b.dummy1) from bench1 as a left outer join bench1 as b on (a.id2=b.id3) where b.id3 is null");
  990.     $end_time=new Benchmark;
  991.     last if ($estimated=predict_query_time($loop_time,$end_time,
  992.                        \$count,$i,
  993.                        $range_loop_count));
  994.   }
  995.   if ($estimated)
  996.   { print "Estimated time"; }
  997.   else
  998.   { print "Time"; }
  999.   print " for outer_join_not_found ($range_loop_count:$count):  " .
  1000.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1001.  
  1002.   if ($opt_lock_tables)
  1003.   {
  1004.     $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  1005.   }
  1006. }
  1007.  
  1008. if ($server->small_rollback_segment())
  1009. {
  1010.   $dbh->disconnect;                # close connection
  1011.   $dbh = $server->connect();
  1012. }
  1013.  
  1014. ####
  1015. #### Test INSERT INTO ... SELECT
  1016. ####
  1017.  
  1018. if ($limits->{'insert_select'})
  1019. {
  1020.   if ($opt_lock_tables)
  1021.   {
  1022.     $sth = $dbh->do("UNLOCK TABLES") || die $DBI::errstr;
  1023.   }
  1024.   print "\nTesting INSERT INTO ... SELECT\n";
  1025.   do_many($dbh,$server->create("bench2",
  1026.                    ["id int NOT NULL",
  1027.                 "id2 int NOT NULL",
  1028.                 "id3 int NOT NULL",
  1029.                 "dummy1 char(30)"],
  1030.                    ["primary key (id,id2)"]));
  1031.   do_many($dbh,$server->create("bench3",
  1032.                    ["id int NOT NULL",
  1033.                 "id2 int NOT NULL",
  1034.                 "id3 int NOT NULL",
  1035.                 "dummy1 char(30)"],
  1036.                    ["primary key (id,id2)",
  1037.                 "index index_id3 (id3)"]));
  1038.   $loop_time=new Benchmark;
  1039.   $sth = $dbh->do("INSERT INTO bench2 SELECT * from bench1") ||
  1040.     die $DBI::errstr;
  1041.   $end_time=new Benchmark;
  1042.   print "Time for insert_select_1_key (1):  " .
  1043.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1044.   $loop_time=new Benchmark;
  1045.   $sth = $dbh->do("INSERT INTO bench3 SELECT * from bench1") ||
  1046.     die $DBI::errstr;
  1047.   $end_time=new Benchmark;
  1048.   print "Time for insert_select_2_keys (1):  " .
  1049.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1050.   $loop_time=new Benchmark;
  1051.   $sth = $dbh->do("DROP TABLE bench2" . $server->{'drop_attr'}) ||
  1052.     die $DBI::errstr;
  1053.   $sth = $dbh->do("DROP TABLE bench3" . $server->{'drop_attr'}) ||
  1054.     die $DBI::errstr;
  1055.   $end_time=new Benchmark;
  1056.   print "Time for drop table(2): " .
  1057.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1058.  
  1059.   if ($opt_fast && defined($server->{vacuum}))
  1060.   {
  1061.     $server->vacuum(1,\$dbh);
  1062.   }
  1063.   if ($server->small_rollback_segment())
  1064.   {
  1065.     $dbh->disconnect;                # close connection
  1066.     $dbh = $server->connect();
  1067.   }
  1068.   if ($opt_lock_tables)
  1069.   {
  1070.     $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  1071.   }
  1072. }
  1073.  
  1074.  
  1075. ####
  1076. #### Do some deletes on the table
  1077. ####
  1078.  
  1079. if (!$opt_skip_delete)
  1080. {
  1081.   print "\nTesting delete\n";
  1082.   $loop_time=new Benchmark;
  1083.   $count=0;
  1084.   for ($i=0 ; $i < $opt_loop_count ; $i+=10)
  1085.   {
  1086.     $count++;
  1087.     $tmp=$opt_loop_count+$random[$i]; # $opt_loop_count*2 <= $tmp < $total_rows
  1088.     $dbh->do("delete from bench1 where id3=$tmp") or die $DBI::errstr;
  1089.   }
  1090.  
  1091.   $end_time=new Benchmark;
  1092.   print "Time for delete_key ($count): " .
  1093.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1094.  
  1095.   if ($server->small_rollback_segment())
  1096.   {
  1097.     $dbh->disconnect;                # close connection
  1098.     $dbh = $server->connect();
  1099.   }
  1100.  
  1101.   $count=0;
  1102.   $loop_time=new Benchmark;
  1103.   for ($i= 0 ; $i < $opt_loop_count ; $i+=$opt_loop_count/10)
  1104.   {
  1105.     $sth=$dbh->do("delete from bench1 where id3 >= 0 and id3 <= $i") or die $DBI::errstr;
  1106.     $count++;
  1107.   }
  1108.   $count+=2;
  1109.   if ($server->small_rollback_segment())
  1110.   {
  1111.     $dbh->disconnect;                # close connection
  1112.     $dbh = $server->connect();
  1113.   }
  1114.   $sth=$dbh->do("delete from bench1 where id3 >= 0 and id3 <= $opt_loop_count") or die $DBI::errstr;
  1115.   if ($server->small_rollback_segment())
  1116.   {
  1117.     $dbh->disconnect;                # close connection
  1118.     $dbh = $server->connect();
  1119.   }
  1120.  
  1121.   $sth=$dbh->do("delete from bench1 where id >= $opt_loop_count and id <= " . ($opt_loop_count*2) ) or die $DBI::errstr;
  1122.  
  1123.   if ($server->small_rollback_segment())
  1124.   {
  1125.     $dbh->disconnect;                # close connection
  1126.     $dbh = $server->connect();
  1127.   }
  1128.   if ($opt_fast)
  1129.   {
  1130.     $sth=$dbh->do("delete from bench1") or die $DBI::errstr;
  1131.   }
  1132.   else
  1133.   {
  1134.     $sth = $dbh->do("delete from bench1 where id3 < " . ($total_rows)) or die $DBI::errstr;
  1135.   }
  1136.  
  1137.   $end_time=new Benchmark;
  1138.   print "Time for delete_all ($count): " .
  1139.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1140.  
  1141.   if ($opt_lock_tables)
  1142.   {
  1143.     $sth = $dbh->do("UNLOCK TABLES") || die $DBI::errstr;
  1144.   }
  1145.   $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'}) or die $DBI::errstr;
  1146. }
  1147.  
  1148. if ($server->small_rollback_segment())
  1149. {
  1150.   $dbh->disconnect;                # close connection
  1151.   $dbh = $server->connect();
  1152. }
  1153. if ($opt_fast && defined($server->{vacuum}))
  1154. {
  1155.   $server->vacuum(1,\$dbh);
  1156. }
  1157.  
  1158.  
  1159. keys_test:
  1160. #
  1161. # Test of insert in table with many keys
  1162. # This test assumes that the server really create the keys!
  1163. #
  1164.  
  1165. my @fields=(); my @keys=();
  1166. $keys=min($limits->{'max_index'},16);          # 16 is more than enough
  1167. $seg= min($limits->{'max_index_parts'},$keys,16); # 16 is more than enough
  1168.  
  1169. print "Insert into table with $keys keys and with a primary key with $seg parts\n";
  1170.  
  1171. # Make keys on the most important types
  1172. @types=(0,0,0,1,0,0,0,1,1,1,1,1,1,1,1,1,1);    # A 1 for each char field
  1173. push(@fields,"field1 tinyint not null");
  1174. push(@fields,"field2 mediumint not null");
  1175. push(@fields,"field3 smallint not null");
  1176. push(@fields,"field4 char(16) not null");
  1177. push(@fields,"field5 integer not null");
  1178. push(@fields,"field6 float not null");
  1179. push(@fields,"field7 double not null");
  1180. for ($i=8 ; $i <= $keys ; $i++)
  1181. {
  1182.   push(@fields,"field$i char(6) not null");    # Should be relatively fair
  1183. }
  1184.  
  1185. # First key contains many segments
  1186. $query="primary key (";
  1187. for ($i= 1 ; $i <= $seg ; $i++)
  1188. {
  1189.   $query.= "field$i,";
  1190. }
  1191. substr($query,-1)=")";
  1192. push (@keys,$query);
  1193.  
  1194. #Create other keys
  1195. for ($i=2 ; $i <= $keys ; $i++)
  1196. {
  1197.   push(@keys,"index index$i (field$i)");
  1198. }
  1199.  
  1200. do_many($dbh,$server->create("bench1",\@fields,\@keys));
  1201. if ($opt_lock_tables)
  1202. {
  1203.   $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  1204. }
  1205.  
  1206. if ($server->small_rollback_segment())
  1207. {
  1208.   $dbh->disconnect;                # close connection
  1209.   $dbh = $server->connect();
  1210. }
  1211.  
  1212. $loop_time=new Benchmark;
  1213. $fields=$#fields;
  1214. if (($opt_fast || $opt_fast_insert) && $server->{'limits'}->{'insert_multi_value'})
  1215. {
  1216.   $query_size=$server->{'limits'}->{'query_size'};
  1217.   $query="insert into bench1 values ";
  1218.   $res=$query;
  1219.   for ($i=0; $i < $many_keys_loop_count; $i++)
  1220.   {
  1221.     $rand=$random[$i];
  1222.     $tmp="(" . ($i & 127) . ",$rand," . ($i & 32766) .
  1223.       ",'ABCDEF$rand',0,";
  1224.  
  1225.     for ($j=5; $j <= $fields ; $j++)
  1226.     {
  1227.       $tmp.= ($types[$j] == 0) ? "$rand," : "'$rand',";
  1228.     }
  1229.     substr($tmp,-1)=")";
  1230.     if (length($tmp)+length($res) < $query_size)
  1231.     {
  1232.       $res.= $tmp . ",";
  1233.     }
  1234.     else
  1235.     {
  1236.       $sth = $dbh->do(substr($res,0,length($res)-1)) or die $DBI::errstr;
  1237.       $res=$query . $tmp . ",";
  1238.     }
  1239.   }
  1240.   $sth = $dbh->do(substr($res,0,length($res)-1)) or die $DBI::errstr;
  1241. }
  1242. else
  1243. {
  1244.   for ($i=0; $i < $many_keys_loop_count; $i++)
  1245.   {
  1246.     $rand=$random[$i];
  1247.     $query="insert into bench1 values (" . ($i & 127) . ",$rand," . ($i & 32767) .
  1248.       ",'ABCDEF$rand',0,";
  1249.  
  1250.     for ($j=5; $j <= $fields ; $j++)
  1251.     {
  1252.       $query.= ($types[$j] == 0) ? "$rand," : "'$rand',";
  1253.     }
  1254.     substr($query,-1)=")";
  1255.     print "query1: $query\n" if ($opt_debug);
  1256.     $dbh->do($query) or die "Got error $DBI::errstr with query: $query\n";
  1257.   }
  1258. }
  1259. $end_time=new Benchmark;
  1260. print "Time for insert_key ($many_keys_loop_count): " .
  1261.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1262.  
  1263. if ($server->small_rollback_segment())
  1264. {
  1265.   $dbh->disconnect;                # close connection
  1266.   $dbh = $server->connect();
  1267. }
  1268. if ($opt_fast && defined($server->{vacuum}))
  1269. {
  1270.   if ($opt_lock_tables)
  1271.   {
  1272.     do_query($dbh,"UNLOCK TABLES");
  1273.   }
  1274.   $server->vacuum(1,\$dbh,"bench1");
  1275.   if ($opt_lock_tables)
  1276.   {
  1277.     $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  1278.   }
  1279. }
  1280.  
  1281. #
  1282. # update one key of the above
  1283. #
  1284.  
  1285. print "Testing update of keys\n";
  1286. $loop_time=new Benchmark;
  1287. for ($i=0 ; $i< 256; $i++)
  1288. {
  1289.   $dbh->do("update bench1 set field5=1 where field1=$i")
  1290.     or die "Got error $DBI::errstr with query: update bench1 set field5=1 where field1=$i\n";
  1291. }
  1292. $end_time=new Benchmark;
  1293. print "Time for update_of_primary_key_many_keys (256): " .
  1294.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1295.  
  1296. if ($server->small_rollback_segment())
  1297. {
  1298.   $dbh->disconnect;                # close connection
  1299.   $dbh = $server->connect();
  1300. }
  1301. if ($opt_fast && defined($server->{vacuum}))
  1302. {
  1303.   if ($opt_lock_tables)
  1304.   {
  1305.     do_query($dbh,"UNLOCK TABLES");
  1306.   }
  1307.   $server->vacuum(1,\$dbh,"bench1");
  1308.   if ($opt_lock_tables)
  1309.   {
  1310.     $sth = $dbh->do("LOCK TABLES bench1 WRITE") || die $DBI::errstr;
  1311.   }
  1312. }
  1313.  
  1314. if ($server->small_rollback_segment())
  1315. {
  1316.   $dbh->disconnect;                # close connection
  1317.   $dbh = $server->connect();
  1318. }
  1319.  
  1320. #
  1321. # Delete everything from table
  1322. #
  1323.  
  1324. print "Deleting rows from the table\n";
  1325. $loop_time=new Benchmark;
  1326. $count=0;
  1327.  
  1328. for ($i=0 ; $i < 128 ; $i++)
  1329. {
  1330.   $count++;
  1331.   $dbh->do("delete from bench1 where field1 = $i") or die $DBI::errstr;
  1332. }
  1333.  
  1334. $end_time=new Benchmark;
  1335. print "Time for delete_big_many_keys ($count): " .
  1336. timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1337.  
  1338. print "Deleting everything from table\n";
  1339. $count=1;
  1340. if ($opt_fast)
  1341. {
  1342.   $dbh->do("delete from bench1") or die $DBI::errstr;
  1343. }
  1344. else
  1345. {
  1346.   $dbh->do("delete from bench1 where field1 > 0") or die $DBI::errstr;
  1347. }
  1348.  
  1349. if ($opt_lock_tables)
  1350. {
  1351.   $sth = $dbh->do("UNLOCK TABLES") || die $DBI::errstr;
  1352. }
  1353.  
  1354. $end_time=new Benchmark;
  1355. print "Time for delete_all_many_keys ($count): " .
  1356.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1357.  
  1358. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'}) or die $DBI::errstr;
  1359. if ($opt_fast && defined($server->{vacuum}))
  1360. {
  1361.   $server->vacuum(1,\$dbh);
  1362. }
  1363.  
  1364. #
  1365. # Test multi value inserts if the server supports it
  1366. #
  1367.  
  1368. if ($limits->{'insert_multi_value'})
  1369. {
  1370.   $query_size=$limits->{'query_size'}; # Same limit for all databases
  1371.  
  1372.   $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'});
  1373.   do_many($dbh,$server->create("bench1",
  1374.                    ["id int NOT NULL",
  1375.                 "id2 int NOT NULL",
  1376.                 "id3 int NOT NULL",
  1377.                 "dummy1 char(30)"],
  1378.                    ["primary key (id,id2)",
  1379.                    "index index_id3 (id3)"]));
  1380.   if ($opt_lock_tables)
  1381.   {
  1382.     $sth = $dbh->do("LOCK TABLES bench1 write") || die $DBI::errstr;
  1383.   }
  1384.  
  1385.   $loop_time=new Benchmark;
  1386.   print "Inserting $opt_loop_count rows with multiple values\n";
  1387.   $query="insert into bench1 values ";
  1388.   $res=$query;
  1389.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  1390.   {
  1391.     my $tmp= "($i,$i,$i,'EFGHIJKLM'),";
  1392.     if (length($i)+length($res) < $query_size)
  1393.     {
  1394.       $res.= $tmp;
  1395.     }
  1396.     else
  1397.     {
  1398.       do_query($dbh,substr($res,0,length($res)-1));
  1399.       $res=$query .$tmp;
  1400.     }
  1401.   }
  1402.   do_query($dbh,substr($res,0,length($res)-1));
  1403.  
  1404.   if ($opt_lock_tables)
  1405.   {
  1406.     $sth = $dbh->do("UNLOCK TABLES ") || die $DBI::errstr;
  1407.   }
  1408.  
  1409.   $end_time=new Benchmark;
  1410.   print "Time for multiple_value_insert (" . ($opt_loop_count) . "): " .
  1411.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1412.  
  1413.   if ($opt_fast && defined($server->{vacuum}))
  1414.   {
  1415.     $server->vacuum(1,\$dbh);
  1416.   }
  1417.   if ($opt_lock_tables)
  1418.   {
  1419.     $sth = $dbh->do("UNLOCK TABLES ") || die $DBI::errstr;
  1420.   }
  1421.  
  1422.   # A big table may take a while to drop
  1423.   $loop_time=new Benchmark;
  1424.   $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'}) or die $DBI::errstr;
  1425.   $end_time=new Benchmark;
  1426.   print "Time for drop table(1): " .
  1427.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  1428. }
  1429.  
  1430. ####
  1431. #### End of benchmark
  1432. ####
  1433.  
  1434. $dbh->disconnect;                # close connection
  1435.  
  1436. end_benchmark($start_time);
  1437.  
  1438. ###
  1439. ### Some help functions
  1440. ###
  1441.  
  1442.  
  1443. # Do some sample selects on direct key
  1444. # First select finds a row, the second one doesn't find.
  1445.  
  1446. sub check_select_key
  1447. {
  1448.   my ($sel_columns,$column,$check)= @_;
  1449.   my ($loop_time,$end_time,$i,$tmp_var,$tmp,$count,$row_count,$estimated);
  1450.  
  1451.   $estimated=0;
  1452.   $loop_time=new Benchmark;
  1453.   $count=0;
  1454.   for ($i=1 ; $i <= $opt_read_key_loop_count; $i++)
  1455.   {
  1456.     $count+=2;
  1457.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $opt_loop_count);
  1458.     $tmp=$tmpvar % ($total_rows);
  1459.     fetch_all_rows($dbh,"select $sel_columns from bench1 where $column=$tmp")
  1460.       or die $DBI::errstr;
  1461.     $tmp+=$total_rows;
  1462.     defined($row_count=fetch_all_rows($dbh,"select $sel_columns from bench1 where $column=$tmp")) or die $DBI::errstr;
  1463.     die "Found $row_count rows on impossible id: $tmp\n" if ($row_count);
  1464.     $end_time=new Benchmark;
  1465.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i,
  1466.                        $opt_loop_count));
  1467.   }
  1468.   if ($estimated)
  1469.   { print "Estimated time"; }
  1470.   else
  1471.   { print "Time"; }
  1472.   print " for $check ($count): " .
  1473.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1474. }
  1475.  
  1476. # Same as above, but select on 2 columns
  1477.  
  1478. sub check_select_key2
  1479. {
  1480.   my ($sel_columns,$column,$column2,$check)= @_;
  1481.   my ($loop_time,$end_time,$i,$tmp_var,$tmp,$count,$row_count,$estimated);
  1482.  
  1483.   $estimated=0;
  1484.   $loop_time=new Benchmark;
  1485.   $count=0;
  1486.   for ($i=1 ; $i <= $opt_read_key_loop_count; $i++)
  1487.   {
  1488.     $count+=2;
  1489.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $opt_loop_count);
  1490.     $tmp=$tmpvar % ($total_rows);
  1491.     fetch_all_rows($dbh,"select $sel_columns from bench1 where $column=$tmp and $column2=$tmp")
  1492.       or die $DBI::errstr;
  1493.     $tmp+=$total_rows;
  1494.     defined($row_count=fetch_all_rows($dbh,"select $sel_columns from bench1 where $column=$tmp and $column2=$tmp")) or die $DBI::errstr;
  1495.     die "Found $row_count rows on impossible id: $tmp\n" if ($row_count);
  1496.     $end_time=new Benchmark;
  1497.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$i,
  1498.                        $opt_loop_count));
  1499.   }
  1500.   if ($estimated)
  1501.   { print "Estimated time"; }
  1502.   else
  1503.   { print "Time"; }
  1504.   print " for $check ($count): " .
  1505.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1506. }
  1507.  
  1508. #
  1509. # Search using some very simple queries
  1510. #
  1511.  
  1512. sub check_select_range
  1513. {
  1514.   my ($column,$check)= @_;
  1515.   my ($loop_time,$end_time,$i,$tmp_var,$tmp,$query,$rows,$estimated);
  1516.  
  1517.   $estimated=0;
  1518.   $loop_time=new Benchmark;
  1519.   $found=$count=0;
  1520.   for ($test=1 ; $test <= $range_loop_count; $test++)
  1521.   {
  1522.     $count+=$#Q+1;
  1523.     for ($i=0 ; $i < $#Q ; $i+=2)
  1524.     {
  1525.       $query=$Q[$i];
  1526.       $rows=$Q[$i+1];
  1527.       $query =~ s/!id!/$column/g;
  1528.       if (($row_count=fetch_all_rows($dbh,$query)) != $rows)
  1529.       {
  1530.     if ($row_count == undef())
  1531.     {
  1532.       die "Got error: $DBI::errstr when executing $query\n";
  1533.     }
  1534.     die "'$query' returned wrong number of rows: $row_count instead of $rows\n";
  1535.       }
  1536.       $found+=$row_count;
  1537.     }
  1538.     $end_time=new Benchmark;
  1539.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$test,
  1540.                        $range_loop_count));
  1541.   }
  1542.   if ($estimated)
  1543.   { print "Estimated time"; }
  1544.   else
  1545.   { print "Time"; }
  1546.   print " for $check ($count:$found): " .
  1547.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1548. }
  1549.  
  1550.  
  1551. #
  1552. # SELECT * from bench where col=x or col=x or col=x ...
  1553.  
  1554.  
  1555. sub check_or_range
  1556. {
  1557.   my ($column,$check)= @_;
  1558.   my ($loop_time,$end_time,$i,$tmp_var,$tmp,$columns,$estimated,$found,
  1559.       $or_part,$count,$loop_count);
  1560.  
  1561.   $columns=min($limits->{'max_columns'},50,($limits->{'query_size'}-50)/13);
  1562.   $columns=$columns- ($columns % 4); # Make Divisible by 4
  1563.  
  1564.   $estimated=0;
  1565.   $loop_time=new Benchmark;
  1566.   $found=0;
  1567.   # The number of tests must be divisible by the following
  1568.   $tmp= $limits->{'func_extra_in_num'} ? 15 : 10; 
  1569.   # We need to calculate the exact number of test to make 'Estimated' right
  1570.   $loop_count=$range_loop_count*10+$tmp-1;
  1571.   $loop_count=$loop_count- ($loop_count % $tmp);
  1572.   
  1573.   for ($count=0 ; $count < $loop_count ; )
  1574.   {
  1575.     for ($rowcnt=0; $rowcnt <= $columns; $rowcnt+= $columns/4)
  1576.     {
  1577.       my $query="select * from bench1 where ";
  1578.       my $or_part= "$column = 1";
  1579.       $count+=2;
  1580.  
  1581.       for ($i=1 ; $i < $rowcnt ; $i++)
  1582.       {
  1583.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $opt_loop_count);
  1584.     $tmp=$tmpvar % ($opt_loop_count*4);
  1585.     $or_part.=" or $column=$tmp";
  1586.       }
  1587.       print $query . $or_part . "\n" if ($opt_debug);
  1588.       ($rows=fetch_all_rows($dbh,$query . $or_part)) or die $DBI::errstr;
  1589.       $found+=$rows;
  1590.  
  1591.       if ($limits->{'func_extra_in_num'})
  1592.       {
  1593.     my $in_part=$or_part;    # Same query, but use 'func_extra_in_num' instead.
  1594.     $in_part=~ s/ = / IN \(/;
  1595.     $in_part=~ s/ or $column=/,/g;
  1596.     $in_part.= ")";
  1597.     fetch_all_rows($dbh,$query . $in_part) or die $DBI::errstr;
  1598.     $count++;
  1599.       }
  1600.       # Do it a little harder by setting a extra range
  1601.       defined(($rows=fetch_all_rows($dbh,"$query($or_part) and $column < 10"))) or die $DBI::errstr;
  1602.       $found+=$rows;
  1603.     }
  1604.     $end_time=new Benchmark;
  1605.     last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$count,
  1606.                        $loop_count));
  1607.   }
  1608.  
  1609.   if ($estimated)
  1610.   { print "Estimated time"; }
  1611.   else
  1612.   { print "Time"; }
  1613.   print " for $check ($count:$found): " .
  1614.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  1615. }
  1616.