The main purpose of this article is to demonstrate how
LookMySQL joins. by "look at" I mean how to see through MySQL's eyes, so-to-speak. this task is fundamentally different from the rather straight-forward tasks of how to do joins or how to index for joins with MySQL. even knowing how to join and how to index for joins does not completely demystify the process because there are missing aspects about the query which MySQL takes into consideration when deciding on a join plan. knowing how to look at MySQL joins (and how to join and how to index for joins) considerably demystifies the process. special in cases like this, when we're re tasked with optimizing a query without access to the actual database, we must be able to look at the join as MySQL wocould to know "if we re-write the query like this, then MySQL shocould execute a join plan like this. "Therefore, this article cocould be titled," Understanding MySQL Joins
A priori."
With that in mind, a visitor sent me an email asking how the following query cocould be optimized. the query is part of NewBB Plus encoded in RunCMS (formerly E-Xoops ). its purpose is to retrieve the top N newest posts in the forums.
SELECT
...
FROM
mpn_bb_posts p,
mpn_bb_forums f
INNER JOIN mpn_bb_topics t ON t.topic_last_post_id = p.post_id
WHERE
f.forum_id = t.forum_id
AND (f.forum_type = 0)
AND (f.forum_id<>15)
AND (f.forum_id<>25)
AND (f.forum_id<>29)
AND (f.forum_id<>33)
GROUP BY
p.topic_id
ORDER BY
t.topic_time DESC;
At first you may notice two parts of the query which are technically wrong, although the query does work: first, the query uses group by without any aggregate functions, and second, the order of tables listed in the FROM clause shoshould be f, p, inner join t. first, we'll briefly discuss these two problems. that will clear the way for us to look at this join as MySQL wowould. finally, we'll fix-optimize-this query.
Two Problems
What is that group by doing? Not much. it's attempted purpose is to limit the results by having no more than one result per forum topic. in case it's not clear, the hierarchy of the forum is form, topic, post. for our visitor who originally asked me about this query, they have roughly 35 forums, 49,000 topics, and 549,000 posts. therefore, the group by limits the result set ~ 49,000 records. obviusly this is a very poor limiter, especially when only the top 40 newest posts are displayed on the website. you may ask, "But if we remove the group by, will the query return more than one post per topic? "No, because the inner join clause joins only the newest post for each topic, and obviously there can't be more than one newest post. removing the group by doesn't change MySQL's join plan for the query, but it needs to be removed anyway to allow us to work order by with LIMIT magic later.
The second problem is the order in which the tables are listed in the FROM clause. I have yet to test older versions of MySQL, but in v5.0.15 the query causes the following error:
ERROR 1054 (42S22): Unknown column 'p. post_id 'in 'on clause'
The answer is stated in the final comment of the MySQL bug report 13832 (which is actually not a bug); I'll rephrase the answer. the comma has lower precedence than JOIN. the FROM clause is interpreted:
FROM
mpn_bb_posts p,
(mpn_bb_forums f INNER JOIN mpn_bb_topics t ON t.topic_last_post_id = p.post_id)
In other words, the inner join joins tables f and t. therefore, the ON clause for this join must reference columns in either tables f or t, but it doesn't, it references a column in table p (p. post_id ). hence, MySQL dies. the finer details of this are discussed in the MySQL manual section 13.2.7.1. JOIN Syntax. the fix is easy: swap p and f in the FROM clause:
FROM
mpn_bb_forums f,
mpn_bb_posts p
INNER JOIN mpn_bb_topics t ON t.topic_last_post_id = p.post_id
A RunCMS developer suggests we can just get rid of the inner join statement and re-write the relevant portions of the query:
FROM
runcms_bbplus_posts p,
runcms_bbplus_forums f,
runcms_bbplus_topics t
WHERE
t.topic_last_post_id = p.post_id
AND f.forum_id = t.forum_id
Either way works; we'll use the inner join style. Now that the query is technically correct we can begin an a priori look at how MySQL will execute this query and then optimize it.
Through the Eyes of MySQL
Three important ideas help guide our understanding of how to look at joins with MySQL:
- Every table has its criteria.
- We have to pick a table to start.
- Use the least costly join plan.
I'll discuss each idea in turn, then we'll apply it to this query. by "criteria" I mean the columns of each table referenced in the query. these are often the columns used to relate or join the tables (commonly "ID" columns), but columns referenced anywhere in the query (WHERE, group by, order by, etc .) are supported ded. for each table we can make a list of its criteria:
Table f |
Table p |
Table t |
WHERE AND f.forum_id = t.forum_id AND (f.forum_id<>15) AND (f.forum_id<>25) AND (f.forum_id<>29) AND (f.forum_id<>33) |
WHERE t.topic_last_post_id = p.post_id |
WHERE t.topic_last_post_id = p.post_id AND f.forum_id = t.forum_id ORDER BY t.topic_time DESC |
Naturally there are overlaps because tables references each other (which is the whole idea of a join); these overlaps are how the tables relate to each other. knowing each tables 'Criteria we can start to consider different ways to join the tables.
We have to pick a table to start with, but first a few words on two ways tables are conceptually joined. Usually, tables are joined throughSeries of relations. That is, the second table in the join plan has a relation to the first, the third table has a relation to the second, the forth to the third, etc. therefore, column values from one table will be used to join the next table. the logical implication that follows is that each table shocould have a criteria that references the table prior to it in the join plan (should t for the first table of course ).
The alternative to joining tables through a series of relations is to join tables onCommon relation. For example, the second and third tables may not share a relation to one another but both share a relation to the first table.
Stated exactly ally, these two ways of joining tables looks like:
- Series of Relations: table 1-> 2-> 3
- Common Relation: table 1-> 2, (1)-> 3
Either way, MySQL reads table 1, then 2, then 3-its "single-sweep multi-join method. "The" (1) "in the common relation join means that when table 3 is read, rows in it are found using its relation to table 1, not table 2 as in the series of relations join. joining tables through a series of relations has the advantage of privilege cing duplicate table reads if the first table is the most restrictive, the second table less restrictive, the third table even less restrictive, etc. why this is a positive consequence of MySQL's single-sweep multi-join method but not of necessary importance for this article.
Going back to picking a table to start with, there is only a limited number of possible join plans that follow from using each table first:
- Table f First: f, t, p
- Table p First: p, t, f
- Table t First: t, f, p or t, p, f
Actually there's more than three join plans: f, p, t cocould be a join plan, but this is neither a series of relations or a common relation because there is no relation between f and p. therefore f, p, t-or p, f, t-is a very poor join plan that MySQL will certainly avoid. for these three join plans though, the first two have a series of relations and the third has a common relation (table t ). which t Able do we pick to start?
The third idea guides us: use the least costly join plan. "Cost" is primarily a matter of three factors:
- Indexes
- Table reads
- Extra work like temp tables and filesorts
Those factors are hierarchical: without proper indexes, all hope is lost; then we have to reduce table reads as much as possible because hard drives are really slow and while we're re reading tables the tables are locked; finally, we can try to optimize out extra work if doing such also satisfies the previous two factors. indexing for joins is discussed in the article How To Index For Joins With MySQL. in this article we're re focusing on table reads and extra work (the tables are properly indexed ). we have three join plans and obviously we want the one which costs the least amount of table reads. as stated earlier: joining tables through a series of relations has the advantage of privilege cing duplicate table reads. therefore, we want to pick table f or p to start. since table f is extremely more restrictive than table p (34 forums verses 549,000 posts), this is the least costly join plan (f, t, p ). until now we haven'tconsulted EXPLAIN because the overall idea of this article is understanding MySQL joinsA priori, But the time has come now to EXPLAIN and see that I haven't been lying to you. The following EXPLAIN is the original, unmodified from my visitor:
+-------+--------+---------------+---------+---------+----------------------+------+--------------------------------------------+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+-------+--------+---------------+---------+---------+----------------------+------+--------------------------------------------+
| f | ALL | PRIMARY | NULL | NULL | NULL | 34 | Using where; Using temporary; Using filesort
| t | ref | idx | idx | 3 | f.forum_id | 1452 | |
| p | eq_ref | PRIMARY | PRIMARY | 3 | t.topic_last_post_id | 1 | |
+-------+--------+---------------+---------+---------+----------------------+------+--------------------------------------------+
MySQL has chosen join plan f, t, p just as we predicted. therefore, we have proper indexes, and we're re using the join plan with the least amount of table reads, the trick now is to get rid of "Using temporary; Using filesort ".
More order by With LIMIT
The "Using temporary; Using filesort" is a consequence of the order by clause in the query. from the article order by With LIMIT and ANALYZE we already know why order by with LIMIT is a MySQL query optimization technique. fortunately, we can apply the same technique to this query because we also need to limit the results (which was what group by was trying to do ). however, the query in the previous article used a single table and didn't incur "Using temporary; Using filesort" so while the optimization technique works for this query too, it does so under a different premise which we'll examine.
In the previous article and query, the premise was that MySQL was using an index to avoid "Using temporary; Using filesort" even when EXPLAIN said there were no possible keys. for this query the premise is dramatically different because with multiple tables we have to properly index for the join first, then get the least costly join plan, and then finally try to remove the extra work of "Using temporary; Using filesort" by using an index that also accomplishes the prior two factors. therefore, the order by with LIMIT technique is not so easy as it was in the previous case.
The first thing to do is index the column in the order. in this case, t. topic_time is indexed. then we have to examine whether a join plan starting with the table of this column is possible. from the list of possible join plans we discussed earlier, we know that t, f, p (same as t, p, f) is a possible common relation join. normally this join plan wowould read more rows than plan f, t, p, but since it can be used for the order by with LIMIT optimization technique it really only reads N rows, where N is the LIMIT. basically, we're just lucky this works with the join. there are a number of situations that wowould prevent us from using this optimization:
- Table t wasn't a plausible first table in the join plan.
- The order by referenced a second column from another table.
- The order by had two columns with one in DESC order and the other in ASC.
Even before consulting EXPLAIN again we can be very sure MySQL will switch the join plan from f, t, p to t, f, p (or t, p, f ). furthermore, we can even predict what indexes it will use for each table. we know that table t will be first in the join plan and MySQL will use the index on column topic_time (called t_time ). then, consulting the table of relations we made earlier, we know that both tables f and p are related to t by their primary key column (forum_id and post_id respectively ). therefore, the new join plan will uses indexes t_time, PRIMARY, PRIMARY. this is what I told our visitor and then asked him for the new EXPLAIN:
+-------+--------+---------------+---------+---------+----------------------+-------+-------------+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+-------+--------+---------------+---------+---------+----------------------+-------+-------------+
| t | index | idx | t_time | 4 | NULL | 49547 | |
| f | eq_ref | PRIMARY | PRIMARY | 3 | t.forum_id | 1 | Using where |
| p | eq_ref | PRIMARY | PRIMARY | 3 | t.topic_last_post_id | 1 | |
+-------+--------+---------------+---------+---------+----------------------+-------+-------------+
We were correct again, a priori. at this point, the query can't really be optimized anymore. the 49,547 rows for table t is misleading. in reality MySQL will stop reading records after it's found N records that satisfy the join, and since the other tables are being joined on unique columns, mySQL shoshould only need to read N records from t and join those with f and p. in a highly unlikely situation, MySQL may find a record in t that has no matching record in f or p, but hopefully the database has no such orphaned forums or posts (I. e ., forms or posts which are not related to any topic ).
Conclusion
In this article we 've discussed how to look at MySQL joins. different from a typical hands-on approach to working with queries and joins, we 've taken an priori approach to understanding how and why MySQL develops and chooses among various multi-table join plans. this process is framed by three ideas, the first of which is that every table has its criteria. to facilitate this idea, I find it helpful to make a side-by-side list of each table's criteria. these criteria also include the relation between tables; that is, where one table references another. knowing these relations is important later. the second idea is that we have to pick a table to start the join plan. entailed within this idea are two conceptual ways to join tables: a series of relations and a common relation. A series of relations join incurs less table reads if the tables are progressively more restrictive from the start of the join plan. since we know the relations between all the tables from the first idea, we can construct a list of all plausible join plans for each table as the starting table. A join plan is plausible if it is either a series of relations or a common relation join. otherwise the join plan isPossibleBut implausible and undesirable because it tends to incur full cross-product joins between the unrelated tables-very slow. the third idea tells us how to pick a table to start with: use the least costly join plan. "Cost" is a hierarchy of factors: indexes, table reads, and finally extra work. proper indexes for the join are most important; without them the join is doomed. then we prefer a join plan with the least amount of table reads. as stated earlier, this is usually the series of relations join plan with the most restrictive first table. finally, if we're luky, we can also index where necessary to avoid extra work like filesorts or temp tables, but only if this index still satisfies the first two factors.
Looks like a lot of information to know, but as always, if the problem is approached methodically as we 've done in this article, you can play the part of the MySQL query execution planner in your mind with a good degree of accuracy. you can take the hands-on approach and tweak the query, EXPLAIN, and repeat, but to optimize the query and join in your mind a priori is a very valid tive way to geek out.