]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - docs/database.txt
MediaWiki 1.11.0-scripts
[autoinstallsdev/mediawiki.git] / docs / database.txt
1 Some information about database access in MediaWiki.
2 By Tim Starling, January 2006.
3
4 ------------------------------------------------------------------------
5     Database layout
6 ------------------------------------------------------------------------
7
8 For information about the MediaWiki database layout, such as a 
9 description of the tables and their contents, please see:
10   http://www.mediawiki.org/wiki/Manual:Database_layout
11   http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/maintenance/tables.sql?view=markup
12
13
14 ------------------------------------------------------------------------
15     API
16 ------------------------------------------------------------------------
17
18 To make a read query, something like this usually suffices:
19
20 $dbr = wfGetDB( DB_SLAVE );
21 $res = $dbr->select( /* ...see docs... */ );
22 while ( $row = $dbr->fetchObject( $res ) ) {
23         ...
24 }
25 $dbr->freeResult( $res );
26
27 Note the assignment operator in the while condition.
28
29 For a write query, use something like:
30
31 $dbw = wfGetDB( DB_MASTER );
32 $dbw->insert( /* ...see docs... */ );
33
34 We use the convention $dbr for read and $dbw for write to help you keep
35 track of whether the database object is a slave (read-only) or a master
36 (read/write). If you write to a slave, the world will explode. Or to be
37 precise, a subsequent write query which succeeded on the master may fail
38 when replicated to the slave due to a unique key collision. Replication
39 on the slave will stop and it may take hours to repair the database and
40 get it back online. Setting read_only in my.cnf on the slave will avoid
41 this scenario, but given the dire consequences, we prefer to have as
42 many checks as possible.
43
44 We provide a query() function for raw SQL, but the wrapper functions
45 like select() and insert() are usually more convenient. They take care
46 of things like table prefixes and escaping for you. If you really need
47 to make your own SQL, please read the documentation for tableName() and
48 addQuotes(). You will need both of them.
49
50
51 ------------------------------------------------------------------------
52     Basic query optimisation
53 ------------------------------------------------------------------------
54
55 MediaWiki developers who need to write DB queries should have some
56 understanding of databases and the performance issues associated with
57 them. Patches containing unacceptably slow features will not be
58 accepted. Unindexed queries are generally not welcome in MediaWiki,
59 except in special pages derived from QueryPage. It's a common pitfall
60 for new developers to submit code containing SQL queries which examine
61 huge numbers of rows. Remember that COUNT(*) is O(N), counting rows in a
62 table is like counting beans in a bucket.
63
64
65 ------------------------------------------------------------------------
66     Replication
67 ------------------------------------------------------------------------
68
69 The largest installation of MediaWiki, Wikimedia, uses a large set of
70 slave MySQL servers replicating writes made to a master MySQL server. It
71 is important to understand the issues associated with this setup if you
72 want to write code destined for Wikipedia.
73
74 It's often the case that the best algorithm to use for a given task
75 depends on whether or not replication is in use. Due to our unabashed
76 Wikipedia-centrism, we often just use the replication-friendly version,
77 but if you like, you can use $wgLoadBalancer->getServerCount() > 1 to
78 check to see if replication is in use.
79
80 === Lag ===
81
82 Lag primarily occurs when large write queries are sent to the master.
83 Writes on the master are executed in parallel, but they are executed in
84 serial when they are replicated to the slaves. The master writes the
85 query to the binlog when the transaction is committed. The slaves poll
86 the binlog and start executing the query as soon as it appears. They can
87 service reads while they are performing a write query, but will not read
88 anything more from the binlog and thus will perform no more writes. This
89 means that if the write query runs for a long time, the slaves will lag
90 behind the master for the time it takes for the write query to complete.
91
92 Lag can be exacerbated by high read load. MediaWiki's load balancer will
93 stop sending reads to a slave when it is lagged by more than 30 seconds.
94 If the load ratios are set incorrectly, or if there is too much load
95 generally, this may lead to a slave permanently hovering around 30
96 seconds lag.
97
98 If all slaves are lagged by more than 30 seconds, MediaWiki will stop
99 writing to the database. All edits and other write operations will be
100 refused, with an error returned to the user. This gives the slaves a
101 chance to catch up. Before we had this mechanism, the slaves would
102 regularly lag by several minutes, making review of recent edits
103 difficult.
104
105 In addition to this, MediaWiki attempts to ensure that the user sees
106 events occurring on the wiki in chronological order. A few seconds of lag
107 can be tolerated, as long as the user sees a consistent picture from
108 subsequent requests. This is done by saving the master binlog position
109 in the session, and then at the start of each request, waiting for the
110 slave to catch up to that position before doing any reads from it. If
111 this wait times out, reads are allowed anyway, but the request is
112 considered to be in "lagged slave mode". Lagged slave mode can be
113 checked by calling $wgLoadBalancer->getLaggedSlaveMode(). The only
114 practical consequence at present is a warning displayed in the page
115 footer.
116
117 === Lag avoidance ===
118
119 To avoid excessive lag, queries which write large numbers of rows should
120 be split up, generally to write one row at a time. Multi-row INSERT ...
121 SELECT queries are the worst offenders should be avoided altogether.
122 Instead do the select first and then the insert.
123
124 === Working with lag ===
125
126 Despite our best efforts, it's not practical to guarantee a low-lag
127 environment. Lag will usually be less than one second, but may
128 occasionally be up to 30 seconds. For scalability, it's very important
129 to keep load on the master low, so simply sending all your queries to
130 the master is not the answer. So when you have a genuine need for
131 up-to-date data, the following approach is advised:
132
133 1) Do a quick query to the master for a sequence number or timestamp 2)
134 Run the full query on the slave and check if it matches the data you got
135 from the master 3) If it doesn't, run the full query on the master
136
137 To avoid swamping the master every time the slaves lag, use of this
138 approach should be kept to a minimum. In most cases you should just read
139 from the slave and let the user deal with the delay.
140
141
142 ------------------------------------------------------------------------
143     Lock contention
144 ------------------------------------------------------------------------
145
146 Due to the high write rate on Wikipedia (and some other wikis),
147 MediaWiki developers need to be very careful to structure their writes
148 to avoid long-lasting locks. By default, MediaWiki opens a transaction
149 at the first query, and commits it before the output is sent. Locks will
150 be held from the time when the query is done until the commit. So you
151 can reduce lock time by doing as much processing as possible before you
152 do your write queries. Update operations which do not require database
153 access can be delayed until after the commit by adding an object to
154 $wgPostCommitUpdateList.
155
156 Often this approach is not good enough, and it becomes necessary to
157 enclose small groups of queries in their own transaction. Use the
158 following syntax:
159
160 $dbw = wfGetDB( DB_MASTER );
161 $dbw->immediateBegin();
162 /* Do queries */
163 $dbw->immediateCommit();
164
165 There are functions called begin() and commit() but they don't do what
166 you would expect. Don't use them.
167
168 Use of locking reads (e.g. the FOR UPDATE clause) is not advised. They
169 are poorly implemented in InnoDB and will cause regular deadlock errors.
170 It's also surprisingly easy to cripple the wiki with lock contention. If
171 you must use them, define a new flag for $wgAntiLockFlags which allows
172 them to be turned off, because we'll almost certainly need to do so on
173 the Wikimedia cluster.
174
175 Instead of locking reads, combine your existence checks into your write
176 queries, by using an appropriate condition in the WHERE clause of an
177 UPDATE, or by using unique indexes in combination with INSERT IGNORE.
178 Then use the affected row count to see if the query succeeded.
179