1.尽量避免在WHERE语句中使用OR来连接条件,否则将导致引擎放弃使用索引而进行全表扫描,可以使用UNION合并查询。
实际测试
(1)使用union all合并查询
select id,name
from table
where id=114
union all
select id,name
from table
where id=334
在presto中查询的时间
18.19sec 279.35kbyte 500 / 11,016 results 2columns
(2)使用or连接进行查询
select id,name
from table
where id=334or type_id=114
在presto中查询的时间
56.98sec 279.35kbyte 500 / 11,016 results 2columns
对比可以看出or连接查询的时间是union all并查询的3倍