一、操作符<>优化
通常<>操作符无法使用索引,举例如下,查询金额不为100元的订单:
select id from orders where amount != 100;
如果金额为100的订单极少,这种数据分布严重不均的情况下,有可能使用索引。鉴于这种不确定性,采用union聚合搜索结果,改写方法如下
(select id from orders where amount > 100)
union all
(select id from orders where amount < 100 and amount > 0)
二、OR优化