$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select( /* ...see docs... */ );
-while ( $row = $dbr->fetchObject( $res ) ) {
+foreach ( $res as $row ) {
...
}
-$dbr->freeResult( $res );
-
-Note the assignment operator in the while condition.
For a write query, use something like:
It's often the case that the best algorithm to use for a given task
depends on whether or not replication is in use. Due to our unabashed
Wikipedia-centrism, we often just use the replication-friendly version,
-but if you like, you can use $wgLoadBalancer->getServerCount() > 1 to
+but if you like, you can use wfGetLB()->getServerCount() > 1 to
check to see if replication is in use.
=== Lag ===
slave to catch up to that position before doing any reads from it. If
this wait times out, reads are allowed anyway, but the request is
considered to be in "lagged slave mode". Lagged slave mode can be
-checked by calling $wgLoadBalancer->getLaggedSlaveMode(). The only
+checked by calling wfGetLB()->getLaggedSlaveMode(). The only
practical consequence at present is a warning displayed in the page
footer.
at the first query, and commits it before the output is sent. Locks will
be held from the time when the query is done until the commit. So you
can reduce lock time by doing as much processing as possible before you
-do your write queries. Update operations which do not require database
-access can be delayed until after the commit by adding an object to
-$wgPostCommitUpdateList.
+do your write queries.
Often this approach is not good enough, and it becomes necessary to
enclose small groups of queries in their own transaction. Use the
following syntax:
$dbw = wfGetDB( DB_MASTER );
-$dbw->immediateBegin();
+$dbw->begin();
/* Do queries */
-$dbw->immediateCommit();
-
-There are functions called begin() and commit() but they don't do what
-you would expect. Don't use them.
+$dbw->commit();
Use of locking reads (e.g. the FOR UPDATE clause) is not advised. They
are poorly implemented in InnoDB and will cause regular deadlock errors.
UPDATE, or by using unique indexes in combination with INSERT IGNORE.
Then use the affected row count to see if the query succeeded.
+------------------------------------------------------------------------
+ Supported DBMSs
+------------------------------------------------------------------------
+
+MediaWiki is written primarily for use with MySQL. Queries are optimized
+for it and its schema is considered the canonical version. However,
+MediaWiki does support the following other DBMSs to varying degrees.
+
+* PostgreSQL
+* SQLite
+* Oracle
+* IBM DB2
+* MSSQL
+
+More information can be found about each of these databases (known issues,
+level of support, extra configuration) in the "databases" subdirectory in
+this folder.
+
+------------------------------------------------------------------------
+ Use of GROUP BY
+------------------------------------------------------------------------
+
+MySQL supports GROUP BY without checking anything in the SELECT clause.
+Other DBMSs (especially Postgres) are stricter and require that all the
+non-aggregate items in the SELECT clause appear in the GROUP BY. For
+this reason, it is highly discouraged to use SELECT * with GROUP BY
+queries.
+