以下查询需要永远完成。我已在join
中包含的所有字段中添加了索引,尝试将where
条件放入join
,我认为在修补FORCE/USE
之前我会先征求意见索引。似乎应该在此连接的两端使用索引。似乎只使用i1
。
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE a ALL i1,i2,i3 2399303 100.00 Using temporary
1 SIMPLE b ref i1,i2,i3 i1 5 db.a.bt 11996 100.00 Using where
create index i1 on obs(bt);
create index i2 on obs(st);
create index i3 on obs(bt,st);
create index i4 on obs(sid);
explain extended
select distinct b.sid
from obs a inner join obs b on a.bt = b.bt and a.st = b.st
where
a.sid != b.sid and
abs( datediff( b.sid_start_date , a.sid_expire_date ) ) < 60;
我已尝试上面的ALTER TABLE
和CREATE INDEX
将索引添加到obs
。
答案 0 :(得分:0)
由于您没有选择表a中的任何列,因此最好使用exists。存在允许您在不使用连接的情况下检查您要查找的信息是否在指定的表中。删除联接可提高性能。我也喜欢存在,因为我认为当你几个月后回到它时它会使查询更容易理解。
select distinct b.sid
from obs b
where exists (Select 1
From obs a
Where a.bt = b.bt
and a.st = b.st
and a.sid != b.sid
and abs( datediff( b.sid_start_date , a.sid_expire_date ) ) < 60);