Sei sulla pagina 1di 1

68 Chapter 2: Inserting

The INPUT statement doesnt take any of the shortcuts used by LOAD TABLE,
so it isnt nearly as fast. What INPUT actually does is pass each row to the data-
base engine, one at a time, to be inserted. You can see this by turning on the
Request-Level Logging feature to see what the engine sees. Here is an example
that compares LOAD TABLE with INPUT when a two-line input file is loaded
into a two-column table:
CREATE TABLE t1 (
col_1 INTEGER NOT NULL,
col_2 INTEGER NOT NULL );

CALL sa_server_option ( 'Request_level_log_file', 'r.txt' );


CALL sa_server_option ( 'Request_level_logging', 'SQL+hostvars' );

LOAD TABLE t1 FROM 't1_f.txt';


INPUT INTO t1 FROM 't1_f.txt';

CALL sa_server_option ( 'Request_level_logging', 'NONE' );


Here is the contents of t1_f.txt:
1, 1
2, 2
The Request-Level Logging file r.txt shows that the engine received the LOAD
TABLE command as it was coded:
STMT_PREPARE "LOAD TABLE t1 FROM 't1_f.txt'"
STMT_EXECUTE Stmt=66327
However, the INPUT statement got changed into an INSERT that was executed
twice, once for each record in the input file, with two host variables used, one
for each field:
STMT_PREPARE "INSERT INTO "t1" ("col_1","col_2") VALUES (?,?)"
STMT_EXECUTE Stmt=66339
HOSTVAR 0 varchar '1'
HOSTVAR 1 varchar '1'
STMT_EXECUTE Stmt=66339
HOSTVAR 0 varchar '2'
HOSTVAR 1 varchar '2'
This explains why INPUT is slower than LOAD TABLE, but also why the dis-
advantages of the LOAD TABLE shortcuts are avoided: The INPUT statement
does cause insert triggers to be fired, it does acquire individual row locks, and it
does write the inserts to the transaction log.
The Request-Level Logging feature is explained further in Section 10.2 of
Chapter 10, Tuning.

2.5 Chapter Summary


This chapter described the five different formats of the INSERT statement:
INSERT with a VALUES list for all columns or a list of named columns,
INSERT with a SELECT for all column values or a list of named columns, and
INSERT using the AUTO NAME facility. Also described were the LOAD
TABLE and ISQL INPUT statements for inserting data from an external file.
The next chapter moves on to the third step in the life cycle of a database:
selecting rows.

Potrebbero piacerti anche