判断A表ID 在B表是否存在
A表 B表
ID name ID AID Date
1 aaa 1001 1 2010-04-20
2 bbb 1002 1 2010-04-21
3 ccc 1003 2 2010-04-20
1004 2 2010-04-21
1005 2 2010-04-22现在用的sql语句:
select id from A
where exists(select null from B
where A.ID = B.AID
and B.Date > '2010-04-20' and B.Date < '2010-06-20')当表B里面的记录超过30万的时候,查询要10几秒,
请教大家,这个语句应该怎么优化,或者有什么别的办法实现这样的功能??
A表 B表
ID name ID AID Date
1 aaa 1001 1 2010-04-20
2 bbb 1002 1 2010-04-21
3 ccc 1003 2 2010-04-20
1004 2 2010-04-21
1005 2 2010-04-22现在用的sql语句:
select id from A
where exists(select null from B
where A.ID = B.AID
and B.Date > '2010-04-20' and B.Date < '2010-06-20')当表B里面的记录超过30万的时候,查询要10几秒,
请教大家,这个语句应该怎么优化,或者有什么别的办法实现这样的功能??
from a inner join b on a.id=b.aid
where B.Date > '2010-04-20' and B.Date < '2010-06-20'
用2表联合查询效率会比较高,
select distinct a.id
from a b
where a.id=b.aid b.Date > '2010-04-20' and b.Date < '2010-06-20'
也行
from a,b
where a.id=b.aid b.Date > '2010-04-20' and b.Date < '2010-06-20'差个逗号
有没有分高人详细分析过exists和inner join的执行过程和DBMS对其优化后的执行过程的?希望能有详细解释 ^_^…………
如果你反悔的结果是多个表的字段 必须使用join
这是MSSQL的结论 不知道mysql如何
我实际测试了一下,用exists查询平均用时4秒,用inner join要8秒