bench_delete_truncate.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Benchmark SQL DELETE vs SQL TRUNCATE.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Benchmark
  22. */
  23. require_once __DIR__ . '/Benchmarker.php';
  24. /**
  25. * Maintenance script that benchmarks SQL DELETE vs SQL TRUNCATE.
  26. *
  27. * @ingroup Benchmark
  28. */
  29. class BenchmarkDeleteTruncate extends Benchmarker {
  30. public function __construct() {
  31. parent::__construct();
  32. $this->addDescription( 'Benchmarks SQL DELETE vs SQL TRUNCATE.' );
  33. }
  34. public function execute() {
  35. $dbw = $this->getDB( DB_MASTER );
  36. $test = $dbw->tableName( 'test' );
  37. $dbw->query( "CREATE TABLE IF NOT EXISTS /*_*/$test (
  38. test_id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
  39. text varbinary(255) NOT NULL
  40. );" );
  41. $this->insertData( $dbw );
  42. $start = microtime( true );
  43. $this->delete( $dbw );
  44. $end = microtime( true );
  45. echo "Delete: " . sprintf( "%6.3fms", ( $end - $start ) * 1000 );
  46. echo "\r\n";
  47. $this->insertData( $dbw );
  48. $start = microtime( true );
  49. $this->truncate( $dbw );
  50. $end = microtime( true );
  51. echo "Truncate: " . sprintf( "%6.3fms", ( $end - $start ) * 1000 );
  52. echo "\r\n";
  53. $dbw->dropTable( 'test' );
  54. }
  55. /**
  56. * @param Database $dbw
  57. * @return void
  58. */
  59. private function insertData( $dbw ) {
  60. $range = range( 0, 1024 );
  61. $data = [];
  62. foreach ( $range as $r ) {
  63. $data[] = [ 'text' => $r ];
  64. }
  65. $dbw->insert( 'test', $data, __METHOD__ );
  66. }
  67. /**
  68. * @param Database $dbw
  69. * @return void
  70. */
  71. private function delete( $dbw ) {
  72. $dbw->delete( 'text', '*', __METHOD__ );
  73. }
  74. /**
  75. * @param Database $dbw
  76. * @return void
  77. */
  78. private function truncate( $dbw ) {
  79. $test = $dbw->tableName( 'test' );
  80. $dbw->query( "TRUNCATE TABLE $test" );
  81. }
  82. }
  83. $maintClass = "BenchmarkDeleteTruncate";
  84. require_once RUN_MAINTENANCE_IF_MAIN;