我的查询如下:
EXPLAIN SELECT u0_.service_id AS sclr0, u1_.property_uri AS property_uri1, u2_.value AS value2, count(u2_.id) AS sclr3
FROM usc_connection_triple u2_
INNER JOIN usc_property u1_ ON u2_.property_id = u1_.id AND (u1_.status = 1)
INNER JOIN usc_account_connection u3_ ON u2_.account_connection_id = u3_.id AND (u3_.status = 1)
INNER JOIN usc_service_subscriber u0_ ON ((u3_.account_1_id = u0_.id OR u3_.account_2_id = u0_.id)) AND (u0_.status = 1)
WHERE (u1_.create_analytics = '1') AND (u2_.status = 1)
GROUP BY u0_.service_id, u2_.property_id, u2_.value;
explain命令的当前状态如下(存储引擎:innodb):
现在,我想在u0_ table上创建一个索引。从列中,我正在尝试(service_id,status)(尝试使用两个订单),这似乎不起作用。我的假设是,primary_key将始终是二级索引的一部分,因此我不需要专门添加它。(仍然尝试过但没有运气)。
我的问题是,在当前情况下,根本无法获得在此表上工作的索引吗?我可以,怎么样?
或者重组查询会有所帮助吗?
答案 0 :(得分:0)
我总是尝试尽可能在JOIN中放置WHERE条件,我也喜欢将JOIN条件保持在我正在等于左边的表格中,我更喜欢IN到OR:
SELECT u0_.service_id sclr0,
u1_.property_uri property_uri1,
u2_.value AS value2,
count(u2_.id) sclr3
FROM usc_connection_triple u2_
JOIN usc_property u1_
ON u1_.id = u2_.property_id
AND u1_.status = 1
AND u1_.create_analytics = 1
JOIN usc_account_connection u3_
ON u3_.id = u2_.account_connection_id
AND u3_.status = 1
JOIN usc_service_subscriber u0_
ON u0_.id IN (u3_.account_1_id,u3_.account_2_id)
AND u0_.status = 1
WHERE u2_.status = 1
GROUP BY u0_.service_id, u2_.property_id, u2_.value;
我还假设u1_.create_analytics
是一个整数并删除了单引号。
我还要仔细检查你的GROUP BY,它与你的SELECT不匹配..也许你想要:
GROUP BY sclr0, property_uri1, value2; /* You can use aliases in GROUP BY */
至于索引我会去:
我对索引和JOIN并不是很好,但是使用EXPLAIN中的possible_keys来确定这些是否适用于查询,或者需要进行调整。
希望有所帮助:)