3 * Benchmark SQL DELETE vs SQL TRUNCATE.
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.
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.
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
24 require_once __DIR__
. '/Benchmarker.php';
26 use Wikimedia\Rdbms\IDatabase
;
27 use Wikimedia\Rdbms\IMaintainableDatabase
;
30 * Maintenance script that benchmarks SQL DELETE vs SQL TRUNCATE.
34 class BenchmarkDeleteTruncate
extends Benchmarker
{
35 protected $defaultCount = 10;
37 public function __construct() {
38 parent
::__construct();
39 $this->addDescription( 'Benchmarks SQL DELETE vs SQL TRUNCATE.' );
42 public function execute() {
43 $dbw = $this->getDB( DB_MASTER
);
45 $test = $dbw->tableName( 'test' );
46 $dbw->query( "CREATE TABLE IF NOT EXISTS /*_*/$test (
47 test_id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
48 text varbinary(255) NOT NULL
53 'setup' => function () use ( $dbw ) {
54 $this->insertData( $dbw );
56 'function' => function () use ( $dbw ) {
57 $this->delete( $dbw );
61 'setup' => function () use ( $dbw ) {
62 $this->insertData( $dbw );
64 'function' => function () use ( $dbw ) {
65 $this->truncate( $dbw );
70 $dbw->dropTable( 'test' );
74 * @param IDatabase $dbw
77 private function insertData( $dbw ) {
78 $range = range( 0, 1024 );
80 foreach ( $range as $r ) {
81 $data[] = [ 'text' => $r ];
83 $dbw->insert( 'test', $data, __METHOD__
);
87 * @param IDatabase $dbw
90 private function delete( $dbw ) {
91 $dbw->delete( 'text', '*', __METHOD__
);
95 * @param IMaintainableDatabase $dbw
98 private function truncate( $dbw ) {
99 $test = $dbw->tableName( 'test' );
100 $dbw->query( "TRUNCATE TABLE $test" );
104 $maintClass = 'BenchmarkDeleteTruncate';
105 require_once RUN_MAINTENANCE_IF_MAIN
;