使用一个表的多个选择查询(提高性能)

时间:2015-08-19 09:06:33

标签: mysql performance

有没有办法改进此查询?

 SELECT variety_id FROM anvariety_trait WHERE variety_id IN
 (SELECT variety_id FROM anvariety_trait WHERE trait_id = 10 and trait_value =     2 and variety_id IN 
 (SELECT variety_id FROM anvariety_trait WHERE trait_id = 9 and trait_value = 1 and variety_id IN 
 (SELECT variety_id FROM anvariety_trait WHERE trait_id = 7 and trait_value = 1 )))

我试图从同一个表中获得三级选择查询的结果。提前谢谢。

1 个答案:

答案 0 :(得分:0)

你可以尝试加入条款,但是我不确定这会改善你的表现(无论如何值得测试)

select distinct v1.variety_id 
FROM anvariety_trait v1
join anvariety_trait v2 on v1.variety_id = v2.variety_id
join anvariety_trait v3 on v2.variety_id = v3.variety_id
join anvariety_trait v4 on v3.variety_id = v4.variety_id
where v2.trait_id = 10 and v2.trait_value = 2
and   v3.trait_id = 9  and v3.trait_value = 1
and   v4.trait_id = 7  and v4.trait_value = 1