简体   繁体   English

在PL-SQL中拆分多个表的联接

[英]Spliting join of multiple tables in PL-SQL

I need to create a function that returns a sysref cursor pointing to some result set. 我需要创建一个返回指向某个结果集的sysref游标的函数。 I need to join a global temporary table with several other tables, the basic aproach would look like this: 我需要将全局临时表与其他几个表连接起来,基本方法如下所示:

 Open sysrefcur for
     select *
     from
       (select * 
       from globaltemptable) t1
       left join t2
         on <conditions1>
       left join t3
         on <conditions2>
       left join t4
         on <conditions2>
       ...
       ;
       return sysrefcur;

The problem is that I have to joint the global temporary table (GTT) with around 30 additional tables (I know, it's a lot) but DBAs set a limit of 10 joins, 问题是我必须将全局临时表(GTT)与大约30个其他表联接(我知道很多),但是DBA设置了10个联接的限制,

So I was thinking of joining the GTT table with a few tables, inserting the result in another GTT, join it to another few tables and so on. 所以我在考虑将GTT表与几个表连接起来,将结果插入另一个GTT中,再将其连接到另外几个表中,依此类推。

I want to know if theres a better approach to this and if I can avoid creating additional temporary tables at all. 我想知道是否有更好的方法,是否可以完全避免创建其他临时表。

Thanks for your help. 谢谢你的帮助。

  1. Create views that do 5 joins each and then join 6 views. 创建每个进行5个联接的视图,然后联接6个视图。

  2. Use CTE. 使用CTE。 Define with statements with some joins then join results of with statements. 用一些联接定义with语句,然后联接with语句的结果。

I think the approach you suggested in your post is probably best. 我认为您在帖子中建议的方法可能是最好的。 It is simple to implement and understand, not horribly less efficient than doing this the right way (I'll second/third the criticisms of your DBA's policy), and it clearly conforms to the policy. 它的实现和理解很简单,并且效率不比正确的方法低得多(我将对DBA政策的批评占第二/三分之二),并且它显然符合该政策。 (Using views or CTEs are arguably "violations", since, at end of it all, it's a 30-table query still). (使用视图或CTE可以说是“违规”,因为到最后,它仍然是一个30张表的查询)。

and if I can avoid creating additional temporary tables at all. 以及是否可以避免创建其他临时表。

You can avoid creating an additional global temp table if you are able to add placeholder columns to the one you have. 如果能够将占位符列添加到现有的临时表中,则可以避免创建其他全局临时表。 In that case, your solution would look something like this: 在这种情况下,您的解决方案将如下所示:

ALTER TABLE globaltemptable ADD ... placeholder columns for T2, T3, T4, etc data...


MERGE INTO globaltemptable t
USING ( SELECT * FROM globaltemptable t1
        LEFT JOIN ... t2 through t10  (10 table join) ...
      ) u
ON ( t.primary_key = u.primary_key )
WHEN MATCHED THEN UPDATE SET ... t2 through t10 columns in globaltemptable ...
;

... repeat merge for tables t11 through t19

... repeat merge for tables t20 through t28

... repeat merge for tables t29 through t30


OPEN SYSREFCUR FOR SELECT * FROM globaltemptable -- no joins.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM