如何在 oracle pl/sql 中的 where 或 join 子句中使用 50,000 个 ID 进行 select 查询?
how to use 50 thousand Ids at where or join clause in oracle pl/sql for a select query?
我有一个包含 5 万个收据 ID(硬编码值)的列表。我想在 where 条件或连接操作中应用这 50,000 个 ID。我在下面使用 'with' 子句创建了一个临时 table 来收集这 5 万个 ID。然后我在连接查询中使用这个临时 table 进行过滤。
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加入查询以根据业务人员的要求提取数据时,在生产服务器中获取结果大约需要8分钟。
我的上述方法是否正确?通过考虑生产服务器的最佳性能,是否有比这更简单的方法。
请注意,每一秒,生产数据库都被客户使用。由于生产数据库非常繁忙,我可以直接在生产数据库中 运行 这个查询吗,它会导致客户使用每秒调用此生产数据库的网站的性能下降吗?正确答案将不胜感激!谢谢
为什么不将那些 receiptID
存储到 table 中?
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 行,它可能会有所作为。
我有一个包含 5 万个收据 ID(硬编码值)的列表。我想在 where 条件或连接操作中应用这 50,000 个 ID。我在下面使用 'with' 子句创建了一个临时 table 来收集这 5 万个 ID。然后我在连接查询中使用这个临时 table 进行过滤。
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加入查询以根据业务人员的要求提取数据时,在生产服务器中获取结果大约需要8分钟。 我的上述方法是否正确?通过考虑生产服务器的最佳性能,是否有比这更简单的方法。 请注意,每一秒,生产数据库都被客户使用。由于生产数据库非常繁忙,我可以直接在生产数据库中 运行 这个查询吗,它会导致客户使用每秒调用此生产数据库的网站的性能下降吗?正确答案将不胜感激!谢谢
为什么不将那些 receiptID
存储到 table 中?
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 行,它可能会有所作为。