teradata查询优化

cngwdvgl  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(442)

我需要使用teradata sql assistant从数据库中选择一组符合某些条件的人员。以下哪种方法更快?原因是:
方法a

Create volatile table selection as ( 
Select * 
from table_a
Where id not in (sel id from table_b)
And id not in (sel id from table_c)
And id not in (sel id from table_d)
...
) With data primary index (id) on commit preserve rows;

方法b

Create volatile table selection as ( 
Select * 
from table_a
) With data primary index (id) on commit preserve rows;
Delete from selection where id in (sel id from table_b);
Delete from selection where id in (sel id from table_c);
Delete from selection where id in (sel id from table_d);
vxf3dgd4

vxf3dgd41#

您应该测试对数据和数据库的任何查询。
我希望 not exists 要表现得更好一点:

Select a.* 
from table_a a
where not exists (select 1 from table_b b where b.id = a.id) and
      not exists (select 1 from table_c c where c.id = a.id) and
      not exists (select 1 from table_d d where d.id = a.id) ;

特别是,这可以利用上的索引 table_b(id) , table_c(id) ,和 table_d(id) . 另外,语义更加清晰。 NOT IN with子查询可以返回(或不返回!)子查询返回时出现奇怪的结果 NULL .
也就是说,我希望正确地获取查询比创建表然后删除行更快。后者似乎涉及许多“make work”--将行添加到表中只是为了删除它们。

igsr9ssn

igsr9ssn2#

正如gordon所写,如果这些id被定义为可空的,notexists将比notin更好。否则他们是平等的,只是比较一下。
这三个子查询将转换为三个联接,另一个解决方案仅使用一个联接:

Create volatile table selection as ( 
Select * 
from table_a
Where id not in 
 ( sel id from table_b 
   union all
   sel id from table_c
   union all
   sel id from table_d
 )
...
) With data primary index (id) on commit preserve rows;

当然,性能还取决于每个表的行数和现有索引。

相关问题