MediaWiki master
runBatchedQuery.php
Go to the documentation of this file.
1<?php
26// @codeCoverageIgnoreStart
27require_once __DIR__ . '/Maintenance.php';
28// @codeCoverageIgnoreEnd
29
31
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription(
41 "Run an update query on all rows of a table. " .
42 "Waits for replicas at appropriate intervals." );
43 $this->addOption( 'table', 'The table name', true, true );
44 $this->addOption( 'set', 'The SET clause', true, true );
45 $this->addOption( 'where', 'The WHERE clause', false, true );
46 $this->addOption( 'key', 'A column name, the values of which are unique', true, true );
47 $this->addOption( 'batch-size', 'The batch size (default 1000)', false, true );
48 $this->addOption( 'db', 'The database name, or omit to use the current wiki.', false, true );
49 }
50
51 public function execute() {
52 $table = $this->getOption( 'table' );
53 $key = $this->getOption( 'key' );
54 $set = $this->getOption( 'set' );
55 $where = $this->getOption( 'where', null );
56 $where = $where === null ? [] : [ $where ];
57 $batchSize = $this->getOption( 'batch-size', 1000 );
58
59 $dbName = $this->getOption( 'db', null );
60 if ( $dbName === null ) {
61 $dbw = $this->getPrimaryDB();
62 } else {
63 $dbw = $this->getServiceContainer()->getConnectionProvider()->getPrimaryDatabase( $dbName );
64 }
65
66 $queryBuilder = $dbw->newSelectQueryBuilder()
67 ->select( $key )
68 ->from( $table )
69 ->where( $where )
70 ->caller( __METHOD__ );
71
72 $iterator = new BatchRowIterator( $dbw, $queryBuilder, $key, $batchSize );
73 foreach ( $iterator as $n => $batch ) {
74 $this->output( "Batch $n: " );
75
76 // Note that the update conditions do not rely on the atomicity of the
77 // SELECT query in order to guarantee that all rows are updated. The
78 // results of the SELECT are merely a partitioning hint. Simultaneous
79 // updates merely result in the wrong number of rows being updated
80 // in a batch.
81
82 $firstRow = reset( $batch );
83 $lastRow = end( $batch );
84
85 $dbw->newUpdateQueryBuilder()
86 ->table( $table )
87 ->set( $set )
88 ->where( $where )
89 ->andWhere( $dbw->expr( $key, '>=', $firstRow->$key ) )
90 ->andWhere( $dbw->expr( $key, '<=', $lastRow->$key ) )
91 ->caller( __METHOD__ )
92 ->execute();
93
94 $affected = $dbw->affectedRows();
95 $this->output( "$affected rows affected\n" );
96 $this->waitForReplication();
97 }
98 }
99
101 public function getDbType() {
102 return Maintenance::DB_ADMIN;
103 }
104}
105
106// @codeCoverageIgnoreStart
107$maintClass = RunBatchedQuery::class;
108require_once RUN_MAINTENANCE_IF_MAIN;
109// @codeCoverageIgnoreEnd
Allows iterating a large number of rows in batches transparently.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Maintenance script to run a database query in batches and wait for replica DBs.
__construct()
Default constructor.
execute()
Do the actual work.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...