假设我的查询是:
SELECT adstable.adid FROM `adstable`
inner join userstable on
(adstable.adid = userstable.adid)
WHERE adstable.desktopimp > 100
and adstable.mobileimp > 100
and adstable.userbal > 0.02
and adstable.realbal> 0.02
order by adstable.imptotal asc
我应该索引哪些列,以确保此查询的“覆盖索引”?
答案 0 :(得分:0)
可以有多个用户"每个"广告"?可以有零吗?如果不是,那么
SELECT a.adid
FROM `adstable` AS a
WHERE a.desktopimp > 100
and a.mobileimp > 100
and a.userbal > 0.02
and a.realbal> 0.02
AND EXISTS (
SELECT *
FROM userstable
WHERE adid = a.adid
)
order by a.imptotal asc
这是否是更好的表述,有这些索引:
userstable: INDEX(adid)
adstable: INDEX(imptotal) -- in case it is better to avoid the sort
INDEX(desktopimp) -- in case it is most selective
INDEX(mobileimp)
INDEX(userbal)
INDEX(realbal)
A"覆盖" index将包括查询中使用的所有6列adstable
。这相当笨重,而且使用起来可疑。
请提供SHOW CREATE TABLE
- 查看数据类型,引擎等可能会有所帮助。
在构建最佳索引时,请从与=
相比的任何列开始。如果需要处理所有WHERE
,请转到ORDER BY
列。您没有这种情况,因为您没有使用=
。