简体   繁体   English

SQl查询中哪个子查询或JOIN更好?

[英]Which is better Sub-Query or JOIN in SQl query?

Can any one suggest which one is better in this case: 在这种情况下,谁能建议哪个更好:

1: 1:

SELECT
    COLUMN1, 
        (SELECT 
            COLUMN2 
        FROM
            TABLE2
        WHERE 
            <some condition>) AS COLUMN2
FROM TABLE 1 

2: 2:

SELECT
    T1.COLUMN1,
    T2.COLUMN2
FROM
    TABLE1 T1
INNER JOIN 
    TABLE2 T2 ON <some condition>

There are several axis of "better"... 有几个“更好”的轴...

  • Better performance 更好的性能
  • Better readability 更好的可读性
  • Better maintainability 更好的可维护性
  • Better flexibility .... 更好的灵活性..

    So, 所以,

    1. Test and find out (examine plans they may be identical) 测试并找出(检查计划可能相同)
    2. In the eye of the beholder, where the queries are equvalent I prefer joins 在情人眼中,查询是等价的,我更喜欢加入
    3. Depends on your staff 取决于你的员工
    4. Doesn't seem applicable in this case ... 在这种情况下似乎不适用...

Better is frequently just a trade off... 更好的往往是权衡...

Use JOIN. 使用JOIN。

Subqueries vs joins 子查询与联接

Which is better? 哪个更好?

It is not the problem which is better, because they are complete different query. 不是更好的问题,因为它们完成不同的查询。

Your first query will be error if your sub query return more than 1 row. 如果您的子查询返回的行数超过1,则您的第一个查询将是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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