我正在尝试加快以下速度
create table tab2 parallel 24 nologging compress for query high as
select /*+ parallel(24) index(a ix_1) index(b ix_2)*/
a.usr
,a.dtnum
,a.company
,count(distinct b.usr) as num
,count(distinct case when b.checked_1 = 1 then b.usr end) as num_che_1
,count(distinct case when b.checked_2 = 1 then b.usr end) as num_che_2
from tab a
join tab b on a.company = b.company
and b.dtnum between a.dtnum-1 and a.dtnum-0.0000000001
group by a.usr, a.dtnum, a.company;
通过使用索引
create index ix_1 on tab(usr, dtnum, company);
create index ix_2 on tab(usr, company, dtnum, checked_1, checked_2);
但是执行计划告诉我,这将是对两个索引的索引全扫描,并且计算时间很长(1天不够)。
关于数据。表格选项卡有超过 300 万条记录。没有一个列是唯一的。此处的唯一值是 (usr, dtnum) 对,其中 dtnum 是日期和时间,格式为 yyyy,mmddhh24miss 中的数字。列checked_1、checked_2 的值来自集合(null、0、1、2)。Company 持有公司的 ID。每对只能有一个值 checked_1、checked_2 和 company,因为它是唯一的。每个用户可以是具有不同 dtnum 的多对。
编辑
@Roberto Hernandez:我附上了执行计划的图片。至于parallel 24,在我们公司,我们被告知要创建带有选项“parallel [num] nologging compress for query high”的表。我正在使用 24,但我不是该领域的专家。
@Sayan Malakshinov:http ://sqlfiddle.com/#!4/40b6b/2在这里,我通过使用checked_1 = checked_2 提供数据进行了简化,但在现实生活中这可能不是真的。
@scaisEdge:对于
create index my_id1 on tab (company, dtnum);
create index my_id2 on tab (company, dtnum, usr);