Efficient approach to populate the temp table

谁说胖子不能爱 提交于 2019-12-13 00:08:48

问题


Default_accounts
========================
p_type        varchar2
t_type        varchar2
A         varchar2
B         varchar2
C             varchar2
D             varchar2
=========================

p_type and t_type is the primary key of the table

p_type  t_type  A   B  C  D
============================
apple    sell   Y   N  N  Y
banana   buy    N   N  N  Y
orange   sell   Y   N  N  N
mango    buy    Y   Y  N  Y
papaya   buy    Y   N  N  Y
apple    buy    Y   N  N  Y
banana   sell   Y   Y  Y  Y
============================


 Accounts_Exceptions
=============================
acc_excep_id          number    --sequence (Primary key)
p_type                varchar2
t_type                varchar2
excep_attribute       varchar2  --contains column of default_accounts like A,B,C,D
priority              number
excep_base_sql        varchar2
excep_value           varchar2
===============================

Unique constraint :p_type,t_type,excep_attribute and priority foreign_key :p_type and t_type from Default_accounts

  acc_excep_id   p_type    t_type  excep_attribute priority  excep_base_sql   excep_value
---------------------------------------------------------------------------------------------
     1           apple    buy       A              1         --some--         XYZ
     2           apple    buy       A              2         --some--         PQR
     3           banana   sell      B              1         --some--         GHT
     4           banana   sell      B              2         --some--         GFF
     5           orange   sell      C              1         --some--         DSA    
---------------------------------------------------------------------------------------------

excep_base_sql: it contain a sql query like select 1 from alloc where alloc_id =:alloc; --its a sample query but it always contain bind vairable :alloc

  1. Now my requirement is that i got a cursor having which contain records of alloc_id ,p_type,t_type

  2. I put the records in a temporary table having structure as below:

     alloc_id p_type  t_type  A   B    C   D
    ---------------------------------------------
     11       apple   buy    
     22       apple   sell
     33       mango   buy
     12       mango   buy
     13       mango   buy
     24       banana  buy
     54       orange  sell
    

    3.But as you see ,i need to populate A,B,C and D value in bulk ,temp table can contain around millions of record.

    4.For populating the value i need to check the account_exceptions table first for any exception . For example: i fetched 11 apple buy from the temp table and see the exception table ,In exception table find records having p_type as apple and t t_type as buy order by priority in ascending order .

    5.So, records with acc_excep_id :1,2 are fetched , i fetch first record and run the excep_base_sql with alloc_id 11 ,if row is returned then i ll update the excep_attribute A with XYZ in temp table and then skip the second record having priority 2 ,because the record with priority one satisfy .

    6.If no entry of apple buy is present in execptions table ,then i need to populate the temp table with default_accounts table ,so this value is Y.

    7.So ,if no exception satisy then default value is picked from default_accounts.

    8.In short i need to populate the value of A B C D in table temp.

    9.I need a way to efficiently populate the data into temp table for value A B C D .

Can anyone help me with this approach


回答1:


I'm going to recommend two things.

1) An insert as select or create table as select is always the most efficient when doing a bulk insert.

2) It "sounds" like you are trying to convert rows into columns. I would recommend Oracle's pivot feature. The pivot feature is described here: Oracle Base Pivot



来源:https://stackoverflow.com/questions/9966852/efficient-approach-to-populate-the-temp-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!