我正在从事一项日常自动化工作,该工作根据 Oracle 数据库检查文本文件中的记录。 我们每天都会收到来自外部团队的文本文件,其中包含大约 100,000 条记录。文本文件将采用 unix 格式,有 6 列,以 | 分隔。符号。
例如, 高动态范围 1
home/sample/file|testvalue1|testvalue2|testval3|testval4|testval5
TRL
我需要检查testval3和testval5中的值是否存在于我的oracle数据库表中。该表大约有 1000 万条记录。 我目前正在通过 shellscript 处理它。在 shellscript 中,我正在读取文本文件并循环遍历每一行。在循环内,我传递每行的值并对数据库运行查询。如果数据库中不存在记录,我必须将它们输出到 csv 文件。使用以下查询:
select ‘testval3’,’testval5’ from dual
where not exists (select primarykeycolumn
from mytable where mycolumn=testval3 and mycolumn2=testval5)
由于输入文件有 100000 个条目,因此我的循环将运行查询 100000 次,每次都会检查包含 1000 万条记录的表。这使得我的批处理作业运行了很多小时,我必须终止它。有没有更好的方法来处理这种情况?如果没有更好的方法通过 shellscript 来做到这一点,我也可以使用 java。
最佳答案
下面是一种简单的解决方案,可以确保不会出现超时,甚至您不需要扫描数百万条记录 100K 次。
一次性设置: 创建临时临时表:
create table a_staging_table(
testvalue1 varchar2(255),
testvalue2 varchar2(255),
testval3 varchar2(255),
testval4 varchar2(255),
testval5 varchar2(255)
);
----重复过程
将“CSV/TEXT”数据加载到临时表中:
some_file_name.ctl:此文件包含以下加载数据命令。
load data
INFILE 'home/sample/file.csv'
INTO TABLE a_staging_table
APPEND FIELDS TERMINATED BY '|'
(testvalue1,testvalue2,testval3,testval4,testval5);
现在,运行 SQL 加载程序将数据加载到临时表表单中。
sqlldr userid=dbUserName/dbUserPassword control=some_file_name.ctl log=some_file_name.log
您的数据已加载到临时表中。现在连接临时表和 your_original_table 以识别不存在的记录。
第一种方法: 使用 SQL*PLUS 对以下 SQL 的输出进行假脱机处理:
select s.testval3,testval5
from (select distinct testval3,testval5
from a_staging_table) s
where not exists
(select 1
from your_original_table
where mycolumn1=s.testval3
and mycolumn2=s.testval5);
第二种方式:
Begin
for x in (
select s.testval3,testval5
from (select distinct testval3,testval5
from a_staging_table) s
where not exists
(select 1
from your_original_table
where mycolumn1=s.testval3
and mycolumn2=s.testval5)
) loop
DBMS_OUTPUT.put_line('testval3: '||x.testval3 || ' ------ '||'testval5: '||x.testval5);
--write all these values into another file saying that these are not matching values, using UTL_FILE.
--Then finally truncate the table "a_staging_table"
--so that this data will not available next time, and next time again process will run with different file
end loop;
关于java - 检查 Oracle DB .shellscript 作业中的大量记录超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50284671/