On 10/18/07, tango@svn.wikimedia.org tango@svn.wikimedia.org wrote:
Revision: 26830 Author: tango Date: 2007-10-18 19:19:37 +0000 (Thu, 18 Oct 2007)
Log Message:
Show changes to pages linked to via a redirect on Special:Recentchangeslinked
. . . rc_new_len, rc_deleted " . ($uid ? ",wl_user" : "") . "
- FROM $pagelinks, $recentchanges
- FROM $pagelinks, $recentchanges, $redirect, $page
" . ($uid ? " LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") . " WHERE rc_timestamp > '{$cutoff}' {$cmq}
AND pl_namespace=rc_namespace
AND (pl_namespace=rc_namespace AND pl_title=rc_title
AND pl_from=$id
AND pl_from=$id)OR (rd_namespace=rc_namespaceAND rd_title=rc_titleAND rd_from=page_idAND page_namespace=pl_namespaceAND page_title=pl_titleAND pl_from=$id)$GROUPBY "; }
At first inspection, this appears to cause the query to be executed as a Cartesian join (query run on a fairly old dump of the Simple English Wikipedia on localhost with some random parameters).
mysql> EXPLAIN SELECT * FROM pagelinks, recentchanges, redirect, page WHERE rc_timestamp > '20071011094400' AND (pl_namespace=rc_namespace AND pl_title=rc_title) OR (rd_namespace=rc_namespace AND rd_title=rc_title AND rd_from=page_id AND page_namespace=pl_namespace AND page_title=pl_title) AND pl_from=1234; +----+-------------+---------------+-------+------------------------------------------------+-------------+---------+------+---------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------------+-------+------------------------------------------------+-------------+---------+------+---------+--------------------------+ | 1 | SIMPLE | pagelinks | index | pl_from,pl_namespace | pl_from | 265 | NULL | 1078997 | Using index | | 1 | SIMPLE | recentchanges | ALL | rc_timestamp,rc_namespace_title,rc_ns_usertext | NULL | NULL | NULL | 176 | Using where | | 1 | SIMPLE | page | ALL | PRIMARY,name_title | NULL | NULL | NULL | 43475 | Using where | | 1 | SIMPLE | redirect | index | PRIMARY,rd_ns_title | rd_ns_title | 265 | NULL | 8185 | Using where; Using index | +----+-------------+---------------+-------+------------------------------------------------+-------------+---------+------+---------+--------------------------+ 4 rows in set (0.00 sec)
Note the join types, and the row numbers. The old code was a simple ref plus range scan:
mysql> EXPLAIN SELECT * FROM pagelinks, recentchanges WHERE rc_timestamp > '20071011094400' AND (pl_namespace=rc_namespace AND pl_title=rc_title) AND pl_from=1234; +----+-------------+---------------+-------+------------------------------------------------+--------------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------------+-------+------------------------------------------------+--------------+---------+-------+------+-------------+ | 1 | SIMPLE | pagelinks | ref | pl_from,pl_namespace | pl_from | 4 | const | 1 | Using index | | 1 | SIMPLE | recentchanges | range | rc_timestamp,rc_namespace_title,rc_ns_usertext | rc_timestamp | 16 | NULL | 1 | Using where | +----+-------------+---------------+-------+------------------------------------------------+--------------+---------+-------+------+-------------+ 2 rows in set (0.00 sec)
Generally speaking, OR does not make MySQL happy. UNION is a good deal better:
mysql> EXPLAIN SELECT rc_namespace, rc_title FROM pagelinks, recentchanges WHERE rc_timestamp > '20071011094400' AND (pl_namespace=rc_namespace AND pl_title=rc_title) AND pl_from=1234 UNION SELECT rc_namespace, rc_title FROM pagelinks, recentchanges, redirect, page WHERE rc_timestamp > '20071011094400' AND rd_namespace=rc_namespace AND rd_title=rc_title AND rd_from=page_id AND page_namespace=pl_namespace AND page_title=pl_title AND pl_from=1234; +----+--------------+---------------+--------+------------------------------------------------+--------------+---------+-----------------------------------------------------------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------+---------------+--------+------------------------------------------------+--------------+---------+-----------------------------------------------------------------+------+-------------+ | 1 | PRIMARY | pagelinks | ref | pl_from,pl_namespace | pl_from | 4 | const | 1 | Using index | | 1 | PRIMARY | recentchanges | range | rc_timestamp,rc_namespace_title,rc_ns_usertext | rc_timestamp | 16 | NULL | 1 | Using where | | 2 | UNION | pagelinks | ref | pl_from,pl_namespace | pl_from | 4 | const | 1 | Using index | | 2 | UNION | recentchanges | range | rc_timestamp,rc_namespace_title,rc_ns_usertext | rc_timestamp | 16 | NULL | 1 | Using where | | 2 | UNION | redirect | ref | PRIMARY,rd_ns_title | rd_ns_title | 261 | wikidb.recentchanges.rc_namespace,wikidb.recentchanges.rc_title | 1 | Using index | | 2 | UNION | page | eq_ref | PRIMARY,name_title | PRIMARY | 4 | wikidb.redirect.rd_from | 1 | Using where | | NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | | +----+--------------+---------------+--------+------------------------------------------------+--------------+---------+-----------------------------------------------------------------+------+-------------+ 7 rows in set (0.00 sec)
Although not really good, given the row counts. And given the randomness of my parameters and the oddity of my database (e.g. it's months out of date, I picked a page that probably has practically no incoming links, my MySQL version is different from Wikimedia's), the UNIONized query may still be a serious problem and needs to be examined more closely before being deployed.
On 10/18/07, Simetrical Simetrical+wikilist@gmail.com wrote:
At first inspection, this appears to cause the query to be executed as a Cartesian join (query run on a fairly old dump of the Simple English Wikipedia on localhost with some random parameters).
mysql> EXPLAIN SELECT * FROM pagelinks, recentchanges, redirect, page WHERE rc_timestamp > '20071011094400' AND (pl_namespace=rc_namespace AND pl_title=rc_title) OR (rd_namespace=rc_namespace AND rd_title=rc_title AND rd_from=page_id AND page_namespace=pl_namespace AND page_title=pl_title) AND pl_from=1234; +----+-------------+---------------+-------+------------------------------------------------+-------------+---------+------+---------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------------+-------+------------------------------------------------+-------------+---------+------+---------+--------------------------+ | 1 | SIMPLE | pagelinks | index | pl_from,pl_namespace | pl_from | 265 | NULL | 1078997 | Using index | | 1 | SIMPLE | recentchanges | ALL | rc_timestamp,rc_namespace_title,rc_ns_usertext | NULL | NULL | NULL | 176 | Using where | | 1 | SIMPLE | page | ALL | PRIMARY,name_title | NULL | NULL | NULL | 43475 | Using where | | 1 | SIMPLE | redirect | index | PRIMARY,rd_ns_title | rd_ns_title | 265 | NULL | 8185 | Using where; Using index | +----+-------------+---------------+-------+------------------------------------------------+-------------+---------+------+---------+--------------------------+ 4 rows in set (0.00 sec)
That was not, of course, the query that was provided (thanks to Xgc for pointing that out when I randomly used this as an example for something in #mysql). First of all, using the toolserver and therefore real test data, and including all parts of the actual query, I get this for the unmodified one (showing minor edits, not bothering with watchlist stuff):
mysql> EXPLAIN SELECT * FROM pagelinks, recentchanges WHERE rc_timestamp > '20071011094400' AND pl_namespace=rc_namespace AND pl_title=rc_title AND pl_from=11105676 GROUP BY rc_cur_id,rc_namespace,rc_title, rc_user,rc_comment,rc_user_text,rc_timestamp,rc_minor,rc_deleted, rc_new, rc_id, rc_this_oldid, rc_last_oldid, rc_bot, rc_patrolled, rc_type, rc_old_len, rc_new_len ORDER BY rc_timestamp DESC LIMIT 50; +----+-------------+---------------+------+------------------------------------------------+--------------------+---------+---------------------------------------------------------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------------+------+------------------------------------------------+--------------------+---------+---------------------------------------------------------+------+----------------------------------------------+ | 1 | SIMPLE | pagelinks | ref | pl_from,pl_namespace | pl_from | 4 | const | 4108 | Using index; Using temporary; Using filesort | | 1 | SIMPLE | recentchanges | ref | rc_timestamp,rc_namespace_title,rc_ns_usertext | rc_namespace_title | 261 | enwiki.pagelinks.pl_namespace,enwiki.pagelinks.pl_title | 1 | Using where | +----+-------------+---------------+------+------------------------------------------------+--------------------+---------+---------------------------------------------------------+------+----------------------------------------------+ 2 rows in set (0.02 sec)
Applying your exact modifications, I appear to get:
mysql> EXPLAIN SELECT * FROM pagelinks, recentchanges, redirect, page WHERE rc_timestamp > '20071011094400' AND (pl_namespace=rc_namespace AND pl_title=rc_title AND pl_from=11105676) OR (rd_namespace=rc_namespace AND rd_title=rc_title AND rd_from=page_id AND page_namespace=pl_namespace AND page_title=pl_title AND pl_from=11105676) GROUP BY rc_cur_id,rc_namespace,rc_title, rc_user,rc_comment,rc_user_text,rc_timestamp,rc_minor,rc_deleted, rc_new, rc_id, rc_this_oldid, rc_last_oldid, rc_bot, rc_patrolled, rc_type, rc_old_len, rc_new_len ORDER BY rc_timestamp DESC LIMIT 50; +----+-------------+---------------+-------+------------------------------------------------+-------------+---------+-------+----------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------------+-------+------------------------------------------------+-------------+---------+-------+----------+----------------------------------------------+ | 1 | SIMPLE | pagelinks | ref | pl_from,pl_namespace | pl_from | 4 | const | 4108 | Using index; Using temporary; Using filesort | | 1 | SIMPLE | recentchanges | ALL | rc_timestamp,rc_namespace_title,rc_ns_usertext | NULL | NULL | NULL | 7446814 | | | 1 | SIMPLE | page | ALL | PRIMARY,name_title | NULL | NULL | NULL | 10593209 | Using where | | 1 | SIMPLE | redirect | index | PRIMARY,rd_ns_title | rd_ns_title | 265 | NULL | 1407500 | Using where; Using index | +----+-------------+---------------+-------+------------------------------------------------+-------------+---------+-------+----------+----------------------------------------------+ 4 rows in set (0.01 sec)
which is of course kind of not really acceptable. For one thing, it has a straightforward logic error: AND binds more tightly than OR, so your x AND (y) OR (z) operates as (x AND (y)) OR (z), with no restriction on rc_timestamp if (z) is met. Fixing that doesn't really help:
mysql> EXPLAIN SELECT * FROM pagelinks, recentchanges, redirect, page WHERE rc_timestamp > '20071011094400' AND ((pl_namespace=rc_namespace AND pl_title=rc_title AND pl_from=11105676) OR (rd_namespace=rc_namespace AND rd_title=rc_title AND rd_from=page_id AND page_namespace=pl_namespace AND page_title=pl_title AND pl_from=11105676)) GROUP BY rc_cur_id,rc_namespace,rc_title, rc_user,rc_comment,rc_user_text,rc_timestamp,rc_minor,rc_deleted, rc_new, rc_id, rc_this_oldid, rc_last_oldid, rc_bot, rc_patrolled, rc_type, rc_old_len, rc_new_len ORDER BY rc_timestamp DESC LIMIT 50; +----+-------------+---------------+-------+------------------------------------------------+--------------+---------+-------+----------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------------+-------+------------------------------------------------+--------------+---------+-------+----------+----------------------------------------------+ | 1 | SIMPLE | recentchanges | range | rc_timestamp,rc_namespace_title,rc_ns_usertext | rc_timestamp | 16 | NULL | 789868 | Using where; Using temporary; Using filesort | | 1 | SIMPLE | pagelinks | ref | pl_from,pl_namespace | pl_from | 4 | const | 4108 | Using index | | 1 | SIMPLE | page | ALL | PRIMARY,name_title | NULL | NULL | NULL | 10593239 | Using where | | 1 | SIMPLE | redirect | index | PRIMARY,rd_ns_title | rd_ns_title | 265 | NULL | 1407504 | Using where; Using index | +----+-------------+---------------+-------+------------------------------------------------+--------------+---------+-------+----------+----------------------------------------------+ 4 rows in set (0.00 sec)
The bigger problem is your join condition. If a row matches pl_namespace=rc_namespace and pl_title=rc_title, then it's joined to *every row* of the page and redirect tables, because there are no restrictions on them! The converse is true as well. Fixing that is somewhat complicated and my first attempt failed. A UNION (with individually limited halves) is probably the best solution.
I had a feeling the SQL was too much of a mess. I didn't remember UNION existed - it is probably a much better way to do it, and I'll make the appropriate changes. However, if you've got the time, perhaps you could help me understand where I went wrong, just for the sake of my education.
which is of course kind of not really acceptable. For one thing, it has a straightforward logic error: AND binds more tightly than OR, so your x AND (y) OR (z) operates as (x AND (y)) OR (z), with no restriction on rc_timestamp if (z) is met. Fixing that doesn't really help:
Yeah, that's obviously wrong - thanks for spotting it.
The bigger problem is your join condition. If a row matches pl_namespace=rc_namespace and pl_title=rc_title, then it's joined to *every row* of the page and redirect tables, because there are no restrictions on them! The converse is true as well.
This is the part I don't understand. What do you mean by there being no restrictions? Why does the pl_from=$id part not restrict it appropriately?
I put it through various tests before committing it, and it seemed to give the correct results (obviously, none of my tests revealed the error with the cutoff - that's a problem with testing on a test install, not a real world database). So is the query correct, just inefficient, or were my tests insufficient to catch the mistakes?
Thanks for your time!
The bigger problem is your join condition. If a row matches pl_namespace=rc_namespace and pl_title=rc_title, then it's joined to *every row* of the page and redirect tables, because there are no restrictions on them! The converse is true as well.
This is the part I don't understand. What do you mean by there being no restrictions? Why does the pl_from=$id part not restrict it appropriately?
Oh hang on, I've think I've spotted it. Did you mean "rd_namespace=rc_namespace" and "rd_title=rc_title"? So the relevant restriction is rd_from=page_id. I take it that restriction isn't applied until after the entire redirect and recentchanges tables have been joined - I guess I need to learn a little more than just basic SQL to understand why not.
A UNION isn't going to fix that, though, is it? The problem isn't with the OR, it's with what's inside the OR, and that would be copied word for word into the second SELECT.
On 10/19/07, Thomas Dalton thomas.dalton@gmail.com wrote:
The bigger problem is your join condition. If a row matches pl_namespace=rc_namespace and pl_title=rc_title, then it's joined to *every row* of the page and redirect tables, because there are no restrictions on them! The converse is true as well.
This is the part I don't understand. What do you mean by there being no restrictions? Why does the pl_from=$id part not restrict it appropriately?
You have to keep in mind that when you're doing simple joins, the result set is equal to the Cartesian product (every possible combination of rows from each table), filtered according to the WHERE/ON conditions (which are equivalent). The pl_from=$id restricts it so that all the rows in the result set will have one particular pagelinks row, no other. The remaining conditions must put enough restrictions on the recentchanges, page, *and* redirect tables to keep the number of returned rows small enough to be reasonable.
The problem is that your conditions state that either the recentchanges table must obey certain conditions, relating to the one pagelinks row already selected, *or* the page and redirect and recentchanges tables must obey certain (different) conditions. If a recentchanges row obeys the first set of conditions (i.e., it corresponds to the pagelinks row), there are no restrictions on what page or redirect rows can be associated with it, and therefore *every* page row and *every* redirect row is associated with it, and so is *every* combination thereof.
This will not actually appear in the result set, because the GROUP BY will condense rows with identical recentchanges rows. I'm not sure exactly how GROUP BY works here as opposed to DISTINCT, say, given that there are no grouping operators or anything: I hardly qualify as an SQL expert. But I could tell from the EXPLAIN that the query was seriously inefficient, and I noticed the deficiency in the join condition that was prompting a Cartesian join of the last two tables (after Xgc in #mysql prompted me to take a closer look at the query).
I put it through various tests before committing it, and it seemed to give the correct results (obviously, none of my tests revealed the error with the cutoff - that's a problem with testing on a test install, not a real world database). So is the query correct, just inefficient, or were my tests insufficient to catch the mistakes?
It may be correct. I'm not sure, because on my PC (which is my test server) it sent mysqld to 90%+ CPU usage for somewhere well over a minute while copying to tmp table, so I got bored and killed the thread. Whether it would have returned the correct results half an hour from now is a somewhat academic question. :)
Generally speaking, it's handy to have a relatively realistic local database. At the suggestion of Yurik, I use the Simple English Wikipedia because it's not gigantic and it's not gibberish to me. It's still not really ideal, because for instance the user table is practically nonexistent, recentchanges is unrealistically small, etc., so I can use the toolserver if I still wasn't sure. That still wouldn't be quite ideal, since it has a different version of MySQL installed and so on, but it would be a pretty good approximation.
By the way, did you test your patch while logged in? It seems to cause a fatal error before it even tries to execute the query. Generally speaking, it's a bad idea to mix implicit join syntax (foo, bar) with explicit join syntax (foo JOIN bar), like foo, bar, baz LEFT OUTER JOIN quuz: it doesn't do what you expect.
Due to all these issues, I've reverted this, r26848.
The problem is that your conditions state that either the recentchanges table must obey certain conditions, relating to the one pagelinks row already selected, *or* the page and redirect and recentchanges tables must obey certain (different) conditions. If a recentchanges row obeys the first set of conditions (i.e., it corresponds to the pagelinks row), there are no restrictions on what page or redirect rows can be associated with it, and therefore *every* page row and *every* redirect row is associated with it, and so is *every* combination thereof.
Ah, so it's the redirects and page tables sitting there doing nothing for non-redirects that's the issue. Makes sense now, thanks!
By the way, did you test your patch while logged in? It seems to cause a fatal error before it even tries to execute the query. Generally speaking, it's a bad idea to mix implicit join syntax (foo, bar) with explicit join syntax (foo JOIN bar), like foo, bar, baz LEFT OUTER JOIN quuz: it doesn't do what you expect.
Now you mention it, no, I don't think I did. However, I'm not sure what I could have done to cause that... the query before I added the redirects bit already had a mixture of implicit joins and explicit joins (implicitly joining recentchanges and pagelinks, explicitly joining watchlist), so I don't think you can blame me for that one... I would have to do some more testing to work out what the fatal error is about, but I have to get up early tomorrow to go to a Wikipedia meetup, so it's almost time for bed.
Due to all these issues, I've reverted this, r26848.
Thanks. I'll try again over the weekend with a UNION. I'll try and understand the fatal error first, though.
Thank you very much for taking the time to explain all this to me, it really is appreciated.
wikitech-l@lists.wikimedia.org