我有一个5万收据I的列表(硬编码值)。我想在where条件或join操作中应用这5万个Ids。我在下面的“with”子句中创建了一个临时表来收集这5万个Ids。然后,我在联接查询中使用这个临时表进行筛选。
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的网站上表现缓慢。正确的答案将不胜感激!谢谢
发布于 2021-01-26 08:45:20
你为什么不把那些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行,这可能会产生不同的效果。
https://stackoverflow.com/questions/65898161
复制相似问题