如果我有类似的查询
SELECT date_trunc('day', assigndate)e,
count(CASE WHEN a.assigneeid = 65548
AND a.assigneeid IN
(SELECT userid
FROM groupmembers
WHERE groupid = 65553) THEN 1 ELSE NULL END) assigned,
count(CASE WHEN a.assigneeid = 65548
AND a.completedtime IS NOT NULL
AND a.assigneeid IN
(SELECT userid
FROM groupmembers
WHERE groupid = 65553) THEN 1 ELSE NULL END) completed
FROM ASSIGNMENT a
WHERE assigndate > CURRENT_TIMESTAMP - interval '20 days'
GROUP BY date_trunc('day',assigndate);
有问题的子查询是
SELECT userid
FROM groupmembers
WHERE groupid = 65553
那么由于子查询是not co-related
针对父查询的,因此它将只执行一次并使用缓存的结果。但是由于子查询存在于查询中的 2 个位置,因此根据SQL plan
,它被评估两次。有没有办法获得cache
该子查询的结果并在两个位置都使用它?
子查询不能转换为连接,因为没有要连接的单个字段(也不能是无条件连接,因为计数会出错)