简体   繁体   中英

MySQL Query optimization thru order synchronization of columns and WHERE clause conditions

Suppose I have a join of four tables, Table 1 which is referencing the other 3 tables (Table2,3,4) via foreign key relations:

SELECT a.column_a, b.column_b, c.column_c, d.column_d
FROM Table1 a, Table2 b, Table3 c, Table4 d
WHERE a.col1_fk=d.col_id
AND a.col2_fk=c.col_id
AND a.col3_fk=b.col_id

In order to have the best optimization of the above statement, should the join conditions be in the same order in which the columns of the SELECT statement are ? Hence, is my example above least optimized ? Or is the said order of no importance ?

It's of no importance. The query optimizer decides, which table is read first.
This might also change when your data size is growing. For example, one day your WHERE clause filters 90% of a table, so MySQL decides to read this table first. A thousand INSERT s later, the same WHERE clause might filter just 1%. Then MySQL may decide to read it last, or even do a full table scan.

Don't worry about this kind of optimization. If you want to optimize, read about EXPLAIN (which also shows you in which order the tables are read, aside from if and which indexes are used) and learn to use indexes properly.

And, no offense, what you should really learn is the proper join syntax that was introduced with the ANSI SQL Standard from 1992. Here are some examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM