我不知道如何为以下查询命中索引:
select * from Cache
where accountId = 'demo'
and (? is null or country = ?)
and (? is null or county = ?)
and (? is null or province = ?)
and (? is null or city = ?)
and (? is null or locality = ?)
order by createdAt desc
我在cols上有一个orderedGroup索引(accountId,国家/地区,国家/地区,省,城市,地区,createdAt(desc))。如果我删除了isull,它正在工作。也许有另一种方法可以在sql中重写它,以便能够解决空参数?
答案 0 :(得分:1)
您可以使用OR expansion
重写它:
select * from Cache
where accountId = 'demo' and ? is null
union all
select * from Cache
where accountId = 'demo' and city = ?