# 哪个更好：加入条件多还是条件多？

13

``````SELECT a,b,c,d,e
FROM tableA
LEFT JOIN tableB
ON tableA.a=tableB.a
WHERE tableA.b=tableB.b AND tableA.c=tableB.c  AND tableA.d=tableB.d  AND tableA.e=tableB.e ``````

``````SELECT a,b,c,d,e
FROM tableA
LEFT JOIN tableB
ON tableA.a=tableB.a AND tableA.b=tableB.b AND tableA.c=tableB.c  AND tableA.d=tableB.d
WHERE tableA.e=tableB.e ``````

3

Lamak

4

@ypercube对，我错过了这种情况。
Lamak

10

``````EXPLAIN
SELECT
tableA.ColA
,tableA.ColB
,tableA.ColC
,tableA.ColD
,tableA.ColE
FROM tableA
JOIN tableB ON tableA.ColA=tableB.ColA
WHERE
tableA.ColB=tableB.ColB
AND tableA.ColC=tableB.ColC
AND tableA.ColD=tableB.ColD
AND tableA.ColE=tableB.ColE``````

[结果]

``````| ID | SELECT_TYPE |  TABLE | TYPE | POSSIBLE_KEYS |    KEY | KEY_LEN |    REF | ROWS |                          EXTRA |
------------------------------------------------------------------------------------------------------------------------
|  1 |      SIMPLE | tableA |  ALL |        (null) | (null) |  (null) | (null) |    1 |                                |
|  1 |      SIMPLE | tableB |  ALL |        (null) | (null) |  (null) | (null) |    1 | Using where; Using join buffer |``````

``````EXPLAIN
SELECT
tableA.ColA
,tableA.ColB
,tableA.ColC
,tableA.ColD
,tableA.ColE
FROM tableA
JOIN tableB ON tableA.ColA=tableB.ColA
AND tableA.ColB=tableB.ColB
AND tableA.ColC=tableB.ColC
AND tableA.ColD=tableB.ColD
WHERE
tableA.ColE=tableB.ColE``````

[结果]

``````| ID | SELECT_TYPE |  TABLE | TYPE | POSSIBLE_KEYS |    KEY | KEY_LEN |    REF | ROWS |                          EXTRA |
------------------------------------------------------------------------------------------------------------------------
|  1 |      SIMPLE | tableA |  ALL |        (null) | (null) |  (null) | (null) |    1 |                                |
|  1 |      SIMPLE | tableB |  ALL |        (null) | (null) |  (null) | (null) |    1 | Using where; Using join buffer |``````

MySQL查询示例

SQL 2008查询示例（确保两个结果都“查看执行计划”）

Geoff

4

Mike Fal 2013年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.