请在此处找到生成组合的解决方案。这是我们之前在房地产开发软件中遇到的问题的一个很好的变体。
创建和填充数据模型
首次设置:
create table contents
( item_purpose_code number
, item_category_id number
, item_id number
)
/
begin
insert into contents values (1, 101, 50);
insert into contents values (2, 202, 94);
insert into contents values (2, 202, 95);
commit;
end;
/
辅助视图
首先,我创建一些视图。但当然你也可以内联它们或使用with
.
--
-- Add to each row the consecutive number of the driver columns
-- (here only item_purpose_code) and for each different value
-- for the driver columns a consecutive number that restarts
-- when a new driver column value starts.
--
create or replace force view sequencedrows
as
select item_purpose_code
, item_category_id
, item_id
, dense_rank()
over
( order
by item_purpose_code
) driver_seq
, row_number()
over
( partition
by item_purpose_code
order
by item_category_id
, item_id
)
values_per_driver_seq
from contents
/
--
-- Generate list of combinations.
--
create or replace force view combinations
as
select sys_connect_by_path (driver_seq || '-' || values_per_driver_seq, '#') || '#' combination
from sequencedrows
where level = ( select max(driver_seq) from sequencedrows )
start
with driver_seq = 1
connect
by
nocycle driver_seq = prior driver_seq + 1
/
有了这些,它变得非常简单,因为组合已经包含在字段中combination
并且行已经编号:
select c.combination
, s.item_purpose_code
, s.item_category_id
, s.item_id
from combinations c
join sequencedrows s
on c.combination like '%#' || to_char(s.driver_seq) || '-' || to_char(s.values_per_driver_seq) || '#%'
order
by c.combination
, s.driver_seq
, s.values_per_driver_seq
/
结果是:
#1-1#2-1# 1 101 50
#1-1#2-1# 2 202 94
#1-1#2-2# 1 101 50
#1-1#2-2# 2 202 95
表现
根据数据量和索引,性能可能不足以进行交互使用。然而,在我们的房地产开发包中,我们发现自 Oracle 11g 以来,即使生成了 50K 行,性能也是可以接受的。Oracle 10g 在优化方面做得不太理想。
当您的站点的性能不可接受时,请列出一些关键统计数据或添加重现场景。