Pros / Cons of MySql JOINS

前端 未结 4 2158
余生分开走
余生分开走 2021-02-08 03:56

When I\'m selecting data from multiple tables I used to use JOINS a lot and recently I started to use another way but I\'m unsure of the impact in the long run.

Examples

4条回答
  •  庸人自扰
    2021-02-08 04:13

    ANSI Syntax

    Both queries are JOINs, and both use ANSI syntax but one is older than the other.

    Joins using with the JOIN keyword means that ANSI-92 syntax is being used. ANSI-89 syntax is when you have tables comma separated in the FROM clause, and the criteria that joins them is found in the WHERE clause. When comparing INNER JOINs, there is no performance difference - this:

    SELECT * 
      FROM table_1 t1, table_2 t2
     WHERE t1.column = t2.column
    

    ...will produce the same query plan as:

    SELECT *
      FROM TABLE_1 t1
      JOIN TABLE_2 t2 ON t2.column = t1.column
    

    Apples to Oranges

    Another difference is that the two queries are not identical - a LEFT [OUTER] JOIN will produce all rows from TABLE_1, and references to TABLE_2 in the output will be NULL if there's no match based on the JOIN criteria (specified in the ON clause). The second example is an INNER JOIN, which will only produce rows that have matching records in TABLE_2. Here's a link to a visual representation of JOINs to reinforce the difference...

    Pros/Cons

    The main reason to use ANSI-92 syntax is because ANSI-89 doesn't have any OUTER JOIN (LEFT, RIGHT, FULL) support. ANSI-92 syntax was specifically introduced to address this shortcoming, because vendors were implementing their own, custom syntax. Oracle used (+); SQL Server used an asterisk on the side of the equals in the join criteria (IE: t1.column =* t2.column).

    The next reason to use ANSI-92 syntax is that it's more explicit, more readable, while separating what is being used for joining tables vs actual filteration.

提交回复
热议问题