Faster way to Insert, via script, in Oracle?

前端 未结 1 1725
情书的邮戳
情书的邮戳 2020-12-03 23:56

I\'m working with a C# .NET 4.0 application, that uses ODP.NET 11.2.0.2.0 with an Oracle 11g database. The application pre-loads a few look-up tables with data, and since m

相关标签:
1条回答
  • 2020-12-04 00:26

    Problem

    Parsing time may increase exponentially with certain types of statements, especially INSERT ALL. For example:

    --Clear any cached statements, so we can consistently reproduce the problem.
    alter system flush shared_pool;
    alter session set sql_trace = true;
    
    --100 rows
    INSERT ALL
        INTO FileIds(Id,FileTypeGroupId) VALUES(1, 1)
        ...
        repeat 100 times
        ...
    select * from dual;
    
    --500 rows
    INSERT ALL
        INTO FileIds(Id,FileTypeGroupId) VALUES(1, 1)
        ...
        repeat 500 times
        ...
    select * from dual;
    
    alter session set sql_trace = false;
    

    Run the trace file through tkprof, and you can see the Parse time increases dramatically for a large number of rows. For example:

    100 rows:

    call     count       cpu    elapsed       disk      query    current        rows
    ------- ------  -------- ---------- ---------- ---------- ----------  ----------
    Parse        1      0.06       0.05          0          1          0           0
    Execute      1      0.00       0.00          0        100        303         100
    Fetch        0      0.00       0.00          0          0          0           0
    ------- ------  -------- ---------- ---------- ---------- ----------  ----------
    total        2      0.06       0.05          0        101        303         100
    

    500 rows:

    call     count       cpu    elapsed       disk      query    current        rows
    ------- ------  -------- ---------- ---------- ---------- ----------  ----------
    Parse        1     14.72      14.55          0          0          0           0
    Execute      1      0.01       0.02          0        502       1518         500
    Fetch        0      0.00       0.00          0          0          0           0
    ------- ------  -------- ---------- ---------- ---------- ----------  ----------
    total        2     14.74      14.58          0        502       1518         500
    

    Solutions

    1. Break your large statement into several smaller statements. It's difficult to find the optimal size. On some versions of Oracle there's a magic number of rows that will cause the problem. I usually go for about 100 rows - enough to get most of the benefits of grouping statements, but low enough to avoid the parsing bug. OR...
    2. Try the insert into ... select ... from dual union all ... method instead. It usually runs much faster, although it's parsing performance may also degrade significantly with size.
    3. Upgrade Oracle. Parsing performance has improved in newer versions. I can no longer reproduce this issue in version 12.2.

    Warning

    Don't learn the wrong lesson from this. If you're worried about SQL performance, 99% of the time you're better off grouping similar things together instead of splitting them apart. You're doing things the right way, you just ran into a weird bug. (I searched My Oracle Support but couldn't find an official bug for this.)

    0 讨论(0)
提交回复
热议问题