* 测试,当on的条件为false的时候,join是否会被优化?
-- 插入测试表
CREATE TABLE person (id INT NOT NULL ,NAME VARCHAR(20));
CREATE TABLE bill (id INT NOT NULL ,NAME VARCHAR(20),personId INT );
-- 插入测试数据
DELETE FROM person;
INSERT INTO person VALUES(1,"martin fowler");
INSERT INTO person VALUES(2,"rebert martin");
-- 插入测试数据
DELETE FROM bill;
INSERT INTO bill VALUES(1,"bill1",1);
INSERT INTO bill VALUES(2,"bill2",1);
INSERT INTO bill VALUES(2,"bill2",3);
SELECT * FROM bill;
// mysql 是不能优化掉的,执行计划内有person
EXPLAIN SELECT bill.* FROM bill LEFT JOIN person ON FALSE AND bill.personId = person.id
同样的测试,sqlserver可以优化掉。
即使select内有person的字段也可以。相见 执行计划
SELECT bill.*,person.* FROM bill LEFT JOIN person ON 1=0 aND bill.personId = person.id