2016-12-15 104 views
1

我正在考虑在(1)中创建散列索引,因为它使用(2)上的等式和位图,因为状态只能被“接受”或“不被接受”。我还能使用什么?也是我的问题是,我只能尽量在MySQL的Oracle B树索引..我应该创建哪些类型的索引来优化这些mysql查询?

(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’) 
+0

为什么你的代码中有['零宽度空间'(U + 200B)](http://www.fileformat.info/info/unicode/char/200b/index.htm)字符? ---为什么你的文字使用时髦的撇号​​(''和''',而不是''')? – Andreas

回答

0

由于没有直接限制查询#1指标分析,它可能会被使用合并完成加入,并没有指数会改善这一点。

对于查询#2,标准E.state = 'accepted'的选择性如何?如果选择性很强(<查询结果的5-15%),那么索引E.state,从E到R和从R到P的连接索引,以及索引P.adress, P.code

0

每桌综合指数:

INDEX(space_id, adress) 

不要使用WHERE(a,b) IN ... - 它执行很差。

不要使用IN (SELECT ...) - 它往往表现不佳。

改为使用JOIN

对于state,有

INDEX(state) 

(或者是已经PRIMARY KEY?)

如果您需要在所有这一切更多的帮助,提供SHOW CREATE TABLEEXPLAIN SELECT ...