微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 oracle pl/sql 中的 where 或 join 子句中使用 50,000 个 ID 进行选择查询?

如何解决如何在 oracle pl/sql 中的 where 或 join 子句中使用 50,000 个 ID 进行选择查询?

我有一个包含 5 万个收据 ID(硬编码值)的列表。我想在 where 条件或连接操作中应用这 5 万个 ID。我使用下面的“with”子句创建了一个临时表来收集这 5 万个 ID。然后我在连接查询中使用这个临时表进行过滤。

with temp_receiptIds(receiptId)
as
(
select 'M0000001' from dual
union
select 'M0000002' from dual
union
select 'M0000003' from dual
union
select 'M0000004' from dual
..
..
...
union
select 'M0049999' from dual
union
select 'M0050000' from dual
)

select sal.receiptId,prd.product_name,prd.product_price,sal.sales_date,sal.seller_name 
from product prd 
join sales sal on prd.product_id=sal.product_id 
join temp_receiptIds tmp on tmp.receiptId=sal.receiptId

每当我运行上述 select join 查询以根据业务人员的要求提取数据时,在生产服务器中获取结果大约需要 8 分钟。 我上面的方法正确吗?通过考虑生产服务器中的最佳性能,是否有比这更简单的方法。 请注意,每秒钟,客户都会使用生产数据库。由于生产数据库非常繁忙,我是否可以直接在生产数据库中运行此查询,是否会导致客户使用每秒调用此生产数据库的网站性能下降。正确答案将不胜感激!谢谢

解决方法

为什么不将这些 receiptID 存储到表中?

create table receiptids as
with temp_receiptIds(receiptId)
as
(
select 'M0000001' from dual
union all                        --> "union ALL" instead of "union"
...
) 
select * from temp_receiptids;

索引:

create index i1recid on receiptids (receiptIdD);

看看该查询现在的表现。


如果您 - 由于某种原因 - 不能这样做,请查看 CTE 中的 UNION ALL 是否有任何好处。对于 50.000 行,它可能会有所作为。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。