我有這個sql查詢,這需要很長時間才能完成。我如何加快速度?SQL IN子句更有效的方法
t_inter_specises_interaction有60k行,t_pathway有100k。 uniprot_id_1,uniprot_id_2,uniprot_id是varchar類型。
在該查詢中,我想選擇uniprot_id_1和uniprot_id_2其在t_pathway兩者都存在:
select distinct uniprot_id_1,uniprot_id_2 from t_intra_species_interaction
where uniprot_id_1 in (select uniprot_id from t_pathway) and
uniprot_id_2 in (select uniprot_id from t_pathway)
在這其中,我想選擇uniprot_id其存在於由所述第一查詢返回的集合uniprot_ids以上。
select distinct uniprot_id,id from t_pathway as t
where uniprot_id in
(
select distinct uniprot_id_2 from t_intra_species_interaction
where uniprot_id_1 in (select uniprot_id from t_pathway) and
uniprot_id_2 in (select uniprot_id from t_pathway)
union
select distinct uniprot_id_1 from t_intra_species_interaction
where uniprot_id_1 in (select uniprot_id from t_pathway) and
uniprot_id_2 in (select uniprot_id from t_pathway)
)
謝謝。
考慮提供適當的DDL(和/或sqlfiddle)連同所需的結果集 – Strawberry
使用exists代替 –
這是緩慢的,因爲在那些'IN()'子句中的子查詢是正在爲't_intra_species_interaction'中的每個匹配行運行。 – DanMan