db2 "NOT LOGGED INITIALLY" 不工作

db2 "NOT LOGGED INITIALLY" not working

我的 DB2 版本是 LUW v11.1。

我正在运行宁select查询大表并插入到新表中,所以我尝试在创建新表时使用"NOT LOGGED INITIALLY"以避免生成大量日志。但是好像NLI这个选项没有用。

以下是我的sql代码:

create table diabetes_v3_2.comm_outpatient_prescription_drugs_t2dm
    as (select * from commercial.outpatient_prescription_drugs)
    with no data
    not logged initially;
insert into diabetes_v3_2.comm_outpatient_prescription_drugs_t2dm
    select * from commercial.outpatient_prescription_drugs
        where enrolid in (
            select enrolid from diabetes_v3_2.t2dm_cohort_filter_age_enrollment
        );


create table diabetes_v3_2.comm_outpatient_services_t2dm
    as (select * from commercial.outpatient_services)
    with no data
    not logged initially;
insert into diabetes_v3_2.comm_outpatient_services_t2dm
    select * from commercial.outpatient_services
        where enrolid in (
            select enrolid from diabetes_v3_2.t2dm_cohort_filter_age_enrollment
        );

我运行脚本为db2 -tvf script.sql。但我仍然收到 "SQL0964C The transaction log for the database is full" 错误:

/* Generate cohort data for the cohort after *  filtering according to age and continuous * enrollment criteria. */ /* facility header */ /* inpatient admissions */ /* inpatient services */ /* outpatient prescription drugs */ create table diabetes_v3_2.comm_outpatient_prescription_drugs_t2dm as (select * from commercial.outpatient_prescription_drugs) with no data not logged initially
DB20000I  The SQL command completed successfully.

insert into diabetes_v3_2.comm_outpatient_prescription_drugs_t2dm select * from commercial.outpatient_prescription_drugs where enrolid in ( select enrolid from diabetes_v3_2.t2dm_cohort_filter_age_enrollment )
  Number of rows affected : 275423901
DB20000I  The SQL command completed successfully.

/* outpatient services */ create table diabetes_v3_2.comm_outpatient_services_t2dm as (select * from commercial.outpatient_services) with no data not logged initially
DB20000I  The SQL command completed successfully.

insert into diabetes_v3_2.comm_outpatient_services_t2dm select * from commercial.outpatient_services where enrolid in ( select enrolid from diabetes_v3_2.t2dm_cohort_filter_age_enrollment )
DB21034E  The command was processed as an SQL statement because it was not a
valid Command Line Processor command.  During SQL processing it returned:
SQL0964C  The transaction log for the database is full.  SQLSTATE=57011

这是为什么?

要正确使用 NOT LOGGED INITIALLY,进行更改的应用程序不应启用 AUTOCOMMIT。关闭 AUTOCOMMIT 也有助于定义事务的范围:

  • 对于 CLP,您可以使用环境变量 DB2OPTIONS :

    关闭 AUTOCOMMIT
    export DB2OPTIONS=+c
    
  • 如果执行包含更新 SQL 语句(例如插入)的脚本,并且未设置 DB2OPTIONS,则可以使用以下命令执行脚本:

    db2 +c -tvf input_script.sql  -z output_script.out
    

确保在脚本中添加明确的 COMMIT 语句,以确保它们出现在合理的位置。