我有下面的查询,我使用DISTINCT,因为有关联,所以它给出了重复的值。但是否有可能实现它使用EXIST以及?
SELECT DISTINCT ON (cl.id)
cl.id,
cl.cid,
cl.name
FROM locations cl
JOIN prop_loc pl ON (cl.cid = pl.cid AND cl.id = pl.loc_id)
JOIN prop_loc_prob plp ON (plp.cid = pl.cid AND plp.prop_id = pl.prop_id AND plp.loc_id = pl.loc_id)
JOIN prop_prob pp ON (plp.cid = pp.cid AND plp.prop_id = pp.prop_id AND plp.prob_id = pp.prob_id)
WHERE
pl.prop_id = 12345 AND
pl.cid = 123
ORDER BY cl.id, cl.name
字符串
我们可以在EXISTS中使用以下两个严格JOINS吗?它是否提高了SQL性能?
JOIN prop_loc_prob plp ON (plp.cid = pl.cid AND plp.prop_id = pl.prop_id AND plp.loc_id = pl.loc_id)
JOIN prop_prob pp ON (plp.cid = pp.cid AND plp.prop_id = pp.prop_id AND plp.prob_id = pp.prob_id)
型
1条答案
按热度按时间eivgtgni1#
所有输出列都来自表
locations
。将其余所有列移动到EXISTS
子查询表达式中。然后您可以完全删除聚合(没有GROUP BY
):比较所有候选查询与
EXPLAIN ANALYZE
的性能。字符串