準備されパラメータ化されたSQL文に関するttIsqlの使用

TimesTenアプリケーションでは、SQL文を1回のみ準備して複数回実行したほうが、実行するたびに文を再準備するよりもはるかに効率的です。ttIsqlユーティリティには、準備されたSQL文を操作するための一連の組込みコマンドがあります。

次に、これらのコマンドの概要を示します。

  • prepare: SQL文を準備します。SQLPrepare ODBCコールに対応します。

  • exec - 事前に準備済の文を実行します。SQLExecute ODBCコールに対応します。

  • execandfetch: 事前に準備済の文を実行し、すべての結果行をフェッチします。1つ以上のSQLFetchコールの前に実行されるSQLExecuteコールに対応します。

  • fetchall - 以前に実行した文のすべての結果行をフェッチします。1つ以上のSQLFetchコールに対応します。

  • fetchone - 以前に実行した文の1行のみをフェッチします。1つのみのSQLFetchコールに対応します。

  • close - 結果セットが生成された以前に実行済の文に対する結果セットのカーソルをクローズします。SQL_CLOSEオプションが指定されたSQLFreeStmtコールに対応します。

  • free: 事前に準備済の文をクローズします。SQL_DROPオプションが指定されたSQLFreeStmtコールに対応します。

  • describe - 入力パラメータおよび結果列を含む準備済の文を表示します。

ttIsqlユーティリティの準備済の文コマンドは、SQL文のパラメータ・マーカーも処理します。準備済のSQL文にパラメータ・マーカーが含まれている場合は、ttIsqlのランタイムに、文の各パラメータに値を入力するように自動的に求められます。

次の例では、ttIsqlユーティリティの準備済の文コマンドを使用して、NUMBERおよびCHAR列を含む表のINSERT文を準備します。文が準備された後、文の2つのパラメータに対してそれぞれ異なる値を使用して2回実行されます。また、ttIsqlユーティリティのtimingコマンドを使用して、各コマンドに関連付けられている主要ODBC関数コールの実行に必要な経過時間を表示します。

Command> connect "DSN=database1";
Connection successful: 
DSN=database1;DataStore=/disk1/databases/database1;DatabaseCharacterSet=AL32UTF8;
ConnectionCharacterSet=AL32UTF8;PermSize=128;
(Default setting AutoCommit=1)

Command> timing 1;
Command> create table t1 (key number not null primary key, value char(20));
Execution time (SQLExecute) = 0.007247 seconds.
Command> prepare insert into t1 values (:f, :g);
Execution time (SQLPrepare) = 0.000603 seconds.

Command> exec;
Type '?' for help on entering parameter values.
Type '*' to end prompting and abort the command.
Type '-' to leave the parameter unbound.
Type '/' to leave the remaining parameters unbound and execute the command.
Enter Parameter 1 'F' (NUMBER) > 1;
Enter Parameter 2 'G' (CHAR) > 'abc';
1 row inserted.
Execution time (SQLExecute) = 0.000454 seconds.

Command> exec;
Type '?' for help on entering parameter values.
Type '*' to end prompting and abort the command.
Type '-' to leave the parameter unbound.
Type '/' to leave the remaining parameters unbound and execute the help command.
Enter Parameter 1 'F' (NUMBER) > 2;
Enter Parameter 2 'G' (CHAR) > 'def';
1 row inserted.
Execution time (SQLExecute) = 0.000300 seconds.

Command> free;
Command> select * from t1;
< 1, abc                  >
< 2, def                  >
2 rows found.
Execution time (SQLExecute + Fetch Loop) = 0.000226 seconds.

Command> disconnect;
Disconnecting...
Execution time (SQLDisconnect) = 2.911396 seconds. 

前述の例では、準備するSQL文がprepareコマンドの直後に指定されています。ttIsqlでSQL文を準備すると、常に、一意のコマンドIDが準備済の文に割り当てられます。ttIsqlユーティリティでは、複数の準備済の文の追跡にこのIDが使用されます。ttIsqlセッションには、最大256の準備済の文が同時に存在できます。freeコマンドを実行すると、準備済のSQL文に対するコマンドIDの関連付けが自動的に解除されます。

準備済の文コマンドを使用した場合にttIsqlによって生成されるコマンドIDを表示するには、文を準備する前にverbosityコマンドを使用して冗長性レベルを4に設定するか、describe *コマンドを使用して準備済のすべての文をそれらのIDとともに表示します。

コマンドIDは、ttIsqlの準備済の文コマンドを使用する場合に明示的に参照できます。Oracle TimesTen In-Memory DatabaseリファレンスttIsqlを参照するか、ttIsqlコマンド・プロンプトでhelpと入力してください。

次の例では、1つのNUMBERパラメータを含む述語が指定されているSELECT文を準備および実行します。fetchoneコマンドは、この文によって生成された結果行をフェッチするために使用します。showplanコマンドは、この文の実行時にTimesTen問合せオプティマイザで使用される実行計画を表示するために使用します。また、冗長性レベルは、準備済の文を追跡するためにttIsqlで使用されるコマンドIDが表示されるように4に設定します。

Command> connect "DSN=database1";
Connection successful: 
DSN=database1;DataStore=/disk1/databases/database1;DatabaseCharacterSet=AL32UTF8;
ConnectionCharacterSet=AL32UTF8;PermSize=128;
(Default setting AutoCommit=1)

Command> CREATE TABLE T1 (KEY NUMBER NOT NULL PRIMARY KEY, VALUE CHAR (64));
The command succeeded.
Command> INSERT INTO T1 VALUES (1, 'abc');
1 row inserted.
The command succeeded.
Command> autocommit 0;
Command> showplan 1;
Command> verbosity 4;
The command succeeded.
Command> prepare SELECT * FROM T1 WHERE KEY=?;
Assigning new prepared command id = 0.

Query Optimizer Plan:
 
  STEP:                1
  LEVEL:               1
  OPERATION:           RowLkRangeScan
  TBLNAME:             T1
  IXNAME:              T1
  INDEXED CONDITION:   T1.KEY = _QMARK_1
  NOT INDEXED:         <NULL>
 
The command succeeded.
Command> exec;

Executing prepared command id = 0.
 
Type '?' for help on entering parameter values.
Type '*' to end prompting and abort the command.
Type '-' to leave the parameter unbound.
Type '/;' to leave the remaining parameters unbound and execute the command.
 
Enter Parameter 1 '_QMARK_1' (NUMBER) > 1
The command succeeded.
Command> fetchone;
Fetching prepared command id = 0.
< 1, abc                                                              >
1 row found.
The command succeeded.
Command> close;
Closing prepared command id = 0.
The command succeeded.
Command> free;
Freeing prepared command id = 0.
The command succeeded.
Command> commit;
The command succeeded.
Command> disconnect;
Disconnecting...
The command succeeded.
Command>

ノート:

Oracle TimesTen In-Memory Database PL/SQL開発者ガイドTimesTenのPL/SQLの概要を参照してください。