sql:避免使用笛卡尔积

时间:2016-12-14 07:53:32

标签: sql oracle

有人可以帮助优化笛卡尔积,因为它需要花费大量时间来执行:

select c_msg.gft_id,
       a_msg.pkSize
from   Gifts c_msg,
       (select   giftContId,
                 count(*) pkSize
        from     Gifts gft
        GROUP BY gft.giftContId) a_msg 
where  sentDate between (sysdate-10) and (sysdate-1)
and    a_msg.giftContId = c_msg.giftContId
order by a_msg.giftId;

1 个答案:

答案 0 :(得分:3)

我不确定这适用于Oracle。在postgresql中,你可以这样做:

SELECT gft_id, count(*) over (partition by giftContId) as pkSize
FROM Gifts
WHERE sentDate between (sysdate-10) and (sysdate-1)
ORDER BY giftId;