我正在考虑在 (1) 中创建散列索引,因为它在 (2) 中使用等式和位图,因为状态只能是“接受”或“不接受”。我还能用什么?而且我的问题是我只能在 mysql oracle 上尝试 b-tree 索引..
(1)select R.user_id from rent as R
inner join supervise S on
R.adress = S.adress
and R.space_id = S.space_id
group by R.user_id
having count(distinct S.supervisor_id) = 1
(2) select distinct P.adress, P.code from space as P where (P.adress, P.code) not in (
select P.adress, P.code from space as P
natural join rent as R
natural join state as E where E.state = ‘accepted’)
最佳答案
由于在查询 #1 中没有直接的限制条件,它可能会使用合并连接来完成,并且没有索引可以改进它。
对于查询 #2,条件 E.state = 'accepted'
的选择性如何?如果非常有选择性(< 5-15% 的查询结果),则在 E.state
上建立索引,为从 E 到 R 和从 R 到 P 的连接建立索引,并在 P 上建立索引。地址, P.code
.
关于mysql - 我应该创建哪些和什么类型的索引来优化这些 mysql 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41171545/