INSERT ALL statement incredibly slow, even when no records to insert

430 Views Asked by At

This is on 11g. I have an INSERT ALL statement which uses a SELECT to build up the values to insert. The select has some subqueries that check that the record doesn't already exist. The problem is that the insert is taking over 30 minutes, even when there are zero rows to insert. The select statement on its own runs instantly, so the problem seems to be when it is used in conjunction with the INSERT ALL. I rewrote the statement to use MERGE but it was just as bad.

The table has no triggers. There is a primary key index, and a unique constraint on two of the columns, but nothing else that looks like it might be causing an issue. It currently has about 15000 rows, so definitely not big.

Has anyone a suggestion for what might be causing this, or how to go about debugging it?

Here's the INSERT ALL statement.

    insert all
  into template_letter_merge_fields (merge_field_id, letter_type_id,table_name,field_name,pretty_name, tcl_proc)
            values (template_let_mrg_fld_sequence.nextval,letter_type_id,table_name,field_name, pretty_name, tcl_proc)
select lt.letter_type_id,
    i.object_type as table_name,
    i.interface_key as field_name,
    i.pretty_name as pretty_name,
    case 
    when w.widget = 'dynamic_select' then
        'dbi::'||i.interface_key||'::get_name'
    when w.widget = 'category_tree' and 
         i.interface_key not like '%_name' and 
         i.interface_key not like '%_desc' then
        'dbi::'||i.interface_key||'::get_name'
    else
        'dbi::'||i.interface_key||'::get_value'
    end as tcl_proc
  from template_letter_types lt,                          
     dbi_interfaces i 
     left outer join acs_attributes aa on (aa.object_type||'_'||aa.attribute_name = i.interface_key
                           and decode(aa.object_type,'person','party','aims_organisation','party',aa.object_type) = i.object_type)
     left outer join flexbase_attributes fa on fa.acs_attribute_id = aa.attribute_id
     left outer join flexbase_widgets w on w.widget_name = fa.widget_name
 where i.object_type IN (select linked_object_type
                     from template_letter_object_map lom
                    where lom.interface_object_type = lt.interface_object_type
                    union select lt.interface_object_type from dual
                    union select 'template_letter' from dual)  
   and lt.interface_object_type = lt.interface_object_type
   and not exists (select 1
                from template_letter_merge_fields m
              where m.sql_code is null
                 and m.field_name = i.interface_key
                 and m.letter_type_id = lt.letter_type_id)
   and not exists (select 1
                from template_letter_merge_fields m2
              where m2.pretty_name = i.pretty_name
                 and m2.letter_type_id = lt.letter_type_id)
0

There are 0 best solutions below