简体   繁体   English

Oracle将大量数据插入临时表

[英]Oracle insert lots of data to temporary table

I need to insert 12000 strings into temporary table, 6 characters each. 我需要在临时表中插入12000个字符串,每个字符串6个字符。 Currently I am doing it with SELECT Code FROM Articles where Code = '111111' OR Code = '222222' ... command that has 400 000 characters and takes 20 seconds to execute. 目前,我正在使用SELECT Code FROM Articles where Code = '111111' OR Code = '222222' ...来执行此操作,该命令具有40万个字符,执行时间为20秒。 I wonder how can I speed up this process? 我想知道如何加快这个过程?

I do not need any validation on my codes. 我不需要验证我的代码。 They need to be transferred from application to database as part of the query or as command parameter. 它们需要作为查询的一部分或作为命令参数从应用程序传输到数据库。

I do not really need to do Select Code From Articles , but oracle does not support multiple records in INSERT INTO (...) VALUES (...) 我真的不需要Select Code From Articles ,但是oracle不支持INSERT INTO (...) VALUES (...)多个记录INSERT INTO (...) VALUES (...)

IN is generally faster than OR as it stops evaluating as soon as the condition is met. IN通常比OR快,因为一旦满足条件, IN就会停止评估。 See previous q here 在这里查看上一个q

So: 所以:

Select code
from Articles
where Code in ('111111','222222')

To allow for the extremely large list, tuples: 为了允许非常大的列表,元组:

Select code
from Articles
where ('1', Code) in (('1','111111'),
                      ('1','222222')...)

As @AlexPoole pointed out it is best to use table valued parameter. 正如@AlexPoole指出的那样,最好使用表值参数。

type t_varchar_tab is table of varchar2(10) index by pls_integer;
procedure insert_table(i_id_tab in t_varchar_tab);

body: 身体:

  procedure insert_table(i_id_tab in t_varchar_tab) is
  begin
  -- if You have temporary table You do not want commits here
  forall i in i_id_tab.first .. i_id_tab.last
      insert into MY_SCHEMA.MY_TABLE
      VALUES (i_id_tab(i));
  end ins_test;

C#: C#:

        using (OracleCommand dbCommand = connection.CreateCommand())
        {
            dbCommand.CommandText = "MY_SCHEMA.MY_PACKAGE.insert_table";
            dbCommand.CommandType = CommandType.StoredProcedure;

            var inputArray = new OracleParameter
            {
                Direction = ParameterDirection.Input,
                CollectionType = OracleCollectionType.PLSQLAssociativeArray,
                Value = StringList.ToArray()
            };
            dbCommand.Parameters.Add(inputArray);
            await dbCommand.ExecuteNonQueryAsync();

Thanks Alex! 谢谢亚历克斯!

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

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