sql:避免使用笛卡尔积

sql:避免使用笛卡尔积,sql,oracle,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-

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

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;

我不确定这在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;

显而易见的解决方案是使用
ON
子句添加连接条件。也就是说,有没有办法将这两个表连接起来?这没有多大意义。giftId不是唯一的吗?你打算换张桌子吗?和pkgSize,它代表什么?请编辑您的问题并添加表结构,其中包含唯一标识符是什么。是的,它对于Oracle来说是正确的语法,并且它看起来像是执行OP所需的查询。