Oracle将大量数据插入临时表

时间:2021-11-23 16:45:46

I need to insert 12000 strings into temporary table, 6 characters each. 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. I wonder how can I speed up this process?

我需要在临时表中插入12000个字符串,每个字符串6个字符。目前我正在使用SELECT Code FROM Articles,其中Code ='111111'OR Code ='222222'...命令有400 000个字符,需要20秒才能执行。我想知道如何加快这个过程?

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 (...)

我真的不需要选择文章中的代码,但是oracle不支持INSERT INTO(...)VALUES(...)中的多个记录

2 个解决方案

#1


2  

IN is generally faster than OR as it stops evaluating as soon as the condition is met. See previous q here

IN通常比OR快,因为只要满足条件就会停止评估。请参阅前面的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')...)

#2


0  

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#:

        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!

#1


2  

IN is generally faster than OR as it stops evaluating as soon as the condition is met. See previous q here

IN通常比OR快,因为只要满足条件就会停止评估。请参阅前面的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')...)

#2


0  

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#:

        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!