MediaWiki
master
Toggle main menu visibility
runBatchedQuery.php
Go to the documentation of this file.
1
<?php
11
12
// @codeCoverageIgnoreStart
13
require_once __DIR__ .
'/Maintenance.php'
;
14
// @codeCoverageIgnoreEnd
15
16
use
MediaWiki\Maintenance\Maintenance
;
17
use
MediaWiki\Utils\BatchRowIterator
;
18
24
class
RunBatchedQuery
extends
Maintenance
{
25
public
function
__construct
() {
26
parent::__construct();
27
$this->addDescription(
28
"Run an update query on all rows of a table. "
.
29
"Waits for replicas at appropriate intervals."
);
30
$this->addOption(
'table'
,
'The table name'
,
true
,
true
);
31
$this->addOption(
'set'
,
'The SET clause'
,
true
,
true
);
32
$this->addOption(
'where'
,
'The WHERE clause'
,
false
,
true
);
33
$this->addOption(
'key'
,
'A column name, the values of which are unique'
,
true
,
true
);
34
$this->addOption(
'batch-size'
,
'The batch size (default 1000)'
,
false
,
true
);
35
$this->addOption(
'db'
,
'The database name, or omit to use the current wiki.'
,
false
,
true
);
36
}
37
38
public
function
execute
() {
39
$table = $this->getOption(
'table'
);
40
$key = $this->getOption(
'key'
);
41
$set = $this->getOption(
'set'
);
42
$where = $this->getOption(
'where'
,
null
);
43
$where = $where ===
null
? [] : [ $where ];
44
$batchSize = $this->getOption(
'batch-size'
, 1000 );
45
46
$dbName = $this->getOption(
'db'
,
null
);
47
if
( $dbName ===
null
) {
48
$dbw = $this->getPrimaryDB();
49
}
else
{
50
$dbw = $this->getServiceContainer()->getConnectionProvider()->getPrimaryDatabase( $dbName );
51
}
52
53
$queryBuilder = $dbw->newSelectQueryBuilder()
54
->select( $key )
55
->from( $table )
56
->where( $where )
57
->caller( __METHOD__ );
58
59
$iterator =
new
BatchRowIterator
( $dbw, $queryBuilder, $key, $batchSize );
60
foreach
( $iterator as $n => $batch ) {
61
$this->output(
"Batch $n: "
);
62
63
// Note that the update conditions do not rely on the atomicity of the
64
// SELECT query in order to guarantee that all rows are updated. The
65
// results of the SELECT are merely a partitioning hint. Simultaneous
66
// updates merely result in the wrong number of rows being updated
67
// in a batch.
68
69
$firstRow = reset( $batch );
70
$lastRow = end( $batch );
71
72
$dbw->newUpdateQueryBuilder()
73
->table( $table )
74
->set( $set )
75
->where( $where )
76
->andWhere( $dbw->expr( $key,
'>='
, $firstRow->$key ) )
77
->andWhere( $dbw->expr( $key,
'<='
, $lastRow->$key ) )
78
->caller( __METHOD__ )
79
->execute();
80
81
$affected = $dbw->affectedRows();
82
$this->output(
"$affected rows affected\n"
);
83
$this->waitForReplication();
84
}
85
}
86
88
public
function
getDbType
() {
89
return
Maintenance::DB_ADMIN;
90
}
91
}
92
93
// @codeCoverageIgnoreStart
94
$maintClass
= RunBatchedQuery::class;
95
require_once RUN_MAINTENANCE_IF_MAIN;
96
// @codeCoverageIgnoreEnd
$maintClass
$maintClass
Definition
addChangeTag.php:56
MediaWiki\Utils\BatchRowIterator
Allows iterating a large number of rows in batches transparently.
Definition
BatchRowIterator.php:26
RunBatchedQuery
Maintenance script to run a database query in batches and wait for replica DBs.
Definition
runBatchedQuery.php:24
RunBatchedQuery\__construct
__construct()
Definition
runBatchedQuery.php:25
RunBatchedQuery\execute
execute()
Definition
runBatchedQuery.php:38
RunBatchedQuery\getDbType
getDbType()
Definition
runBatchedQuery.php:88
MediaWiki\Maintenance
Definition
applyChangeTag.php:11
maintenance
runBatchedQuery.php
Generated on
for MediaWiki by
1.18.0