首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于select查询,如何在oracle /sql中使用位于哪里的50000个in或联接子句?

问对于select查询,如何在oracle /sql中使用位于哪里的50000个in或联接子句?
EN

Stack Overflow用户
提问于 2021-01-26 08:40:30
回答 1查看 106关注 0票数 0

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

代码语言:javascript
复制
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分钟。我的上述方法正确吗?通过考虑生产服务器中的最佳性能,还有比这更简单的方法吗?请注意,每隔一秒,生产数据库就会被客户使用。由于production很忙,我可以直接在production中运行这个查询,这会不会导致用户在使用每秒钟调用这个产品db的网站上表现缓慢。正确的答案将不胜感激!谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-26 08:45:20

你为什么不把那些receiptID存储在桌子上呢?

代码语言:javascript
复制
create table receiptids as
with temp_receiptIds(receiptId)
as
(
select 'M0000001' from dual
union all                        --> "union ALL" instead of "union"
...
) 
select * from temp_receiptids;

索引:

代码语言:javascript
复制
create index i1recid on receiptids (receiptIdD);

查看该查询现在的行为。

如果您--由于某种原因--无法做到这一点,那么看看CTE中的UNION ALL是否有任何好处。对于50.000行,这可能会产生不同的效果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65898161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档